Python直升机游戏不更新分数
作为Pygame学习课程的一部分,我必须创建简单的直升机游戏,类似于flappy bird。 它也应该有一个分数计数-所以每次你用直升机通过两个街区之间的差距时,分数应该加1。 游戏逻辑和其他一切都很好,除了每次直升机通过积木时,分数都没有增加,所以所有时间分数都显示为0。 下面是代码,有人能帮忙吗Python直升机游戏不更新分数,python,function,pygame,Python,Function,Pygame,作为Pygame学习课程的一部分,我必须创建简单的直升机游戏,类似于flappy bird。 它也应该有一个分数计数-所以每次你用直升机通过两个街区之间的差距时,分数应该加1。 游戏逻辑和其他一切都很好,除了每次直升机通过积木时,分数都没有增加,所以所有时间分数都显示为0。 下面是代码,有人能帮忙吗 import pygame import time from random import randint black = (0,0,0) white = (255,255,255) pygame
import pygame
import time
from random import randint
black = (0,0,0)
white = (255,255,255)
pygame.init()
surfaceWidth = 1280
surfaceHeight = 720
imageHeight = 30
imageWidth = 60
surface = pygame.display.set_mode((surfaceWidth,surfaceHeight))
pygame.display.set_caption('EVGENY GAME')
#clock = frames per second
clock = pygame.time.Clock()
img = pygame.image.load('Helicopter.jpg')
def score(count):
font = pygame.font.Font('freesansbold.ttf', 20)
text = font.render("Score: "+str(count), True, white)
surface.blit(text, [0,0])
def blocks(x_block, y_block, block_width, block_height, gap):
pygame.draw.rect(surface, white, [x_block, y_block, block_width, block_height])
pygame.draw.rect(surface, white, [x_block, y_block+block_height+gap, block_width, surfaceHeight])
def replay_or_quit():
for event in pygame.event.get([pygame.KEYDOWN, pygame.KEYUP, pygame.QUIT]):
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
continue
return event.key
return None
def makeTextObjs(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def msgSurface(text):
smallText = pygame.font.Font('freesansbold.ttf', 20)
largeText = pygame.font.Font('freesansbold.ttf', 150)
titleTextSurf, titleTextRect = makeTextObjs(text, largeText)
titleTextRect.center = surfaceWidth / 2, surfaceHeight / 2
surface.blit(titleTextSurf, titleTextRect)
typTextSurf, typTextRect = makeTextObjs('Press any key to continue', smallText)
typTextRect.center = surfaceWidth / 2, ((surfaceHeight / 2) + 100)
surface.blit(typTextSurf, typTextRect)
pygame.display.update()
time.sleep(1)
while replay_or_quit() == None:
clock.tick()
main()
def gameover():
msgSurface('GAME OVER')
def helicopter(x, y, image):
surface.blit(img, (x,y,))
# first constance = game over, while not true we are playing game
def main():
x = 50
y = 100
y_move = 0
x_block = surfaceWidth
y_block = 0
block_width = 100
block_height = randint(0, surfaceHeight/2)
gap = imageHeight * 8
block_move = 3
current_score = 0
game_over = False
while not game_over:#this will keep running until it hits pygame.quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_move = -10
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_move = 5
y += y_move
surface.fill(black)
helicopter(x,y,img)
score(current_score)
blocks(x_block, y_block, block_width, block_height, gap)
x_block -= block_move
if y > surfaceHeight-40 or y < 0:
gameover()
if x_block < (-1*block_width):
x_block = surfaceWidth
block_height = randint(0, surfaceHeight/2)
if x + imageWidth > x_block:
if x < x_block + block_width:
if y < block_height:
if x - imageWidth < block_width + x_block:
gameover()
if x + imageWidth > x_block:
if y + imageHeight > block_height+gap:
gameover()
if x < x_block and x > x_block-block_move:
current_score += 1
pygame.display.update()#update, updates specific area on display, flip - updates whole display
clock.tick(100)#100 frames per second
main()
pygame.quit()
quit() enter code here
导入pygame
导入时间
从随机导入randint
黑色=(0,0,0)
白色=(255255)
pygame.init()
表面宽度=1280
表面高度=720
图像高度=30
图像宽度=60
surface=pygame.display.set_模式((表面宽度,表面高度))
pygame.display.set_标题('EVGENY GAME')
#时钟=每秒帧数
clock=pygame.time.clock()
img=pygame.image.load('helposer.jpg'))
def分数(计数):
font=pygame.font.font('freesansbold.ttf',20)
text=font.render(“分数:+str(计数),真,白色)
blit(文本,[0,0])
def块(x块、y块、块宽度、块高度、间隙):
pygame.draw.rect(表面,白色,[x\u块,y\u块,块宽度,块高度])
pygame.draw.rect(表面,白色,[x_块,y_块+块高度+间隙,块宽度,表面高度])
def RELAY_或_quit():
对于pygame.event.get([pygame.KEYDOWN,pygame.keydup,pygame.QUIT])中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
elif event.type==pygame.KEYDOWN:
持续
返回事件.key
一无所获
def makeTextObjs(文本、字体):
textSurface=font.render(文本、真、白)
返回textSurface,textSurface.get_rect()
def msgSurface(文本):
smallText=pygame.font.font('freesansbold.ttf',20)
largeText=pygame.font.font('freesansbold.ttf',150)
titleTextSurf,titleTextRect=makeTextObjs(text,largeText)
titleTextRect.center=表面宽度/2,表面高度/2
表面。blit(titleTextSurf,titleTextRect)
typTextSurf,typTextRect=makeTextObjs('按任意键继续',smallText)
typTextRect.center=表面宽度/2,((表面高度/2)+100)
曲面.blit(typTextSurf,typTextRect)
pygame.display.update()
时间。睡眠(1)
重播或退出时==无:
时钟滴答作响
main()
def gameover():
msgSurface(“游戏结束”)
def直升机(x、y、图像):
表面光点(img,(x,y,))
#first constance=游戏结束,虽然不是真的,但我们正在玩游戏
def main():
x=50
y=100
y_move=0
x_块=表面宽度
y_块=0
块宽度=100
块体高度=randint(0,表面高度/2)
间隙=图像高度*8
块移动=3
当前分数=0
游戏结束=错误
在游戏未结束时:#这将一直运行,直到它击中pygame.quit
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
游戏结束=正确
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_UP:
y_move=-10
如果event.type==pygame.KEYUP:
如果event.key==pygame.K_UP:
y_move=5
y+=y_移动
表面填充(黑色)
直升机(x、y、img)
分数(当前的大学分数)
块(x块、y块、块宽度、块高度、间隙)
x_块-=块移动
如果y>表面高度-40或y<0:
gameover()
如果x_块<(-1*块宽度):
x_块=表面宽度
块体高度=randint(0,表面高度/2)
如果x+imageWidth>x_块:
如果xx_块:
如果y+imageHeight>block_高度+间隙:
gameover()
如果xx_块-块移动:
当前_分数+=1
pygame.display.update()#更新,更新显示器上的特定区域,翻转-更新整个显示器
时钟。滴答(100)#每秒100帧
main()
pygame.quit()
quit()在此处输入代码
您的分数计数器从未更新,因为您的分数计数器条件从未达到。改变
if x < x_block and x > x_block-block_move:
你好欢迎来到StackOverflow!我建议您也提供代码中引用的图像,以便可以运行它,更好地查看错误所在。在运行代码时,我注意到,如果x
:
从未执行。只要在上面印个字,然后看电视就行了。同时,打印当前的_分数也会显示相同的结果:您永远不会增加分数。
if x_block >= x > x_block-block_move: