我正在创建一个我使用多个文件的游戏,下面的代码是我有精灵的文件,2个类Player()
代表一个水坑,和一个球Ball()
所以我想以某种方式(我不知道可能从player类继承,但不能用它做任何事情)控制我在Player()
中创建的水坑的边缘,这样当球在Player()
的水坑边缘之间时,当球:>;=560像素时,它就会反弹
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((130,20))
self.image.fill(black)
self.rect = self.image.get_rect()
self.rect.centerx = width/2
self.rect.y = 560
def update(self):
self.vx = 0
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.vx = -15
if keys[pg.K_RIGHT]:
self.vx = 15
if self.rect.left <= 0:
self.rect.left = 0
if self.rect.right >= width:
self.rect.right = width
self.rect.x += self.vx
class Ball(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((10,10))
self.image.fill(black)
self.rect = self.image.get_rect()
self.rect.centerx = width / 2
self.rect.centery = height / 2
self.vx = -4
self.vy = 10
def update(self):
if self.rect.y >= 560:# and... how do i declare the puddle here???
self.vy = -self.vy
if self.rect.top <= 0:
self.vy = -self.vy
if self.rect.right <= 0:
self.vx = -self.vx
if self.rect.left >= width:
self.vx = -self.vx
self.rect.x += self.vx
self.rect.y += self.vy
你可以用桨。矩形。碰撞矩形(球.rect)测试球和桨之间的碰撞。不过,之后你需要将球移出碰撞区域。你知道吗
相关问题 更多 >
编程相关推荐