嗨,我有一个游戏代码,其中有许多水果从天上掉下来,底部的青蛙必须设法抓住它们。当他抓到一个分数时,分数上升。这仅在青蛙与某些水果而不是所有水果碰撞时才发生。分数在某个点之后无缘无故地随机开始无休止地增加。这是大多数代码,因为我不确定错误将在哪里:
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)
分数每次与任何下降的果粒发生碰撞时都应该增加,而不仅仅是某些分数,而不仅仅是开始不停地随机增加。任何帮助将不胜感激谢谢你!
答案 0 :(得分:0)
基于代码段-该代码段不包含青蛙位置更新代码,因此我认为存在以下问题:
这是因为碰撞矩形只为青蛙的起始位置定义了一次,但从未随青蛙的位置变化而更新。
############ initialising sprites##############
frog= pygame.image.load('actual frog.bmp')
frog.set_colorkey(blue)
frog_rect=frog.get_rect()
frog_rect.centerx=(x) # <-- rect only ever updated here!
frog_rect.centery=(y)
这将导致所描述的症状,因为通过碰撞矩形掉落的得分对象(水果?)会增加得分(看似随机),并且当青蛙仍在其原始位置上方时,碰撞会完美地起作用。如果青蛙部分在直肠上方,它将有些起作用,并且一旦青蛙移开起始位置直肠就完全不起作用。
该问题的解决方案是每当更新青蛙位置的frog_rect
和x
时更新矩形y
的坐标。这可以通过在frog_rect.centerx
函数中设置frog_rect.centery
和update()
来实现:
def update(x, y, all_things, score ):
frog_rect.x = x
frog_rect.y = y
使用.centerx
,.centery
坐标初始化青蛙的rect,但是在x,y
绘制青蛙,因此碰撞rect也开始偏离中心。因此,最好也更新初始化函数:
frog_rect=frog.get_rect()
frog_rect.x = x
frog_rect.y = y