首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么这段代码只注册某些冲突,而不注册所有冲突?

为什么这段代码只注册某些冲突,而不注册所有冲突?
EN

Stack Overflow用户
提问于 2019-04-23 06:09:52
回答 1查看 39关注 0票数 0

嗨,我有一个游戏的代码,其中有多个水果从天而降,在底部的青蛙必须试图抓住他们。当他接住一个球时,分数就上升了。这只发生在青蛙与一些水果而不是所有水果相撞的时候。在达到某一点后,分数开始随机增加,而且没有任何原因。下面是大部分代码,因为我不确定错误在哪里:

代码语言:javascript
运行
复制
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)

每次它撞到任何掉下来的小坚果时,分数都应该增加,而不仅仅是某些特定的小坚果,也不应该只是开始不停的随机增加。任何帮助都将不胜感激,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2019-04-24 06:45:25

基于代码片段-其中不包括青蛙位置更新代码,我猜测以下问题:

随机得分increasing

  • Collisions

不总是有效

这是由于碰撞矩形为青蛙的起始位置定义了一次,但之后永远不会随着青蛙位置的变化而更新。

代码语言:javascript
运行
复制
############ 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)

这将导致所描述的症状,因为计分对象(水果?)穿过碰撞矩形会增加分数(看起来是随机的),当青蛙还在它的原始位置时,碰撞会完美地工作。如果青蛙的部分位置在rect上,它就会起作用,而一旦青蛙离开了起始位置rect,它就完全不起作用了。

这个问题的解决方案是,每当青蛙位置的x & y更新时,就更新矩形frog_rect的坐标。这可以通过在update()函数中设置frog_rect.centerxfrog_rect.centery来实现:

代码语言:javascript
运行
复制
def update(x, y, all_things, score ):
    frog_rect.x = x
    frog_rect.y = y

青蛙的矩形是用.centerx.centery坐标初始化的,但是青蛙是在x,y绘制的,所以碰撞矩形也开始有点偏离中心。因此,最好也更新初始化函数:

代码语言:javascript
运行
复制
frog_rect=frog.get_rect()
frog_rect.x = x
frog_rect.y = y
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55801747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档