如果对pygames有基本了解的任何人都可以帮助我解决我当前面临的问题,那将是很好的。对于绝对的神灵来说,这应该不太困难。
我现在的代码遇到了麻烦。
我正在尝试制作一个简单的游戏,程序会显示一个随机字母,用户必须输入该字母才能到达页面底部。
我有在随机列中显示随机字母的代码,但是,一旦字母到达底部,我只会让程序打印“ end”,然后退出程序。
我不理解如何进一步开发代码。这是现在的代码:
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()
我知道这是很多代码,我尝试在其中添加尽可能多的注释,以帮助阅读此书的任何人理解我的想法。
任何与此相关的帮助都将非常有用。我知道我做错了很多-我才几天前才开始。感谢您的输入,非常感谢!
根据您提供的其他信息:
我面临的主要/整体问题是,触底,我希望它重新做一个新的“迭代”在新列中随机输入字母,并继续重复。最终,我希望该程序只允许5次“生命”,如果它们使更多人陷入困境超过5次,他们可以选择重新启动或退出程序。
此外,我将来对这段代码的期望是允许用户选择出现在上面的速度和字母组合屏幕。但我个人认为我可以编写代码一旦我使整体代码重复一遍(就像我刚才描述的那样)以上)
我不得不将碰撞检测算法移出按键部分。我还清理了代码,将每个部分分成了自己的功能。我对类还不太熟悉,所以我不知道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()