拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Python游戏开发,pygame模块,Python实作乒乓球小游戏

Python游戏开发,pygame模块,Python实作乒乓球小游戏

白鹭 - 2022-01-24 2092 0 0

前言:

利用python制作一款简单的乒乓球小游戏,让我们愉快地开始吧~

开发工具

Python版本: 3.6.4

相关模块:

pygame模块;

以及一些Python自带的模块,

环境搭建

pip安装需要的相关模块即可,

效果展示

image.png

原理简介

游戏规则:

操作:

玩家1(右)通过操作↑↓键上下移动球拍;

玩家2(左)通过操作ws键上下移动球拍(仅双人模式有效),

得分:

玩家没有接住乒乓球则失一分,即对方玩家得一分,得分先累计到11的一方即为获胜方,

逐步实作:

Step1:开始界面

开始界面其实很简单,只需要定义两个按钮,然后当检测到玩家点击按钮时,将按钮对应的值传到接下来的游戏主回圈中即可,代码实作如下:

'''定义按钮'''
def Button(screen, position, text, button_size=(200, 50)):
  left, top = position
  bwidth, bheight = button_size
  pygame.draw.line(screen, (150, 150, 150), (left, top), (left+bwidth, top), 5)
  pygame.draw.line(screen, (150, 150, 150), (left, top-2), (left, top+bheight), 5)
  pygame.draw.line(screen, (50, 50, 50), (left, top+bheight), (left+bwidth, top+bheight), 5)
  pygame.draw.line(screen, (50, 50, 50), (left+bwidth, top+bheight), (left+bwidth, top), 5)
  pygame.draw.rect(screen, (100, 100, 100), (left, top, bwidth, bheight))
  font = pygame.font.Font(config.FONTPATH, 30)
  text_render = font.render(text, 1, (255, 235, 205))
  return screen.blit(text_render, (left+50, top+10))


'''
Function:
  开始界面
Input:
  --screen: 游戏界面
Return:
  --game_mode: 1(单人模式)/2(双人模式)
'''
def startInterface(screen):
  clock = pygame.time.Clock()
  while True:
    screen.fill((41, 36, 33))
    button_1 = Button(screen, (150, 175), '1 Player')
    button_2 = Button(screen, (150, 275), '2 Player')
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
      if event.type == pygame.MOUSEBUTTONDOWN:
        if button_1.collidepoint(pygame.mouse.get_pos()):
          return 1
        elif button_2.collidepoint(pygame.mouse.get_pos()):
          return 2
    clock.tick(10)
    pygame.display.update()

Step2:游戏主回圈

接下来写游戏主回圈,为了方便起见,先定义两个游戏精灵类,分别是球拍精灵和球精灵,其中球拍精灵应当具备被玩家手动控制而移动/根据乒乓球的位置由计算机自动控制而移动的能力,具体实作如下:

'''乒乓球拍'''
class Racket(pygame.sprite.Sprite):
  def __init__(self, imgpath, type_, **kwargs):
    pygame.sprite.Sprite.__init__(self)
    self.type_ = type_
    self.image = loadImage(imgpath, False)
    self.rect = self.image.get_rect()
    self.reset()
  '''移动'''
  def move(self, direction):
    if direction == 'UP':
      self.rect.top = max(0, self.rect.top-self.speed)
    elif direction == 'DOWN':
      self.rect.bottom = min(config.HEIGHT, self.rect.bottom+self.speed)
    else:
      raise ValueError('[direction] in Racket.move is <%s>, expect <%s> or <%s>...' % (direction, 'UP', 'DOWN'))
  '''计算机自动移动'''
  def automove(self, ball):
    if ball.rect.centery - 25 > self.rect.centery:
      self.move('DOWN')
    if ball.rect.centery + 25 < self.rect.centery:
      self.move('UP')
  '''初始化'''
  def reset(self):
    # 左/右边的拍
    self.rect.centerx = config.WIDTH-self.rect.width//2 if self.type_ == 'RIGHT' else self.rect.width//2
    self.rect.centery = config.HEIGHT // 2
    # 速度
    self.speed = 5
  '''系结到荧屏上'''
  def draw(self, screen):
    screen.blit(self.image, self.rect)

而乒乓球则只需要根据当前的情况(包括是否撞到了墻,是否撞到了球拍等情况)自动移动即可,需要注意的一点是,为了避免游戏无限地进行下去,每次乒乓球撞到球拍/上下墻,乒乓球的运动速度都会增****加,具体而言,代码实作如下:

'''乒乓球'''
class Ball(pygame.sprite.Sprite):
  def __init__(self, imgpath, **kwargs):
    pygame.sprite.Sprite.__init__(self)
    self.image = loadImage(imgpath)
    self.rect = self.image.get_rect()
    self.reset()
  '''移动'''
  def move(self, ball, racket_left, racket_right, hit_sound, goal_sound):
    self.rect.left = self.rect.left + self.speed * self.direction_x
    self.rect.top = min(max(self.rect.top+self.speed*self.direction_y, 0), config.HEIGHT-self.rect.height)
    # 撞到球拍
    if pygame.sprite.collide_rect(ball, racket_left) or pygame.sprite.collide_rect(ball, racket_right):
      self.direction_x, self.direction_y = -self.direction_x, random.choice([1, -1])
      self.speed += 1
      scores = [0, 0]
      hit_sound.play()
    # 撞到上侧的墻
    elif self.rect.top == 0:
      self.direction_y = 1
      self.speed += 1
      scores = [0, 0]
    # 撞到下侧的墻
    elif self.rect.top == config.HEIGHT - self.rect.height:
      self.direction_y = -1
      self.speed += 1
      scores = [0, 0]
    # 撞到左边的墻
    elif self.rect.left < 0:
      self.reset()
      racket_left.reset()
      racket_right.reset()
      scores = [0, 1]
      goal_sound.play()
    # 撞到右边的墻
    elif self.rect.right > config.WIDTH:
      self.reset()
      racket_left.reset()
      racket_right.reset()
      scores = [1, 0]
      goal_sound.play()
    # 普通情况
    else:
      scores = [0, 0]
    return scores
  '''初始化'''
  def reset(self):
    self.rect.centerx = config.WIDTH // 2
    self.rect.centery = random.randrange(self.rect.height//2, config.HEIGHT-self.rect.height//2)
    self.direction_x = random.choice([1, -1])
    self.direction_y = random.choice([1, -1])
    self.speed = 1
  '''系结到荧屏上'''
  def draw(self, screen):
    screen.blit(self.image, self.rect)

定义完两个主要的游戏精灵类,我们就可以开始写游戏主回圈了,逻辑其实很简单,首先,通过按键检测回应玩家的操作;然后,根据玩家操作实时更新游戏状态(乒乓球的位置,球拍等);最后统计得分,判断游戏是否已经结束,若结束,则进入结束界面,否则更新当前的游戏界面,具体而言,代码实作如下:

while True:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit(-1)
    screen.fill((41, 36, 33))
    # 玩家操作
    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[pygame.K_UP]:
      racket_right.move('UP')
    elif pressed_keys[pygame.K_DOWN]:
      racket_right.move('DOWN')
    if game_mode == 2:
      if pressed_keys[pygame.K_w]:
        racket_left.move('UP')
      elif pressed_keys[pygame.K_s]:
        racket_left.move('DOWN')
    else:
      racket_left.automove(ball)
    # 球运动
    scores = ball.move(ball, racket_left, racket_right, hit_sound, goal_sound)
    score_left += scores[0]
    score_right += scores[1]
    # 显示
    # --分隔线
    pygame.draw.rect(screen, config.WHITE, (247, 0, 6, 500))
    # --球
    ball.draw(screen)
    # --拍
    racket_left.draw(screen)
    racket_right.draw(screen)
    # --得分
    screen.blit(font.render(str(score_left), False, config.WHITE), (150, 10))
    screen.blit(font.render(str(score_right), False, config.WHITE), (300, 10))
    if score_left == 11 or score_right == 11:
      return score_left, score_right
    clock.tick(100)
    pygame.display.update()

Step3:游戏结束界面

游戏结束界面和游戏开始界面的原理差不多,就不多说了,直接放代码吧:

'''结束界面'''
def endInterface(screen, score_left, score_right):
  clock = pygame.time.Clock()
  font1 = pygame.font.Font(config.FONTPATH, 30)
  font2 = pygame.font.Font(config.FONTPATH, 20)
  msg = 'Player on left won!' if score_left > score_right else 'Player on right won!'
  texts = [font1.render(msg, True, config.WHITE),
       font2.render('Press ESCAPE to quit.', True, config.WHITE),
       font2.render('Press ENTER to continue or play again.', True, config.WHITE)]
  positions = [[120, 200], [155, 270], [80, 300]]
  while True:
    screen.fill((41, 36, 33))
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RETURN:
          return
        elif event.key == pygame.K_ESCAPE:
          sys.exit()
          pygame.quit()
    for text, pos in zip(texts, positions):
      screen.blit(text, pos)
    clock.tick(10)
    pygame.display.update()

文章到这里就结束了,感谢你的观看,Python24个小游戏系列,下篇文章分享打砖块小游戏

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *