Guest User

python simple pong

a guest
Mar 3rd, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. import pygame, sys
  2. from pygame.locals import *
  3.  
  4. STICK_WIDTH = 50
  5. BALL_WIDTH = 25
  6. BALL_SLOWNESS = 6
  7.  
  8. SCREEN_Y_MARGIN = 60
  9. SCREEN_WIDTH = 800
  10. SCREEN_HEIGHT = 600
  11.  
  12.  
  13. class MovingObject(object):
  14.     def __init__(self, surface, x, y, image):
  15.         self.x = x
  16.         self.y = y
  17.         self.surface = surface
  18.         self.image = image
  19.  
  20.     def move(self, dx, dy):
  21.         self.x += dx
  22.         self.y += dy
  23.  
  24.     def draw(self):
  25.         self.surface.blit(self.image, (self.x, self.y))
  26.  
  27.  
  28. class Stick(MovingObject):
  29.     def __init__(self, surface, x, y, image):
  30.         super().__init__(surface, x, y, image)
  31.  
  32.     def move(self, dx, dy):
  33.         super().move(dx, dy)
  34.         if self.x + STICK_WIDTH > SCREEN_WIDTH:
  35.             self.x = SCREEN_WIDTH - STICK_WIDTH
  36.         if self.x < 0:
  37.             self.x = 0
  38.  
  39.     def draw(self):
  40.         super().draw()
  41.  
  42.     def close_to(self, ball):
  43.         if abs(self.x - ball.x) < STICK_WIDTH and abs(self.y - ball.y) < BALL_WIDTH and not ball.was_last_bounced:
  44.             return True
  45.         else:
  46.             return False
  47.  
  48.  
  49. class Ball(MovingObject):
  50.     def __init__(self, surface, x, y, image):
  51.         super().__init__(surface, x, y, image)
  52.         self.direction_x = 1
  53.         self.direction_y = 1
  54.         self.was_last_bounced = False
  55.         self.last_bounce_count = 0
  56.  
  57.     def move(self):
  58.         super().move(self.direction_x, self.direction_y)
  59.         if self.x + BALL_WIDTH > SCREEN_WIDTH:
  60.             self.x = SCREEN_WIDTH - BALL_WIDTH
  61.             self.direction_x *= -1
  62.         if self.x < 0:
  63.             self.x = 0
  64.             self.direction_x *= -1
  65.         if self.y + BALL_WIDTH > SCREEN_HEIGHT:
  66.             self.y = SCREEN_HEIGHT - BALL_WIDTH
  67.             self.direction_y *= -1
  68.         if self.y < 0:
  69.             self.y = 0
  70.             self.direction_y *= -1
  71.         if self.was_last_bounced:
  72.             self.last_bounce_count += 1
  73.             if self.last_bounce_count > BALL_WIDTH + 1:
  74.                 self.was_last_bounced = False
  75.  
  76.     def bounce(self):
  77.         if self.was_last_bounced:
  78.             return
  79.         self.was_last_bounced = True
  80.         self.last_bounce_count = 1
  81.         self.direction_y *= -1
  82.  
  83.     def draw(self):
  84.         super().draw()
  85.  
  86.  
  87. def the_end():
  88.     pygame.quit()
  89.     sys.exit()
  90.  
  91.  
  92. def main():
  93.     sticks = []
  94.     balls = []
  95.     pygame.init()
  96.     window_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
  97.  
  98.     pygame.display.set_caption('Simple pong!')
  99.     WHITE = (255, 255, 255)
  100.  
  101.     window_surface.fill(WHITE)
  102.     stick_image = pygame.image.load('stick.png')
  103.     upper_stick = Stick(window_surface, SCREEN_WIDTH / 2 - STICK_WIDTH / 2, SCREEN_Y_MARGIN, stick_image)
  104.     lower_stick = Stick(window_surface, SCREEN_WIDTH / 2 - STICK_WIDTH / 2, SCREEN_HEIGHT - SCREEN_Y_MARGIN,
  105.                         stick_image)
  106.     sticks.append(upper_stick)
  107.     sticks.append(lower_stick)
  108.  
  109.     ball_image = pygame.image.load('ball.png')
  110.     balls.append(Ball(window_surface, SCREEN_WIDTH / 2, SCREEN_Y_MARGIN, ball_image))
  111.     balls.append(Ball(window_surface, SCREEN_WIDTH / 2, SCREEN_HEIGHT - SCREEN_Y_MARGIN, ball_image))
  112.  
  113.     for stick in sticks:
  114.         stick.draw()
  115.     # pygame.draw.circle(window_surface, BLUE, (300, 50), 20, 0)
  116.     # pygame.draw.ellipse(window_surface, RED, (300, 250, 40, 80), 1)
  117.     pygame.display.update()
  118.     tick_count = 0
  119.     ticker = pygame.time.Clock()
  120.     while True:
  121.         if pygame.key.get_pressed()[pygame.K_ESCAPE]:
  122.             print("Esc")
  123.             the_end()
  124.         update = False
  125.         tick_count += ticker.tick()
  126.         if tick_count > BALL_SLOWNESS:
  127.             for ball in balls:
  128.                 ball.move()
  129.             update = True
  130.             tick_count = 0
  131.         if pygame.key.get_pressed()[pygame.K_LEFT]:
  132.             update = True
  133.             lower_stick.move(-1, 0)
  134.  
  135.         if pygame.key.get_pressed()[pygame.K_RIGHT]:
  136.             update = True
  137.             lower_stick.move(1, 0)
  138.  
  139.         if pygame.key.get_pressed()[pygame.K_a]:
  140.             update = True
  141.             upper_stick.move(-1, 0)
  142.  
  143.         if pygame.key.get_pressed()[pygame.K_d]:
  144.             update = True
  145.             upper_stick.move(1, 0)
  146.  
  147.         if update:
  148.             for stick in sticks:
  149.                 for ball in balls:
  150.                     if stick.close_to(ball):
  151.                         ball.bounce()
  152.             for stick in sticks:
  153.                 stick.draw()
  154.             for ball in balls:
  155.                 ball.draw()
  156.             pygame.display.update()
  157.  
  158.         for event in pygame.event.get():
  159.             if event.type == QUIT:
  160.                 the_end()
  161.  
  162.  
  163. if __name__ == "__main__":
  164.     main()
Advertisement
Add Comment
Please, Sign In to add comment