嘿,我正在做一个家庭作业,我必须通过 pygame 和 livewire 创建一个单人乒乓球游戏。我的代码大部分都完成了,但我确实有一个问题。屏幕不会更新,因此球拍不会移动,球也不会反弹。我在桨和球上都有一个更新方法,但由于某种原因它不起作用。这是我的代码谢谢!
更新:我不得不重做一些事情,但我现在可以调用我的课程并且球会反弹,但是我无法移动桨。我不知道为什么这不起作用,因为 self.y = games.mouse.y 应该更新我的桨的 y 坐标。然而,这是我重新编辑的代码,感谢到目前为止的帮助!
from livewires import games, color
import random
#make screen size
games.init(screen_width = 640, screen_height = 480 , fps = 50)
class Paddle(games.Sprite):
image = games.load_image("paddle.bmp")
def __init__(self):
super(Paddle, self).__init__(image = Paddle.image,x = 10, y = games.mouse.y)
self.score = games.Text(value = 0, size = 25, color = color.black,
top = 20, right = games.screen.width - 8)
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_hit()
def check_hit(self):
countdown = 0
if countdown == 0:
for ball in self.overlapping_sprites:
self.score.value += 1
ball.dat_touch()
countdown == 10
else: countdown -= 1
"""
class Ball(games.Sprite):
image = games.load_image("ball.bmp")
speed = 2
def __init__(self, x = games.screen.height/2, y = games.screen.height/2):
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 dat_touch(self):
#self.dx = - self.dx
#handles paddle touch
#doesn't work = game_over
"""
def end_game(self):
end_message = games.Message(value = "Game Over",
size = 90,
color = color.red,
x = games.screen.width/2,
y = games.screen.height/2,
lifetime = 5 * games.screen.fps,
after_death = games.screen.quit)
games.screen.add(end_message)
"""
def main():
wall_image = games.load_image("background.bmp", transparent = False)
games.screen.background = wall_image
the_ball = Ball()
games.screen.add(the_ball)
the_paddle = Paddle()
games.screen.add(the_paddle)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()