2013-04-15 39 views
0

嘿所有即時通訊工作在一個家庭作業,其中我必須通過pygame和livewire創建單人乒乓遊戲。我的代碼大部分已完成,但我確實有一個問題。屏幕不會更新,因此槳板不會移動,球也不會反彈。我有槳和球的更新方法,但由於某些原因它不起作用。這是我的代碼謝謝!在python中的乒乓程序,不能用鼠標移動槳

更新:我不得不重做出頭,但現在我可以對我的課打電話,球反彈,但我不能移動擋板。我不知道爲什麼這不起作用,因爲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() 
+0

我會嘗試,謝謝編輯:我試圖把pygame.display.update()並沒有發生。即時通訊假設該行代碼不適用於livewires。 –

+0

是的。您正在使用livewires屏幕,而不是pygame屏幕。 – PygameNerd

+0

所以,至少,球/槳DO會出現在屏幕上。你確定更新方法被調用嗎?嘗試將打印語句放入球更新方法中,並確保該方法確實被稱爲 –

回答

1

在這裏我看到了幾個問題:

首先,你定義你的「球」和「槳」類,但你似乎沒有使用它們。取而代之的是,你正在做以下在main()函數:

the_ball=games.Sprite(image=ball_image, 
         x=600, 
         y=250, 
         dx=2, 
         dy=-2) 

通過僅定義這是一個Sprite,你只使用已在創業奇兵的雪碧對象定義的邏輯,全然不顧你的球或酒吧班。相反,您可能想創建一個「球」和「酒吧」對象。例如,要創建一個「球」的對象,你會改變上述行來......

the_ball=Ball(image=ball_image, 
         x=600, 
         y=250, 
         dx=2, 
         dy=-2) 

所以,你必須定義一個Ball類,和(使用變更後的代碼我上面提供的)你會在座標x = 600和y = 250時在球上創建。爲了讓這個球移動,x和y座標需要改變。例如,將其移動到正確的20個單位中,x將需要更改爲620

所以,你需要考慮的X和Y座標如何變化。通過LiveWires代碼展望(下課時爲如何,如果你很好奇訪問LiveWires代碼中發言,以你的老師),我讀了以下內容:

# Some [objects] will, more specifically, move at regular intervals; 
# their classes should subclass Mover (which < Timer) and 
# define a moved method. 

所以,現在,你的球類是隻從雪碧繼承...

class Ball(games.Sprite): 

    image = games.load_image("ball.bmp") 
    speed = 2 

    def __init__(self, x = 100, y = 100): 
     super(Ball, self).__init__(image = Ball.image, 
            x = x, y = y, 
            dx = Ball.speed, dy = Ball.speed) 

嘗試改變這使得它從兩個雪碧和捷運繼承...

class Ball(games.Sprite, games.Mover): 

    image = games.load_image("ball.bmp") 
    speed = 2 

    def __init__(self, x = 100, y = 100): 
     super(Ball, self).__init__(image = Ball.image, 
            x = x, y = y, 
            dx = Ball.speed, dy = Ball.speed) 

你在做什麼這裏是讓你的對象不僅inher它是「Sprite」類的功能(它處理它在提供的座標上畫在屏幕上),但也繼承了「Mover」類(它根據dx和dy更改x和y值)的功能。

希望這會給你一個你想要做的開始。

+0

,謝謝我會嘗試您的建議 –

+0

我試圖使用類並使球彈跳,但移動槳時遇到問題。非常感謝您的幫助 –

+1

您確定更新方法在槳板對象上被調用嗎?如果你在該方法中放置打印語句,是否有東西會打印到控制檯? –