2016-05-07 66 views
0

我正在製作一款乒乓球遊戲,最終會在中間會有一個阻擋/流動,與大炮一樣,它會拍攝球或通電和生病最終加入不同的功能,但現在,我甚至不能得到球擊中槳正確..... 進口pygame的乒乓球正在通過槳並擊中牆壁並反射而擊中槳

BLACK = (0,0,0) 
WHITE = (255,255,255) 
RED = (255,0,0) 
GOLD = (201,168,168) 
Call = pygame.sprite.Group(()) 
got = pygame.sprite.Group(()) 
SCR_WID, SCR_HEI = 640, 480 
backg = pygame.image.load("background.png") 


class Player(pygame.sprite.Sprite): 
     def __init__(self, name): 
       pygame.sprite.Sprite.__init__(self) 
       if name == "george": 
         self.x, self.y = 16, SCR_HEI/2 
       elif name =="frank": 
         self.x, self.y = SCR_WID-16, SCR_HEI/2 
       self.name = name 
       self.speed = 3 
       self.padWid, self.padHei = 8, 64 
       self.score = 0 
       self.scoreFont = pygame.font.Font("imagine_font.ttf", 64)   



     def scoring(self): 
       scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255)) 
       if self.name == "george": 
         screen.blit(scoreBlit, (32, 16)) 
         if self.score == 10: 
           print ("player 1 wins!") 
           exit() 
       elif self.name == "frank":   
         screen.blit(scoreBlit, (SCR_HEI+92, 16)) 
         if self.score == 10: 
           print ("Player 2 wins!") 
           exit() 

     def movement(self): 

       keys = pygame.key.get_pressed() 
       if self.name == "george": 
         if keys[pygame.K_w]: 
           self.y -= self.speed 
         elif keys[pygame.K_s]: 
           self.y += self.speed 
       elif self.name == "frank": 
         if keys[pygame.K_UP]: 
           self.y -= self.speed 
         elif keys[pygame.K_DOWN]: 
           self.y += self.speed 

       if self.y <= 0: 
         self.y = 0 
       elif self.y >= SCR_HEI-64: 
         self.y = SCR_HEI-64 

     def draw(self): 
       pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei)) 

球是突破敵人槳去。它實際上並沒有通過它會阻止它,但它通過槳。我一直在嘗試不同的方式,但我對此很陌生並且很掙扎。

這是我認爲是導致我麻煩的類,我沒有得到任何錯誤,因爲它只是沒有擊中槳,因爲id喜歡它。球員槳工作正常,它只是一個不正確的敵人。

class Ball(pygame.sprite.Sprite): 
     image = pygame.image.load("bong.png") 
     def __init__(self): 
       pygame.sprite.Sprite.__init__(self) 
       self.rect = self.image.get_rect() 

       self.rect.x, self.rect.y = SCR_WID/2, SCR_HEI/2 
       self.speed_x = -3 
       self.speed_y = 3 
       self.size = 8 

     def movement(self): 
       self.rect.x += self.speed_x 
       self.rect.y += self.speed_y 


       if self.rect.y <= 0: 
         self.speed_y *= -1 
       elif self.rect.y >= SCR_HEI-self.size: 
         self.speed_y *= -1 

       if self.rect.x <= 0: 
         self.__init__() 
         enemy.score += 1 
       elif self.rect.x >= SCR_WID-self.size: 
         self.__init__() 
         self.speed_x = 3 
         player.score += 1 

       for n in range(-self.size, player.padHei): 
         if self.rect.y == player.y + n: 
           if self.rect.x <= player.x + player.padWid: 
             self.speed_x *= -1 
             break 
         n += 1 
       for n in range(-self.size, enemy.padHei): 
         if self.rect.y == enemy.y + n: 
           if self.rect.x >= enemy.x - enemy.padWid: 
             self.speed_x *= -1 
             break 

我也在爲增加碰撞而苦苦掙扎,所以球會反彈。我嘗試了幾種不同的方式,但注意到球正在通過槳並且必須先解決。這是我的下一步。任何幫助表示讚賞。

class Block(pygame.sprite.Sprite): 
     image = pygame.image.load("brick.png") 


     def __init__(self): 
       pygame.sprite.Sprite.__init__(self) 
       self.rect = self.image.get_rect() 
       self.rect.x = 300 
       self.rect.y = 0 





SCR_WID, SCR_HEI = 640, 480 
screen = pygame.display.set_mode((SCR_WID, SCR_HEI)) 
pygame.display.set_caption("Pong") 
pygame.font.init() 
clock = pygame.time.Clock() 
FPS = 60 
pygame.init() 

def main(): 
    backgscale = pygame.transform.scale(backg,(SCR_WID, SCR_HEI)) 

    global player 
    player = Player("george") 

    global enemy 
    enemy = Player("frank") 

    block = Block() 
    got.add(block) 

    ball = Ball() 
    Call.add(ball) 

    while True: 

      for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
          print("Game exited by user") 
          exit() 

      screen.blit(backg,(0,0))     
      got.update() 
      Call.update() 
      backgscale = pygame.transform.scale(backg,(SCR_WID, SCR_HEI)) 
      got.draw(screen) 
      Call.draw(screen) 
      player.movement() 
      enemy.movement() 
      ball.movement() 

      player.draw() 
      player.scoring() 
      enemy.draw() 
      enemy.scoring() 





      pygame.display.flip() 
      clock.tick(FPS) 

main() 

我不知道我是否打電話給事情是否錯誤。

我試圖創造的精靈羣體,並顯示他們的方式,我不知道一個更好的辦法來顯示他們仍然有他們繼續前進......

+0

嗨,請不要在您接受幫助後對您的帖子進行破壞。這就像在樹下遮蔽下砍樹。請允許其他未來的用戶從知識中獲益。答覆者會付出很多努力。不要把他們的寶貴時間浪費。 –

回答

1

我不得不改變圖像表面爲我沒有圖像,但這對我來說工作正常。

import pygame 
from pygame.locals import * 

SCR_WID, SCR_HEI = 640, 480 
screen = pygame.display.set_mode((SCR_WID, SCR_HEI)) 
pygame.display.set_caption("Pong") 
pygame.font.init() 
clock = pygame.time.Clock() 
FPS = 60 
pygame.init() 


BLACK = (0,0,0) 
WHITE = (255,255,255) 
RED = (255,0,0) 
GOLD = (201,168,168) 
Call = pygame.sprite.Group(()) 
got = pygame.sprite.Group(()) 
#SCR_WID, SCR_HEI = 640, 480 
backg = pygame.Surface((SCR_WID, SCR_HEI)) #pygame.image.load("background.png") 


class Player(pygame.sprite.Sprite): 
     def __init__(self, name): 
       pygame.sprite.Sprite.__init__(self) 
       if name == "george": 
         self.x, self.y = 16, SCR_HEI/2 
       elif name =="frank": 
         self.x, self.y = SCR_WID-16, SCR_HEI/2 
       self.name = name 
       self.speed = 3 
       self.padWid, self.padHei = 8, 64 
       self.score = 0 
       self.scoreFont = pygame.font.SysFont("Arial", 32) #imagine_font.ttf", 64)   



     def scoring(self): 
       scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255)) 
       if self.name == "george": 
         screen.blit(scoreBlit, (32, 16)) 
         if self.score == 10: 
           print ("player 1 wins!") 
           exit() 
       elif self.name == "frank":   
         screen.blit(scoreBlit, (SCR_HEI+92, 16)) 
         if self.score == 10: 
           print ("Player 2 wins!") 
           exit() 

     def movement(self): 

       keys = pygame.key.get_pressed() 
       if self.name == "george": 
         if keys[pygame.K_w]: 
           self.y -= self.speed 
         elif keys[pygame.K_s]: 
           self.y += self.speed 
       elif self.name == "frank": 
         if keys[pygame.K_UP]: 
           self.y -= self.speed 
         elif keys[pygame.K_DOWN]: 
           self.y += self.speed 

       if self.y <= 0: 
         self.y = 0 
       elif self.y >= SCR_HEI-64: 
         self.y = SCR_HEI-64 

     def draw(self): 
       pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei)) 

class Ball(pygame.sprite.Sprite): 
    image = pygame.Surface((8,8)) #pygame.image.load("bong.png") 
    image.fill((255,255,255)) 
    def __init__(self): 
      pygame.sprite.Sprite.__init__(self) 
      self.rect = self.image.get_rect() 

      self.rect.x, self.rect.y = SCR_WID/2, SCR_HEI/2 
      self.speed_x = -3 
      self.speed_y = 3 
      self.size = 8 

    def movement(self): 
      self.rect.x += self.speed_x 
      self.rect.y += self.speed_y 


      if self.rect.y <= 0: 
        self.speed_y *= -1 
      elif self.rect.y >= SCR_HEI-self.size: 
        self.speed_y *= -1 

      if self.rect.x <= 0: 
        self.__init__() 
        enemy.score += 1 
      elif self.rect.x >= SCR_WID-self.size: 
        self.__init__() 
        self.speed_x = 3 
        player.score += 1 

      for n in range(-self.size, player.padHei): 
        if self.rect.y == player.y + n: 
          if self.rect.x <= player.x + player.padWid: 
            self.speed_x *= -1 
            break 
        n += 1 
      for n in range(-self.size, enemy.padHei): 
        if self.rect.y == enemy.y + n: 
          if self.rect.x >= enemy.x - enemy.padWid: 
            self.speed_x *= -1 
            break 

    def draw(self, screen): 
     pygame.draw.rect(screen, (255,255,255), (self.rect.x, self.rect.y, self.size, self.size)) 

class Block(pygame.sprite.Sprite): 
     image = pygame.Surface((20,20)) #image.load("brick.png") 


     def __init__(self): 
       pygame.sprite.Sprite.__init__(self) 
       self.rect = self.image.get_rect() 
       self.rect.x = 300 
       self.rect.y = 0 






def main(): 
    backgscale = pygame.transform.scale(backg,(SCR_WID, SCR_HEI)) 

    global player 
    player = Player("george") 

    global enemy 
    enemy = Player("frank") 

    block = Block() 
    got.add(block) 

    ball = Ball() 
    Call.add(ball) 

    while True: 

      for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
          print("Game exited by user") 
          exit() 

      screen.blit(backg,(0,0))     
      got.update() 
      Call.update() 
      backgscale = pygame.transform.scale(backg,(SCR_WID, SCR_HEI)) 
      got.draw(screen) 
      Call.draw(screen) 
      player.movement() 
      enemy.movement() 
      ball.movement() 

      player.draw() 
      player.scoring() 
      enemy.draw() 
      enemy.scoring() 





      pygame.display.flip() 
      clock.tick(FPS) 

if __name__ == "__main__": 
    main()