Python 3.2:我如何从拥有2个工作类改为只有1个工人阶级,但让Enemy + Player在1个工人阶级中工作?

时间:2016-04-01 20:56:00

标签: python-3.x

    **import pygame**
     

##我的问题是从类Player():到类Enemy()

    **SCR_WID, SCR_HEI = 640, 480**
    **class Player()**:
            ***def __init__(self):***
                    *self.x, self.y = 16, SCR_HEI/2
                    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))
                    screen.blit(scoreBlit, (32, 16))
                    if self.score == 10:
                            print ("player 1 wins!")
                            exit()

            def movement(self):
                    keys = pygame.key.get_pressed()
                    if keys[pygame.K_w]:
                            self.y -= self.speed
                    elif keys[pygame.K_s]:
                            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 Enemy():**
            ***def __init__(self):***
                    *self.x, self.y = SCR_WID-16, SCR_HEI/2
                    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))*
                    *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 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():**
            ***def __init__(self):***
                    *self.x, self.y = SCR_WID/2, SCR_HEI/2
                    self.speed_x = -3
                    self.speed_y = 3
                    self.size = 8*

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

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

                    if self.x <= 0:
                            self.__init__()
                            enemy.score += 1
                    elif self.x >= SCR_WID-self.size:
                            self.__init__()
                            self.speed_x = 3
                            player.score += 1
                    ##wall col
                    #paddle col
                    #player
                    for n in range(-self.size, player.padHei):
                            if self.y == player.y + n:
                                    if self.x <= player.x + player.padWid:
                                            self.speed_x *= -1
                                            break
                            n += 1
                    #enemy
                    for n in range(-self.size, enemy.padHei):
                            if self.y == enemy.y + n:
                                    if self.x >= enemy.x - enemy.padWid:
                                            self.speed_x *= -1
                                            break
                            n += 1
                    ##paddle col*

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

    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

    player = Player() 
    ball = Ball()
    #player = Player()
    enemy = Enemy()

    while True:
            #process
            for event in pygame.event.get():
                            if event.type == pygame.QUIT:
                                    print ("Game exited by user")
                                    exit()
            ##process
            #logic
            ball.movement()
            player.movement()
            enemy.movement()
            ##logic
            #draw
            screen.fill((0, 0, 0))
            ball.draw()
            player.draw()
            player.scoring()
            enemy.draw()
            enemy.scoring()
            ##draw
            #_______
            pygame.display.flip()
            clock.tick(FPS)*
     

##我的大学教授希望我这样做,即使他从未教过课程如何去做。那么当我不知道怎么做时,我怎么想这样做呢?

1 个答案:

答案 0 :(得分:1)

如果你看一下Player and Enemy的代码,你会发现它们非常相似 - 它们之间的唯一区别是:

  • 桨位置
  • 分数的x位置
  • 获胜消息的名称
  • 上下键

您可以将它们传递到Service方法并将它们存储为属性,然后在需要它们的方法中使用它们。

__init__

然后在def __init__(self, name, paddleX, scoreX, upKey, downKey): self.name = name self.paddleX = paddleX self.scoreX = scoreX self.upKey = upKey self.downKey = downKey ... 代码中使用这些代码而不是代码现在使用的常量 - 所以代替Playerpygame.K_UP,您使用pygame.K_w

然后而不是:

self.upKey

你可以为玩家和敌人传递正确的值:

player = Player()
enemy = Enemy()

然后您不再需要player = Player( name='player 1', paddleX=16, scoreX=16, upKey=pygame.K_w, downKey=pygame.K_s, ) enemy = Player( name='player 2', paddleX=SCR_WID - 16, scoreX=SCR_HEI + 92, upKey=pygame.K_UP, downKey=pygame.K_DOWN, ) 课程 - 它使用与Enemy相同的代码,但有一些不同的设置。希望有所帮助!