- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果任何对 pygames 有基本了解的人可以帮助我解决我目前面临的问题,那就太好了。对于你们这些绝对的神来说,应该不会太难。
我现在的代码遇到了问题。
我正在尝试制作一个简单的游戏,其中程序显示一个随机字母,用户必须在该字母到达页面底部之前输入该字母。
我有在随机列中显示随机字母的代码,但是,一旦字母到达底部,我只能让程序打印“结束”,然后退出程序。
我无法理解如何进一步开发代码。这是现在的代码:
import os #
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (1335,330) # sets the coordinates so that the window pops up in the lower right side of the screen (1600x900 screen)
import pygame # pygame is the platform im using
import random # random needs to be imported for the random letters and columns
pygame.init() # initializes pygame
win = pygame.display.set_mode((260, 527)) # size of the window ,screenwidth = 260, screenheight = 527
pygame.display.set_caption("Daveypoo is King") # title of the window
clock = pygame.time.Clock() # used for frame rate
bg = pygame.image.load('phone3cropped.jpg') # background picture upload
qp = pygame.image.load('q.png') # uploads pictures of the letters (use paint, 50x50 pixels, black, 1 pixel outline, font = 36)
wp = pygame.image.load('w.png') # ^
ep = pygame.image.load('e.png') # ^
ap = pygame.image.load('a.png') # ^
sp = pygame.image.load('s.png') # ^
dp = pygame.image.load('d.png') # ^
jp = pygame.image.load('j.png') # ^
kp = pygame.image.load('k.png') # ^
lp = pygame.image.load('l.png') # ^
letterlist = [qp,wp,ep,ap,sp,dp,jp,kp,lp] # creates a list of letter images
randletter = random.choice(letterlist) # randomizes the list of images^
class player(): # creates letter instance
def __init__(self, x,y,width,height,end): # initializes the list
self.x = x
self.y = y
self.width = width
self.height = height
self.path = [self.y, 415] # creates the path that the letters will travel
self.vel = 1 # this is where the velocity of the letters is set
self.hitbox = (self.x, self.y, 50, 50) # creates the hitbox for the letter pictures (no need to change for now -- 50x50 is good)
def move(self, win): # creates the movement for the letters
if self.vel > 0:
if self.y + self.vel < self.path[1] : # if the bottom of the letter is less than the end, then:
self.y += self.vel # keep going
else: # if not:
self.vel = 0 # stop moving
else: # if the bottom of the letter is at the end, then
print('end')
quit()
#redrawGameWindow()
# this is where the action to make a new letter has to go
class greenzone(object): # creates the zone (greenzone) where the player should get the letter in
def __init__(self,x,y,width,height): # initializes it
self.x = x
self.y = y
self.width = width
self.height = height
self.hitbox = (16, 342, 228, 66) # first two letters are top left (x,y) coordinate of the zone, next is width, next is height
# def hit(self):
# print('hit')
def redrawGameWindow():
win.blit(bg, (0,0)) # diplays the background image, coordinates of top left
win.blit(randletter, pygame.draw.rect(win, (255, 255, 255), (letters.x, letters.y, letters.width, letters.height))) # displays the letters going down the page (letters.x, letters.y, letters.width, letters.height)
# win.blit(randletter, pygame.draw.rect(win, (255, 255, 255), (letters.x, letters.y, letters.width, letters.height)))
letters.move(win) # calls upon the movement of the letters -- makes them move
# for letter in letters:
# letter.draw(win)
#pygame.draw.rect(win, (255,0,0), (16, 342, 228, 66), 2) # this shows the hitbox of the greenzone if needed
pygame.display.update() # idk what this does -- but it is needed
### mainloop
abc_list = [27,106,185] # this is the list of the starting positions of the letters (top left)
rand = random.choice(abc_list) # takes a random starting position of the letters
letters = player(rand,56,50,50,415) # instances the letters to start
#letters = [] # apparently creates a list of the the letters -- used to initialize and create new letters
target = greenzone(18,343,228,67) # sets the hitbox of the greenzone
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
# for letter in letters:
# if letter.x < 500 and letter.x > 0:
# letter.x += letter.vel
# else:
# letters.pop(letters.index(letter))
keys = pygame.key.get_pressed()
if keys[pygame.K_q] and randletter == qp:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_w] and randletter == wp:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_e] and randletter == ep:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_a] and randletter == ap:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_s] and randletter == sp:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_d] and randletter == dp:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_j] and randletter == jp:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_k] and randletter == kp:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
if keys[pygame.K_l] and randletter == lp:
if letters.y < target.hitbox[1] + target.hitbox[3]:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail')
redrawGameWindow()
pygame.quit()
我知道这是很多代码,我尝试添加尽可能多的注释,以帮助阅读本文的人理解我的心态。
对此的任何和所有帮助都会很棒。我知道我做错了很多——我几天前才开始这样做。感谢您的任何意见,非常感谢!!
最佳答案
根据您提供的其他信息:
My main/overall problem that I am facing is that once the letter doeshit the bottom, I want it to re-do the "iteration" of making a newrandom letter in a new column, and keep on repeating. Eventually, Iwant the program to only allow 5 "lives", and if they mess up morethan 5 times, they either get the option to restart or quit theprogram.
Along with that, my future aspirations for this code is to allow the user to select the speed and letter combinations that appear onthe screen. But I personally believe that I would be able to code thatonce I have the overall code repeat itself (like I just describedabove)
我必须将碰撞检测算法移出按键部分。我还清理了代码,将每个部分分成自己的函数。我对类还不太熟悉,所以我不知道用 target = greenzone(..) 做什么 - 这也可能是我大量使用 global 的原因
update()、init()、main() 都很简单。 dropLetter() 是您正在寻找的函数。在 updateInput() 中,randLetterIndex 用于更有效地检查按下的键是否是您需要的,我仍然对 Python 没有 switch() 语句感到震惊,所以这就是我想出的方法来解决这个问题。 checkCollision() - 我试图保留它,因为我不确定你将来对这个游戏的计划。我确实在 else 语句中添加了很多内容,包括将 quit() 语句移至此处
import os #
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (1335,330) # sets the coordinates so that the window pops up in the lower right side of the screen (1600x900 screen)
import pygame # pygame is the platform im using
import random # random needs to be imported for the random letters and columns
pygame.init() # initializes pygame
win = pygame.display.set_mode((260, 527)) # size of the window ,screenwidth = 260, screenheight = 527
pygame.display.set_caption("Daveypoo is King") # title of the window
clock = pygame.time.Clock() # used for frame rate
bg = pygame.image.load('phone3cropped.jpg') # background picture upload
qp = pygame.image.load('q.png') # uploads pictures of the letters (use paint, 50x50 pixels, black, 1 pixel outline, font = 36)
wp = pygame.image.load('w.png') # ^
ep = pygame.image.load('e.png') # ^
ap = pygame.image.load('a.png') # ^
sp = pygame.image.load('s.png') # ^
dp = pygame.image.load('d.png') # ^
jp = pygame.image.load('j.png') # ^
kp = pygame.image.load('k.png') # ^
lp = pygame.image.load('l.png') # ^
letterlist = [qp,wp,ep,ap,sp,dp,jp,kp,lp] # creates a list of letter images
pygameKeyList = [pygame.K_q, pygame.K_w, pygame.K_e, pygame.K_a, pygame.K_s, pygame.K_d, pygame.K_j, pygame.K_k, pygame.K_l]
random.seed() # seed the RNG
abc_list = [27,106,185] # this is the list of the starting positions of the letters (top left)
lives = 5 # 5 lives to start
userPressingKey = False # if user is currenlty pressing the key of the currently chosen letter
run = False
class player(): # creates letter instance
def __init__(self, x,y,width,height,end): # initializes the list
self.x = x
self.y = y
self.width = width
self.height = height
self.path = [self.y, 415] # creates the path that the letters will travel
self.vel = 1 # this is where the velocity of the letters is set
self.hitbox = (self.x, self.y, 50, 50) # creates the hitbox for the letter pictures (no need to change for now -- 50x50 is good)
def move(self, win): # creates the movement for the letters
if self.vel > 0:
if self.y + self.vel < self.path[1] : # if the bottom of the letter is less than the end, then:
self.y += self.vel # keep going
else: # if not:
self.vel = 0 # stop moving
# else: # if the bottom of the letter is at the end, then
# print('end')
# quit()
#redrawGameWindow()
# this is where the action to make a new letter has to go
class greenzone(object): # creates the zone (greenzone) where the player should get the letter in
def __init__(self,x,y,width,height): # initializes it
self.x = x
self.y = y
self.width = width
self.height = height
self.hitbox = (16, 342, 228, 66) # first two letters are top left (x,y) coordinate of the zone, next is width, next is height
# def hit(self):
# print('hit')
def redrawGameWindow():
win.blit(bg, (0,0)) # diplays the background image, coordinates of top left
win.blit(letterlist[randLetterIndex], pygame.draw.rect(win, (255, 255, 255), (letters.x, letters.y, letters.width, letters.height))) # displays the letters going down the page (letters.x, letters.y, letters.width, letters.height)
# win.blit(randletter, pygame.draw.rect(win, (255, 255, 255), (letters.x, letters.y, letters.width, letters.height)))
letters.move(win) # calls upon the movement of the letters -- makes them move
# for letter in letters:
# letter.draw(win)
#pygame.draw.rect(win, (255,0,0), (16, 342, 228, 66), 2) # this shows the hitbox of the greenzone if needed
pygame.display.update() # idk what this does -- but it is needed
def dropLetter():
#global randletter
global letters, rand, randLetterIndex
randLetterIndex = random.randrange(0,len(letterlist))
# randletter = letterlist[randLetterIndex] # randomizes the list of images^
rand = random.choice(abc_list) # takes a random starting position of the letters
letters = player(rand,56,50,50,415) # instances the letters to start
#letters = [] # apparently creates a list of the the letters -- used to initialize and create new letters
def checkCollision():
global run, lives
if letters.y < target.hitbox[1] + target.hitbox[3]:
if userPressingKey:
if letters.y + letters.height > target.hitbox[1]:
print('hit')
else:
print('fail')
else:
print('fail hard')
lives -= 1
print("lives:",lives)
if(lives <= 0):
print('end')
run = False
quit()
dropLetter()
def updateInput():
global userPressingKey
keys = pygame.key.get_pressed()
if(keys[pygameKeyList[randLetterIndex]]):
userPressingKey = True
else:
userPressingKey = False
# if keys[pygame.K_q] and randletter == qp:
# checkCollision()
# if keys[pygame.K_w] and randletter == wp:
# checkCollision()
# if keys[pygame.K_e] and randletter == ep:
# checkCollision()
# if keys[pygame.K_a] and randletter == ap:
# checkCollision()
# if keys[pygame.K_s] and randletter == sp:
# checkCollision()
# if keys[pygame.K_d] and randletter == dp:
# checkCollision()
# if keys[pygame.K_j] and randletter == jp:
# checkCollision()
# if keys[pygame.K_k] and randletter == kp:
# checkCollision()
# if keys[pygame.K_l] and randletter == lp:
# checkCollision()
# for letter in letters:
# if letter.x < 500 and letter.x > 0:
# letter.x += letter.vel
# else:
# letters.pop(letters.index(letter))
def update():
updateInput()
checkCollision()
def init():
dropLetter()
def main():
global run
init()
### mainloop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
exit()
update()
redrawGameWindow()
clock.tick(144)
target = greenzone(18,343,228,67) # sets the hitbox of the greenzone
main()
pygame.quit()
关于python - 有没有办法让我的 blit 在第一次迭代后重复自身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59224639/
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
在现代 IDE 中,有一个键盘快捷键可以通过键入文件名称来打开文件,而无需将手放在鼠标上。例如: Eclipse:Cmd|Ctrl + Shift + R -> 打开资源 IntelliJ:Cmd|C
有什么东西会等待事件发生(我正在等待的是 WebBrowser.DocumentCompleted),然后执行代码吗?像这样: If (WebBrowser.DocumentCompleted) 不会
我使用 PHP Minify,它很棒。但我的问题是,是否有任何 PHP 插件或其他东西可以自动检测 javascript/css 代码并自动缩小它?谢谢。 最佳答案 Javascript 压缩器? 看
有没有一种语言,类似什么CoffeeScript是JavaScript,编译成windows batch|cmd|command line的语言? 我指的cmd版本是基于NT的,尤其是XP sp3及以
我知道我可以 ,但是,我真的宁愿有一个任务,我可以从任何可以使用所有(或至少大部分)属性的操作系统调用 copy ,但这并没有消除 unix 上的权限。 我想知道是否已经有解决方案,或者我必须自己编
我正在使用 Vuejs(不使用 jQuery)开发一个项目,该项目需要像 jvectormap 这样的 map 但正如我所说,我没有使用 jQuery,那么是否有任何其他库可以在不使用 jQuery
想要进行一个简单的民意调查,甚至不需要基于 cookie,我不在乎投了多少票。有没有类似的插件或者简单的东西? 最佳答案 这是一个有用的教程 - 让我知道它是否适合您 using jQuery to
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
var FileBuff: TBytes; Pattern: TBytes; begin FileBuff := filetobytes(filename); Result := Co
我想要一个 vqmod xml 文件来添加一次上传多个图像的功能。身边有这样的事吗? 编辑:Opencart版本:2.1.0.1 最佳答案 最后我写了一个xml来添加到opencart 2.1.0.1
所以考虑这样的函数: public void setTemperature(double newTemperatureValue, TemperatureUnit unit) 其中Temperatur
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我是 ggplot2 的新手,一直在尝试找到一个全面的美学列表。我想我理解它们的目的,但很难知道哪些可以在各种情况下使用(主要是几何图形?)。 Hadley 的网站偶尔会在各个几何图形的页面上列出可用
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
是否有任何 PHP 函数可以将整数转换为十万和千万? 900800 -> 9,00,800 500800 -> 5,00,800 最佳答案 由于您已在问题标签中添加了 Yii,因此您可以按照 Yii
使用 Clojure 一段时间后,我积累了一些关于它的惰性的知识。我知道诸如map之类的常用API是否是惰性的。然而,当我开始使用一个不熟悉的API(例如with-open)时,我仍然感到怀疑。 是否
我的项目需要一个像 AvalonDock 这样的对接系统,但它的最后一次更新似乎是在 2013 年 6 月。是否有更多...积极开发的东西可以代替它? 最佳答案 AvalonDock 实际上相当成熟并
我正在寻找一个可以逆转 clojure 打嗝的函数 所以 turns into [:html] 等等 根据@kotarak的回答,这现在对我有用: (use 'net.cgrand.enliv
我是一名优秀的程序员,十分优秀!