2011-06-25 92 views
0

我一直在使用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() 

任何有识之士都将不胜感激!

+1

为什么你使用不同的方法名称?你需要正确的名字或者你的函数不会被调用。 –

+0

我会重命名,检查并回传。 – Louis93

+0

是的,我需要每个类中的前导更新(self)方法来引发其他方法,还有其他一些小错误,但我大部分都是固定的。 – Louis93

回答

1

我不知道你的框架,但为了防止“卡住”板,你需要更新它的位置,当鼠标移动。

在这里,您将其初始化:

theslab = Slab (image = slabimage, 
        x = games.mouse.x, 
        y = games.mouse.y) 

然后在这里你将它添加到游戏:

games.screen.add(theslab) 

据推测,本场比赛将随后调用此函数,只要鼠标移动:

def mouse_moves (self): 
    self.x = games.mouse.x 
    self.y = games.mouse.y 
    self.iCollide() 

但是,要么没有发生,或屏幕没有得到更新。

所以你应该找出,这样做:

def mouse_moves (self): 
    print "mouse_moves: ", str(games.mouse.x), str(games.mouse.y) 
    self.x = games.mouse.x 
    self.y = games.mouse.y 
    self.iCollide() 

如果看到打印语句的输出发生当鼠标移动时,你很可能不会更新屏幕,你需要检查框架文档。但我不认为是这样。我认为你没有在鼠标移动时更新游戏。我想这个框架有一些你需要挂钩的onMouseMove类型的事件,以允许你在发生鼠标移动时更新游戏状态(即调用mouse_moves())。然后,下次更新屏幕时,应检查更改(使对象无效,将其标记为脏),并且如果它们很脏,则更新屏幕的一部分,然后再次将其标记为干净。

+0

谢谢,我会再次阅读文档。 – Louis93

+0

对不起,在我第一次询问时忘了选择这个。刚刚做到了。再次感谢。 – Louis93

+1

@ Louis93:谢谢。有事尽管问我。 –

0

只是把这个更新方法在平板类:

def update(self): 
    self.x=games.mouse.x 
    self.y=games.mouse.y 

它将自动运行1次(你的更新速度)的每五十。目前,您只需在鼠标位置启动平板,并将其留在那里。