Advertisement
renix1

pong in python

Jan 10th, 2020
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # coding: utf-8
  3. import constant
  4. import pygame
  5.  
  6.  
  7. class Keyboard:
  8.     def listen_keys(self):
  9.         pygame.event.pump()
  10.         keys = pygame.key.get_pressed()
  11.         keys_players = {
  12.                         self.player_one: { pygame.K_w: -10, pygame.K_s: +10},
  13.                         self.player_two: { pygame.K_UP: -10, pygame.K_DOWN: +10}
  14.         }
  15.         for player,v in keys_players.items():
  16.             for key_number,add in keys_players[player].items():
  17.                 if keys[key_number]:
  18.                     player.move(add)
  19.                 if keys[pygame.K_r] and self.ball_tmp is not None:
  20.                     self.ball = self.ball_tmp
  21.                     self.ball_tmp = None
  22.                     self.init_settings()
  23.  
  24.  
  25. class Ball:
  26.     def __init__(self, pos_x, pos_y):
  27.         self.x, self.y = pos_x, pos_y
  28.         self.xspeed, self.yspeed = 5, 0
  29.         self.width, self.height = 20, 20
  30.         self.last_move = pygame.time.get_ticks()
  31.         self.last_player_touched = None
  32.  
  33.     def draw(self, surface):
  34.         self.rect = pygame.draw.rect(surface, (constant.WHITE), (self.x, self.y,
  35.                                                                     self.width, self.height))
  36.  
  37.     def move(self):
  38.         diff = (pygame.time.get_ticks() - self.last_move)
  39.         if diff > 0.3: # each 0,3*10**-3μs move
  40.             self.yspeed = +5 if self.y <= 0 else self.yspeed
  41.             self.yspeed = -5 if self.y >= (constant.HEIGHT - self.height) else self.yspeed
  42.             if (self.x >= 0) and (self.x <= constant.WIDTH):
  43.                 self.x += self.xspeed
  44.             if (self.y >= -constant.HEIGHT) and (self.y <= constant.HEIGHT):
  45.                 self.y += self.yspeed
  46.             self.last_move = pygame.time.get_ticks()
  47.  
  48.     def collide_with(self, *players):
  49.         for player in players:
  50.             if player.rect.colliderect(self.rect):
  51.                 self.last_player_touched = player
  52.                 self.xspeed = abs(self.xspeed) if player.x < 30 else -abs(self.xspeed)
  53.                 self.yspeed = ((self.y**1.3) - (player.y**1.3)) / player.height
  54.  
  55.     def has_gone(self):
  56.         if (self.x <= self.width) or (self.x >= constant.WIDTH):
  57.             return True
  58.  
  59. class Player:
  60.     def __init__(self, pos_x, pos_y, name):
  61.         self.name = name
  62.         self.width, self.height = 10, 100
  63.         self.x, self.y = pos_x, (pos_y - (self.height // 2))
  64.  
  65.     def draw(self, surface):
  66.         self.rect = pygame.draw.rect(surface, (constant.WHITE), (self.x, self.y,
  67.                                                         self.width, self.height))
  68.  
  69.     def move(self, y):
  70.         if (self.y + y > self.width) and ((self.y + y) < constant.HEIGHT - (self.height + self.width)):
  71.             self.y += y
  72.  
  73.  
  74. class Game(Keyboard):
  75.     def __init__(self):
  76.         pygame.init()
  77.         pygame.font.init()
  78.         pygame.display.set_caption("Pong in Python")
  79.         pygame.event.set_allowed([pygame.QUIT, pygame.KEYDOWN])
  80.         self._game_border = 25
  81.         self.font = pygame.font.Font(pygame.font.get_default_font(), 32)
  82.         self.screen = pygame.display.set_mode(constant.SIZE, pygame.DOUBLEBUF)
  83.         self.screen.set_alpha(None)
  84.         self.ball = Ball(self.on_center(constant.WIDTH), self.on_center(constant.HEIGHT))
  85.         self.ball_tmp = None
  86.         self.player_one = Player(self._game_border, self.on_center(constant.HEIGHT), "1")
  87.         self.player_two = Player(constant.WIDTH - self._game_border, self.on_center(constant.HEIGHT), "2")
  88.         self.init_settings()
  89.         self.start()
  90.  
  91.     def on_center(self, size):
  92.         return size // 2
  93.  
  94.     def init_settings(self):
  95.         self.ball.x, self.ball.y = self.on_center(constant.WIDTH), self.on_center(constant.HEIGHT)
  96.         self.player_one_x, self.player_one_y = self._game_border, self.on_center(constant.HEIGHT)
  97.         self.player_two_x, self.player_two_y = constant.WIDTH - self._game_border, self.on_center(constant.HEIGHT)
  98.  
  99.     def start(self):
  100.         while True:
  101.             try:
  102.                 if pygame.event.get(pygame.QUIT): break
  103.                 self.screen.fill(constant.BLACK)
  104.                 self.ball.draw(self.screen)
  105.                 self.ball.move()
  106.                 self.player_one.draw(self.screen)
  107.                 self.player_two.draw(self.screen)
  108.                 self.ball.collide_with(self.player_one, self.player_two)
  109.                 if self.ball.has_gone():
  110.                     self.ball_tmp = self.ball
  111.                     del self.ball
  112.             except AttributeError as e:
  113.                 someone_wins = self.font.render("player {} wins!".format(self.ball_tmp.last_player_touched.name), True, (constant.WHITE))
  114.                 info = self.font.render("press R to restart", True, (constant.WHITE))
  115.                 self.screen.blit(someone_wins, (self.on_center(constant.WIDTH) - 100, 50))
  116.                 self.screen.blit(info, (self.on_center(constant.WIDTH) - 115, 100))
  117.             finally:
  118.                 self.listen_keys()
  119.                 pygame.display.flip()
  120.  
  121. if __name__ == '__main__':
  122.     Game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement