python初学者关于碰撞的问题
自学python三个月,想请教很初级的问题,不知道可以问谁。是《python编程:从基础到实践》第十三章的课后题。全部代码在下方,目的是想做一个水珠从屏幕上方任意位置落下...
自学python三个月,想请教很初级的问题,不知道可以问谁。是《python编程:从基础到实践》第十三章的课后题。
全部代码在下方,目的是想做一个水珠从屏幕上方任意位置落下,下面键盘控制的游戏角色左右移动接住后水珠消失的游戏。因为后续应该在水珠消失后再补充新的,所以水珠建立了组,我使用了spritecollide来检查碰撞( def catch_water(doll,waters))。但是发现如果水珠本来就能掉落在角色上时,水珠会自动消失。如果是用键盘移动角色才接住水珠,那水珠不会消失,继续下落。也就是说检查碰撞时,好像并没有更新精灵,想知道怎么解决这个问题呀。
开始接触代码以后深感智商不够用,不知道有没有讲的很啰嗦,总之谢谢各位啦。
pygame.init()
bg_color=(20,40,50)
screen=pygame.display.set_mode((1200,800))
random_number=randint(1,1200)
dropspeed=3
speed_factor=5
class Doll(Sprite):
def __init__(self, screen):
super(Doll, self).__init__()
self.screen = screen
self.image=pygame.image.load('timg.bmp')
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
self.moving_right=False
self.moving_left=False
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
if self.moving_right and self.rect.right<self.screen_rect.right:
self.rect.centerx+=speed_factor
if self.moving_left and self.rect.left>0:
self.rect.centerx-=speed_factor
class Water(Sprite):
def __init__(self,screen,random_number):
super(Water, self).__init__()
self.screen=screen
self.image=pygame.image.load("raindrop.bmp")
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
self.rect.centerx=random_number
self.rect.top=0
self.y=float(self.rect.y)
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
self.y+=dropspeed
self.rect.y=self.y
def check_keydown_events(event,doll):
if event.key==pygame.K_RIGHT:
doll.moving_right=True
elif event.key==pygame.K_LEFT:
doll.moving_left=True
def check_keyup_events(event,doll):
if event.key==pygame.K_RIGHT:
doll.moving_right=False
elif event.key==pygame.K_LEFT:
doll.moving_left=False
def event_check(doll):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
check_keydown_events(event,doll)
elif event.type==pygame.KEYUP:
check_keyup_events(event,doll)
def catch_water(doll,waters):
water=Water(screen,random_number)
doll=Doll(screen)
catch=pygame.sprite.spritecollide(doll,waters,True)
def rungame():
water=Water(screen,random_number)
waters=Group()
waters.add(water)
doll=Doll(screen)
while True:
screen.fill(bg_color)
doll.blitme()
waters.draw(screen)
pygame.display.flip()
event_check(doll)
doll.update()
waters.update()
catch_water(doll,waters)
rungame() 展开
全部代码在下方,目的是想做一个水珠从屏幕上方任意位置落下,下面键盘控制的游戏角色左右移动接住后水珠消失的游戏。因为后续应该在水珠消失后再补充新的,所以水珠建立了组,我使用了spritecollide来检查碰撞( def catch_water(doll,waters))。但是发现如果水珠本来就能掉落在角色上时,水珠会自动消失。如果是用键盘移动角色才接住水珠,那水珠不会消失,继续下落。也就是说检查碰撞时,好像并没有更新精灵,想知道怎么解决这个问题呀。
开始接触代码以后深感智商不够用,不知道有没有讲的很啰嗦,总之谢谢各位啦。
pygame.init()
bg_color=(20,40,50)
screen=pygame.display.set_mode((1200,800))
random_number=randint(1,1200)
dropspeed=3
speed_factor=5
class Doll(Sprite):
def __init__(self, screen):
super(Doll, self).__init__()
self.screen = screen
self.image=pygame.image.load('timg.bmp')
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
self.moving_right=False
self.moving_left=False
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
if self.moving_right and self.rect.right<self.screen_rect.right:
self.rect.centerx+=speed_factor
if self.moving_left and self.rect.left>0:
self.rect.centerx-=speed_factor
class Water(Sprite):
def __init__(self,screen,random_number):
super(Water, self).__init__()
self.screen=screen
self.image=pygame.image.load("raindrop.bmp")
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
self.rect.centerx=random_number
self.rect.top=0
self.y=float(self.rect.y)
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
self.y+=dropspeed
self.rect.y=self.y
def check_keydown_events(event,doll):
if event.key==pygame.K_RIGHT:
doll.moving_right=True
elif event.key==pygame.K_LEFT:
doll.moving_left=True
def check_keyup_events(event,doll):
if event.key==pygame.K_RIGHT:
doll.moving_right=False
elif event.key==pygame.K_LEFT:
doll.moving_left=False
def event_check(doll):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
check_keydown_events(event,doll)
elif event.type==pygame.KEYUP:
check_keyup_events(event,doll)
def catch_water(doll,waters):
water=Water(screen,random_number)
doll=Doll(screen)
catch=pygame.sprite.spritecollide(doll,waters,True)
def rungame():
water=Water(screen,random_number)
waters=Group()
waters.add(water)
doll=Doll(screen)
while True:
screen.fill(bg_color)
doll.blitme()
waters.draw(screen)
pygame.display.flip()
event_check(doll)
doll.update()
waters.update()
catch_water(doll,waters)
rungame() 展开
1个回答
展开全部
#你的catch_water没用啊
#稍微改了一下 不知道是不是你要的效果
import pygame.sprite
import random
import sys
class Doll(pygame.sprite.Sprite):
speed_factor = 1
def __init__(self, screen):
super(Doll, self).__init__()
self.screen = screen
self.image=pygame.image.load("hand.png")
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
self.rect.centerx=self.screen_rect.centerx
self.rect.bottom=self.screen_rect.bottom
self.moving_right=False
self.moving_left=False
def blit_me(self):
self.screen.blit(self.image,self.rect)
def update(self):
if self.moving_right and self.rect.right<self.screen_rect.right:
self.rect.centerx+=Doll.speed_factor
if self.moving_left and self.rect.left>0:
self.rect.centerx-=Doll.speed_factor
class Water(pygame.sprite.Sprite):
dropspeed=0.1
def __init__(self,screen,random_number):
super(Water, self).__init__()
self.screen=screen
self.image=pygame.image.load("drop.png")
self.rect=self.image.get_rect()
self.screen_rect=self.screen.get_rect()
self.rect.centerx=random_number
self.rect.top=0
self.y=float(self.rect.y)
def blit_me(self):
self.screen.blit(self.image,self.rect)
def update(self):
self.y+=Water.dropspeed
self.rect.y=self.y
def check_key_down_events(event,doll):
if event.key==pygame.K_RIGHT:
doll.moving_right=True
elif event.key==pygame.K_LEFT:
doll.moving_left=True
def check_key_up_events(event,doll):
if event.key==pygame.K_RIGHT:
doll.moving_right=False
elif event.key==pygame.K_LEFT:
doll.moving_left=False
def event_check(doll):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
check_key_down_events(event,doll)
elif event.type==pygame.KEYUP:
check_key_up_events(event,doll)
def run_game():
pygame.init()
bg_color = (20, 40, 50)
screen = pygame.display.set_mode((1000, 500))
random_number = random.randint(1, 1000)
water=Water(screen,random_number)
doll=Doll(screen)
while True:
screen.fill(bg_color)
doll.blit_me()
water.blit_me()
pygame.display.flip()
event_check(doll)
doll.update()
water.update()
if pygame.sprite.collide_rect(doll,water) or water.y>500:
random_number = random.randint(1, 1000)
water = Water(screen, random_number)
run_game()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询