Python pygame相关代码中的2个函数不工作。(可能是语法)
我写了一个代码来制作一个游戏,玩家在游戏中上下移动,必须躲避火焰。但是waitforkey()和drawtext()函数根本不起作用。 所以当游戏结束时,代码会给出一个错误Python pygame相关代码中的2个函数不工作。(可能是语法),python,python-3.x,pygame,Python,Python 3.x,Pygame,我写了一个代码来制作一个游戏,玩家在游戏中上下移动,必须躲避火焰。但是waitforkey()和drawtext()函数根本不起作用。 所以当游戏结束时,代码会给出一个错误 if event.type == "QUIT": 代码如下: #Import modules import pygame, random, sys from pygame.locals import * #intialising variables for ease wind
if event.type == "QUIT":
代码如下:
#Import modules
import pygame, random, sys
from pygame.locals import *
#intialising variables for ease
window_height=600
window_width=600
blue = (0,0,255)
black = (0,0,0)
fps=30
flamesize=20
speed = 5
addnewflamerate =20
#defining the required function
def terminate(): #to end the program
pygame.quit()
sys.exit()
def waitforkey(): #to wait for user to start
for event in pygame.event.get():
if event.type == "QUIT":
terminate()
if event.type == "KEYDOWN": #to terminate if the user presses the escape key
if event.key == "K_ESCAPE":
terminate()
return
def flamehitsmario(playerrect, flames): #to check if flame has hit mario or not
for f in flames:
if playerrect.colliderect(f['rect']):
return True
return False
def drawtext(text, WindowSurface, font, x, y): #to display text on the screen
textobj = font.render(text, 1, blue)
textrect = textobj.get_rect()
textrect.topleft = (x,y)
WindowSurface.blit(textobj, textrect)
#end of functions, begin to start the main code
pygame.init()
mainClock = pygame.time.Clock()
WindowSurface = pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption('MARIO')
#setting up font and sounds and images
font = pygame.font.SysFont(None, 48)
playerimage = pygame.image.load('player.png')
playerrect = playerimage.get_rect()
flameimage = pygame.image.load('flame.png')
#getting to the start screen
#waitforkey() : if i remove the '#', the game hangs.
drawtext("Mario", WindowSurface, font, (window_width/3), (window_height/3))
drawtext("Press any key to start", WindowSurface, font, (window_width/3-50), (window_height/3+50))
pygame.display.update()
#start for the main code
topscore = 0
while True:
score = 0
flames = []
playerrect.topleft = (50,window_height/2)
moveup = movedown = False
flameaddcounter = 0
while True: #the main game loop
score+=1
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_UP:
movedown = False
moveup = True
if event.key == K_DOWN:
movedown = True
moveup = False
if event.type == KEYUP:
if event.key == K_UP:
moveup = False
if event.key == K_DOWN:
movedown = False
if event.key == K_ESCAPE:
terminate()
flameaddcounter += 1
if flameaddcounter == addnewflamerate:
flameaddcounter = 0
newflame = {'rect' : pygame.Rect(window_width - flamesize,random.randint(0, window_height - flamesize), flamesize, flamesize),
'speed' : speed,
'Surface' : pygame.transform.scale(flameimage, (flamesize,flamesize))
}
flames.append(newflame)
if (moveup and (playerrect.top > 0)):
playerrect.top -= speed
if (movedown and (playerrect.bottom < window_height)):
playerrect.bottom += speed
for f in flames:
f['rect'].left -= speed
for f in flames:
if f['rect'].left <= 0:
flames.remove(f)
WindowSurface.fill(black)
WindowSurface.blit(playerimage, playerrect)
for f in flames:
WindowSurface.blit(f['Surface'], f['rect'])
pygame.display.update()
if flamehitsmario(playerrect, flames):
if score > topscore:
topscore = score
drawtext('GAME OVER', font, WindowSurface, (window_width / 3), (window_height / 3))
drawtext('Press a key to play again.', font, WindowSurface, (window_width / 3) - 80, (window_height / 3) + 50)
pygame.display.update()
waitforkey()
mainClock.tick(fps)
if event.type == "QUIT":
#导入模块
导入pygame、random、sys
从pygame.locals导入*
#初始化变量以方便
窗户高度=600
窗宽=600
蓝色=(0,0255)
黑色=(0,0,0)
fps=30
火焰尺寸=20
速度=5
addnewflamerate=20
#定义所需的功能
def terminate():#结束程序
pygame.quit()
sys.exit()
def waitforkey():#等待用户启动
对于pygame.event.get()中的事件:
如果event.type==“退出”:
终止()
如果event.type==“KEYDOWN”:#如果用户按下退出键,则终止
如果event.key==“K_ESCAPE”:
终止()
回来
def flamehitsmario(playerrect,火焰):#检查火焰是否击中马里奥
对于火焰中的f:
如果playerrect.colliderect(f['rect']):
返回真值
返回错误
def drawtext(文本、窗口表面、字体、x、y):#在屏幕上显示文本
textobj=font.render(文本,1,蓝色)
textrect=textobj.get_rect()
textrect.topleft=(x,y)
WindowSurface.blit(textobj、textrect)
#函数结束后,开始启动主代码
pygame.init()
mainClock=pygame.time.Clock()
WindowSurface=pygame.display.set_模式((窗口宽度、窗口高度))
pygame.display.set_标题('MARIO'))
#设置字体、声音和图像
font=pygame.font.SysFont(无,48)
playerimage=pygame.image.load('player.png')
playerrect=playerimage.get_rect()
flameimage=pygame.image.load('flame.png')
#进入开始屏幕
#waitforkey():如果删除“#”,游戏将挂起。
drawtext(“Mario”,窗口表面,字体,(窗口宽度/3),(窗口高度/3))
drawtext(“按任意键开始”,窗口表面,字体,(窗口宽度/3-50),(窗口高度/3+50))
pygame.display.update()
#启动主代码
上核=0
尽管如此:
分数=0
火焰=[]
playerrect.topleft=(50,窗高/2)
moveup=movedown=False
flameaddcounter=0
虽然正确:#主游戏循环
分数+=1
对于pygame.event.get()中的事件:
如果event.type==退出:
终止()
如果event.type==KEYDOWN:
如果event.key==K_UP:
向下移动=错误
向上移动=真
如果event.key==K_DOWN:
向下移动=真
moveup=False
如果event.type==KEYUP:
如果event.key==K_UP:
moveup=False
如果event.key==K_DOWN:
向下移动=错误
如果event.key==K_转义:
终止()
flameaddcounter+=1
如果flameaddcounter==addnewflamerate:
flameaddcounter=0
newflame={'rect':pygame.rect(窗口宽度-火焰大小,随机.randint(0,窗口高度-火焰大小),火焰大小,火焰大小),
“速度”:速度,
“表面”:pygame.transform.scale(flameimage,(flamesize,flamesize))
}
火焰。附加(新火焰)
如果(向上移动和(playerrect.top>0)):
playerrect.top-=速度
如果(向下移动和(播放区域底部<窗口高度)):
playerrect.bottom+=速度
对于火焰中的f:
f['rect'].左-=速度
对于火焰中的f:
如果f['rect'],左上核:
上核=得分
drawtext('游戏结束',字体,窗口表面,(窗口宽度/3),(窗口高度/3))
drawtext('按键可再次播放'),字体,窗口表面,(窗口宽度/3)-80,(窗口高度/3)+50)
pygame.display.update()
waitforkey()
主时钟滴答声(fps)
以及所需的图像:
if event.type == "QUIT":
if event.type == "QUIT":
谢谢:D
if event.type == "QUIT":
您将看到event.type
是否等于字符串“QUIT”。不会的。其他任何事件也不等于字符串
if event.type == "QUIT":
pygame定义了一些常量(int
s),您应该根据这些常量进行测试
if event.type == "QUIT":
if event.type == pygame.QUIT:
#etc
同样,对于其他
事件,键入检查。我可以看到您切换传递给函数的参数的顺序
if event.type == "QUIT":
drawtext
。在游戏循环中,使用以下参数调用函数
if event.type == "QUIT":
drawtext('GAME OVER', font, WindowSurface, (window_width / 3), (window_height / 3))
但您可以这样定义函数:
if event.type == "QUIT":
def drawtext(text, WindowSurface, font, x, y):
正如您所看到的,font参数是在WindowsSurface参数的位置传递的,因此您基本上是在尝试从曲面而不是字体进行渲染:)。
只需修改将参数传递给函数的顺序,它就可以顺利工作:)需要查看的代码很多,但看起来“游戏结束”后,您想等待用户按键吗?您的waitforkey
函数的问题是,它只会调用pygame.event.get
一次,因此如果在调用waitforkey
时没有事件排队,它将不会做任何事情。如果这听起来像你的问题,让我知道,我会用代码格式化一个实际的答案。是的,当我在游戏循环之前调用waitforkey函数时,它什么都不做。显示文本,但未检测到按键。您收到的错误消息是什么
if event.type == "QUIT":