我如何编程我的乒乓球游戏在 python 中有一个 npc?

问题描述

我正在制作一个包含 NPC 选项的乒乓球游戏,我是 pygame 的新手,无法弄清楚如何让左侧的球拍自己跟随球。我有一个 2 人选项和一个单人选项,但一个人不工作。我会在这里添加我所拥有的,任何帮助将不胜感激。谢谢!

      import pygame
      from random import randint

      pygame.init()

      BLACK = (255,255,255)
      WHITE = (0,0)

      class Paddle(pygame.sprite.Sprite):
        def __init__(self,color,width,height):
          super().__init__()
          self.image = pygame.Surface([width,height])
          self.image.fill(WHITE)
          self.image.set_colorkey(WHITE)
          pygame.draw.rect(self.image,[0,height])
          self.rect = self.image.get_rect()
        def moveUp(self,pixels):
          self.rect.y -= pixels
          if self.rect.y < 0:
            self.rect.y = 0
        def moveDown(self,pixels):
          self.rect.y += pixels
          if self.rect.y > 400:
            self.rect.y = 400

      class NPC(pygame.sprite.Sprite):
        def __init__(self,height])
          self.rect = self.image.get_rect()


      class Ball(pygame.sprite.Sprite):
          def __init__(self,height):
              super().__init__()
              self.image = pygame.Surface([width,height])
              self.image.fill(WHITE)
              self.image.set_colorkey(WHITE)
              pygame.draw.rect(self.image,height])
              self.veLocity = [randint(4,8),randint(-8,8)]
              self.rect = self.image.get_rect()
          def update(self):
              self.rect.x += self.veLocity[0]
              self.rect.y += self.veLocity[1]
          def bounce(self):
              self.veLocity[0] = -self.veLocity[0]
              self.veLocity[1] = randint(-8,8)

      size = (700,500)
      screen = pygame.display.set_mode(size)
      pygame.display.set_caption("Pong")

      paddleA = Paddle(BLACK,10,100)
      paddleA.rect.x = 20
      paddleA.rect.y = 200
      
      paddleB = Paddle(BLACK,100)
      paddleB.rect.x = 670
      paddleB.rect.y = 200

      ball = Ball(BLACK,10)
      ball.rect.x = 345
      ball.rect.y = 195

      all_sprites_list = pygame.sprite.Group()
      all_sprites_list.add(paddleA)
      all_sprites_list.add(paddleB)
      all_sprites_list.add(ball)


      keepPlaying = True

      clock = pygame.time.Clock()

      scoreA = 0
      scoreB = 0

      screen.fill(WHITE)
      font = pygame.font.Font(None,50)
      text = font.render(str("Press 1 for 1 player."),1,BLACK)
      screen.blit(text,(200,150))
      text = font.render(str("Press 2 for 2 player."),250))
      pygame.display.flip()

      playGame = "Yes"

      #2 Player
      while playGame == "Yes":
        for event in pygame.event.get():
          if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_2:
              while keepPlaying == True:
                for event in pygame.event.get():
                  if event.type == pygame.QUIT:
                    keepPlaying = False
                  elif event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_x:
                      keepPlaying=False

                keys = pygame.key.get_pressed()
                if keys[pygame.K_w]:
                  paddleA.moveUp(5)
                if keys[pygame.K_s]:
                  paddleA.moveDown(5)
                if keys[pygame.K_UP]:
                  paddleB.moveUp(5)
                if keys[pygame.K_DOWN]:
                  paddleB.moveDown(5)  

              
                all_sprites_list.update()

                if ball.rect.x>=690:
                  scoreA += 1
                  ball.veLocity[0] = -ball.veLocity[0]
                if ball.rect.x<=0:
                  scoreB += 1
                  ball.veLocity[0] = -ball.veLocity[0]
                if ball.rect.y>490:
                  ball.veLocity[1] = -ball.veLocity[1]
                if ball.rect.y<0:
                  ball.veLocity[1] = -ball.veLocity[1] 

                if pygame.sprite.collide_mask(ball,paddleA) or pygame.sprite.collide_mask(ball,paddleB):
                  ball.bounce()

                screen.fill(WHITE)
                pygame.draw.line(screen,BLACK,[349,50],500],5)
                all_sprites_list.draw(screen) 

                font = pygame.font.Font(None,74)
                text = font.render(str(scoreA),BLACK)
                screen.blit(text,(250,10))
                text = font.render(str(scoreB),(420,10))
                font = pygame.font.Font(None,20)
                text = font.render(str("Press x to quit."),(310,30))

                pygame.display.flip()
                clock.tick(60)
              pygame.quit()

      #Player vs A.I.

            elif event.key==pygame.K_1:
              while keepPlaying == True:
                for event in pygame.event.get():
                  if event.type == pygame.QUIT:
                    keepPlaying = False
                  elif event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_x:
                      keepPlaying=False

                keys = pygame.key.get_pressed()
                if keys[pygame.K_UP]:
                  paddleB.moveUp(5)
                if keys[pygame.K_DOWN]:
                  paddleB.moveDown(5)  

                paddleNPC = NPC(BLACK,100)
                paddleNPC.rect.x = 20
                paddleNPC.rect.y = 200

                while keepPlaying == True:
                  paddleNPC.rect.y = ball.rect.y

                all_sprites_list.update()

                if ball.rect.x>=690:
                  scoreA += 1
                  ball.veLocity[0] = -ball.veLocity[0]
                if ball.rect.x<=0:
                  scoreB += 1
                  ball.veLocity[0] = -ball.veLocity[0]
                if ball.rect.y>490:
                  ball.veLocity[1] = -ball.veLocity[1]
                if ball.rect.y<0:
                  ball.veLocity[1] = -ball.veLocity[1] 

                if pygame.sprite.collide_mask(ball,30))

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

              pygame.quit()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)