我刚开始学习pygame和livewires,我正在尝试制作一款单人乒乓球游戏,你只要击球,球就会弹来跳去,直到它通过你的球拍(位于屏幕左侧,由鼠标控制),这让你输了。我有基本的代码,但是球不会停留在屏幕上,它只是闪烁而不是保持不变。而且,桨叶不随鼠标移动。我肯定我错过了一些简单的事情,但我就是想不起来。救命啊!以下是我所拥有的:
from livewires import games
import random
games.init(screen_width=640, screen_height=480, fps=50)
class Paddle(games.Sprite):
image=games.load_image("paddle.bmp")
def __init__(self, x=10):
super(Paddle, self).__init__(image=Paddle.image,
y=games.mouse.y,
left=10)
self.score=games.Text(value=0, size=25, top=5, right=games.screen.width - 10)
games.screen.add(self.score)
def update(self):
self.y=games.mouse.y
if self.top<0:
self.top=0
if self.bottom>games.screen.height:
self.bottom=games.screen.height
self.check_collide()
def check_collide(self):
for ball in self.overlapping_sprites:
self.score.value+=1
ball.handle_collide()
class Ball(games.Sprite):
image=games.load_image("ball.bmp")
speed=5
def __init__(self, x=90, y=90):
super(Ball, self).__init__(image=Ball.image,
x=x, y=y,
dx=Ball.speed, dy=Ball.speed)
def update(self):
if self.right>games.screen.width:
self.dx=-self.dx
if self.bottom>games.screen.height or self.top<0:
self.dy=-self.dy
if self.left<0:
self.end_game()
self.destroy()
def handle_collide(self):
self.dx=-self.dx
def end_game(self):
end_message=games.Message(value="Game Over",
size=90,
x=games.screen.width/2,
y=games.screen.height/2,
lifetime=250,
after_death=games.screen.quit)
games.screen.add(end_message)
def main():
background_image=games.load_image("background.bmp", transparent=False)
games.screen.background=background_image
paddle_image=games.load_image("paddle.bmp")
the_paddle=games.Sprite(image=paddle_image,
x=10,
y=games.mouse.y)
games.screen.add(the_paddle)
ball_image=games.load_image("ball.bmp")
the_ball=games.Sprite(image=ball_image,
x=630,
y=200,
dx=2,
dy=2)
games.screen.add(the_ball)
games.mouse.is_visible=False
games.screen.event_grab=True
games.screen.mainloop()
main()
你为什么要用电线?你只能在乒乓游戏中使用pygame。在
我帮不了你,因为你没有在这里发布完整的代码。至少,我看不到你在哪里更新精灵的位置(
self.x += self.dx
在什么地方?)并将绘图更新到屏幕上。你也没有在main()
函数中使用你的类。在也就是说,我看到了
在构造函数中,您从未使用过
x
变量。我也很担心。在考虑将“桨和球”类用作精灵,如下所示:
^{pr2}$注意,我冒昧地让你的代码读得更像Python。不过,我从未使用过livewires,因此我的代码可能无法正常工作。但它应该给你指明正确的方向。祝你好运!在
相关问题 更多 >
编程相关推荐