调试简单的乒乓球游戏?
我一直在使用 Python 的 Pygame 通过 Livewires 编写一个简单的乒乓球游戏。我已经发布了这个问题和一个类似的示例游戏代码,该代码可以完美运行,因为并非这里的每个人都会精通 livewires 和/或 pygame。
这是有错误的代码。发生的情况是窗口打开,“球”对象运行良好并从屏幕上落下(应该如此,它是不完整的),并且“板”对象卡在鼠标最初所在的位置。就是这样:
from livewires import games, color
games.init (screen_width = 640, screen_height = 480, fps = 50)
class Ball (games.Sprite):
def iMove (self):
self.dx = -self.dx
self.dy = -self.dy
class Slab (games.Sprite):
def mouse_moves (self):
self.x = games.mouse.x
self.y = games.mouse.y
self.iCollide()
def iCollide (self):
for Ball in overlapping_sprites:
Ball.iMove()
def main():
#Backgrounds
pingpongbackground = games.load_image ("pingpongbackground.jpg", transparent = False)
games.screen.background = pingpongbackground
#Ball: Initializing object and setting speed.
ballimage = games.load_image ("pingpongball.jpg")
theball = Ball (image = ballimage,
x = 320,
y = 240,
dx = 2,
dy = 2)
games.screen.add(theball)
#Paddle: Initializing ping pong object and setting initial poisition to the initial mouse position
slabimage = games.load_image ("pingpongpaddle.jpg")
theslab = Slab (image = slabimage,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(theslab)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main ()
这是一段类似的功能代码:
# Slippery Pizza Program
# Demonstrates testing for sprite collisions
from livewires import games
import random
games.init(screen_width = 640, screen_height = 480, fps = 50)
class Pan(games.Sprite):
"""" A pan controlled by the mouse. """
def update(self):
""" Move to mouse position. """
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
""" Check for collision with pizza. """
for pizza in self.overlapping_sprites:
pizza.handle_collide()
class Pizza(games.Sprite):
"""" A slippery pizza. """
def handle_collide(self):
""" Move to a random screen location. """
self.x = random.randrange(games.screen.width)
self.y = random.randrange(games.screen.height)
def main():
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
pizza_image = games.load_image("pizza.bmp")
pizza_x = random.randrange(games.screen.width)
pizza_y = random.randrange(games.screen.height)
the_pizza = Pizza(image = pizza_image, x = pizza_x, y = pizza_y)
games.screen.add(the_pizza)
pan_image = games.load_image("pan.bmp")
the_pan = Pan(image = pan_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pan)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
# kick it off!
main()
任何见解都将不胜感激!
I've been programming a simple ping-pong game using Python's Pygame as run through Livewires. I've posted the question and a similar sample game code that runs perfectly as not everyone here will be versed in livewires and/or pygame.
Here's the code that has bugs on it. What happens is the window opens, and the "Ball" object works well and falls of the screen (as it should, it's incomplete), and the "Slab" object is gets stuck to wherever the mouse initially is. Here it is:
from livewires import games, color
games.init (screen_width = 640, screen_height = 480, fps = 50)
class Ball (games.Sprite):
def iMove (self):
self.dx = -self.dx
self.dy = -self.dy
class Slab (games.Sprite):
def mouse_moves (self):
self.x = games.mouse.x
self.y = games.mouse.y
self.iCollide()
def iCollide (self):
for Ball in overlapping_sprites:
Ball.iMove()
def main():
#Backgrounds
pingpongbackground = games.load_image ("pingpongbackground.jpg", transparent = False)
games.screen.background = pingpongbackground
#Ball: Initializing object and setting speed.
ballimage = games.load_image ("pingpongball.jpg")
theball = Ball (image = ballimage,
x = 320,
y = 240,
dx = 2,
dy = 2)
games.screen.add(theball)
#Paddle: Initializing ping pong object and setting initial poisition to the initial mouse position
slabimage = games.load_image ("pingpongpaddle.jpg")
theslab = Slab (image = slabimage,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(theslab)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main ()
And here's a piece of similar, functioning code:
# Slippery Pizza Program
# Demonstrates testing for sprite collisions
from livewires import games
import random
games.init(screen_width = 640, screen_height = 480, fps = 50)
class Pan(games.Sprite):
"""" A pan controlled by the mouse. """
def update(self):
""" Move to mouse position. """
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
""" Check for collision with pizza. """
for pizza in self.overlapping_sprites:
pizza.handle_collide()
class Pizza(games.Sprite):
"""" A slippery pizza. """
def handle_collide(self):
""" Move to a random screen location. """
self.x = random.randrange(games.screen.width)
self.y = random.randrange(games.screen.height)
def main():
wall_image = games.load_image("wall.jpg", transparent = False)
games.screen.background = wall_image
pizza_image = games.load_image("pizza.bmp")
pizza_x = random.randrange(games.screen.width)
pizza_y = random.randrange(games.screen.height)
the_pizza = Pizza(image = pizza_image, x = pizza_x, y = pizza_y)
games.screen.add(the_pizza)
pan_image = games.load_image("pan.bmp")
the_pan = Pan(image = pan_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pan)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
# kick it off!
main()
Any insight at all would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你的框架,但为了防止平板“卡住”,你需要在移动鼠标时更新其位置。
在这里初始化它:
然后在这里将它添加到游戏中:
据推测,只要鼠标移动,游戏就会调用这个函数:
但这要么没有发生,要么屏幕没有更新。
因此,您应该通过这样做来发现:
如果您看到鼠标移动时发生打印语句的输出,则您可能没有更新屏幕,您需要检查框架文档。但我认为情况并非如此。我认为当鼠标移动时你没有更新游戏。我想框架有某种您需要挂钩的 onMouseMove 类型事件,以允许您在发生鼠标移动时更新游戏状态(即调用 mouse_moves())。然后,下次更新屏幕时,您应该检查是否有更改(使对象无效,将它们标记为脏),如果它们脏了,则更新其屏幕部分,然后再次将它们标记为干净。
I don't know your framework, but to keep the slab from "getting stuck" you need to update its location when the mouse is moved.
Here you initialize it:
Then here you add it to the game:
Presumably, the game would then call this function whenever the mouse moves:
But that is either not happening, or the screen is not getting updated.
So you should find out, by doing this:
If you see the output of the print statement happening when the mouse moves, you're probably not updating the screen, you'll need to check the framework docs. But I don't think that's the case. I think you are not updating the game when the mouse moves. I would imagine the framework has some sort of onMouseMove type event that you need to hook into, to allow you to update the game state (i.e. call mouse_moves()) when mouse movement occurs. Then, the next time the screen is updated, you should check for changes (invalidate the objects, mark them as dirty) and if they are dirty, update their part of the screen then mark them clean again.
只需将这个更新方法放在slab类中即可:
它会每五十分之一秒自动运行一次(你的更新速度)。目前,您只需在鼠标的位置启动板,然后将其留在那里。
Just put this update method in the slab class:
It will automatically run once every fiftieth of a second (your update speed). Currently, you just start the slab at the position of the mouse, and leave it there.