2011-06-25 134 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次(你的更新速度)的每五十。目前,您只需在鼠標位置啓動平板,並將其留在那裏。