【发布时间】:2019-04-22 22:09:51
【问题描述】:
您好,我有一个游戏代码,其中有多个水果从天而降,底部的青蛙必须尝试抓住它们。当他接住一个时,分数就会上升。这只发生在青蛙与一些水果而不是全部碰撞时。并且在某一点之后,分数随机开始无缘无故地增加。这是大部分代码,因为我不确定错误在哪里:
import pygame, sys, time, random
from pygame.locals import *
from math import fabs
######### constants ##########
jumpvel=20
fallingspeed=1
running= True
blue= [129,183 ,253]
pink=[255,174,201]
textcolour= [255,255,255]
x=700//2
y=1000//2
score=0
thingylist= ['fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','naughty1.bmp','naughty2.bmp','naughty3.bmp',]
all_things=[]
for i in range (12):
new_thing_image=pygame.image.load(thingylist[(random.randrange(0,12))])
new_thing_image.set_colorkey(pink)
new_thing_rect=new_thing_image.get_rect()
new_thing_rect.x=random.randrange(0,950)
new_thing_rect.y=-random.randrange(50,500)
all_things.append([new_thing_image,new_thing_rect])
def checkCollision (frog_rect,all_things,score):
collides_with=None
for thing_image, thing_rect in all_things:
if frog_rect.colliderect(thing_rect):
collides_with=True
if collides_with == True:
score= score+100
return collides_with,score
######## initialising screen#########
pygame.init()
gamedisplay=pygame.display.set_mode((1000,600)) #making the screen
pygame.display.set_caption('frog')
clock=pygame.time.Clock()# frames per second
bg=pygame.image.load('actual clouds.bmp').convert()
############ initialising sprites##############
frog= pygame.image.load('actual frog.bmp')
frog.set_colorkey(blue)
frog_rect=frog.get_rect()
frog_rect.centerx=(x)
frog_rect.centery=(y)
##########drawing things#############
def drawThings (all_things):
for item in all_things:
new_thing_image, new_thing_rect= item
gamedisplay.blit(new_thing_image, (new_thing_rect.x, new_thing_rect.y))
#########update display function###########
def update(x,y,all_things,score):
gamedisplay.blit(bg,[0,0])
gamedisplay.blit(frog,(x,y))
for thing in range (len(all_things)):
new_thing_rect=all_things[i][1]
#thing_rect.y=thing_rect.y+fallingspeed
new_thing_rect.y+= fallingspeed
drawThings(all_things)
label=font.render("score "+ str(score) ,1,textcolour)
gamedisplay.blit(label,(750,10))
gamedisplay.blit(heart1,(750,50))
gamedisplay.blit(heart2,(850,50))
gamedisplay.blit(heart2,(800,50))
pygame.display.update()
pygame.time.delay(50)
while running == True:
gamedisplay.blit(bg,[0,0])
gamedisplay.blit(frog,(x,y))
drawThings(all_things)
label=font.render("score "+ str(score) ,1,textcolour)
gamedisplay.blit(label,(750,10))
pygame.display.flip()
pygame.event.pump()
key=pygame.key.get_pressed()
for item in all_things:
new_thing_image, new_thing_rect= item
new_thing_rect.y+= fallingspeed
if new_thing_rect.y >450:
new_thing_rect.x=random.randrange(0,950)
new_thing_rect.y=-random.randrange(50,500)
############collision detection##########
detect,score =checkCollision (frog_rect, all_things,score)
update(x,y,all_things,score)
每次它与任何掉落的水果碰撞时,分数都应该增加,而不仅仅是某些水果,而不是开始随机不断地增加。任何帮助将不胜感激谢谢!
【问题讨论】:
-
frog_rect在哪里更新?青蛙会动吗?碰撞功能看起来不错(除非您只为可能的多次碰撞得分一次)。 -
@Kingsley the frog 正在更新功能中进行更新:gamedisplay.blit(frog,(x,y)) 但我不确定 frog_rect。
-
查看这个可爱的debug 博客寻求帮助。
-
gamedisplay.blit(frog,(x,y))只是将青蛙位图绘制到屏幕(x, y)。frog_rect需要根据青蛙坐标的任何变化进行更新,以使碰撞起作用。这发生了吗?我本来希望青蛙被画成这样:gamedisplay.blit( frog, ( frog_rect.centerx, frog_rect.centery ) ),因为(中心)坐标在frog_rect的初始化中是这样设置的。 -
不,它不能那样工作。您需要在
x和y更改时更新frog_rect,或者仅使用frog_rect保持精灵的位置。问题的核心是您使用一组坐标作为精灵在屏幕上的位置,另一组坐标用于碰撞矩形(除了最初之外,它永远不会更新)。
标签: python pygame collision-detection