Advertisement
Psycho_Coder

Very Simple Pong Game in Python using PyGame

Nov 20th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. import random
  2.  
  3. import pygame
  4.  
  5.  
  6. pygame.init()
  7. screen = pygame.display.set_mode( ( 640, 480 ) )
  8. screen.fill( ( 255, 255, 255 ) )
  9. balx, baly = ( 320, 240 )
  10. balxspeed, balyspeed = random.choice( [( 1, 1 ), ( -1, 1 ), ( 1, -1 ), ( -1, -1 )] )
  11. fps = pygame.time.Clock()
  12. yleft = 240
  13. yright = 240
  14.  
  15. right_score = 0
  16. left_score = 0
  17.  
  18. while True:
  19.     eventlst = pygame.event.get()
  20.     for event in eventlst:
  21.         if event.type == pygame.QUIT:
  22.             raise SystemExit
  23.  
  24.     #  move the ball
  25.     balx += balxspeed
  26.     baly += balyspeed
  27.  
  28.     #  if the ball hits 0-up or 480-down it bounces back
  29.     if baly >= 480 or baly <= 0:
  30.         balyspeed *= -1
  31.  
  32.     #  score board
  33.     if balx <= 0:
  34.         right_score += 1
  35.         #  print "left", left_score, "-", right_score, "right"
  36.     if balx >= 640:
  37.         left_score += 1
  38.         #  print "left", left_score, "-", right_score, "right"
  39.  
  40.     #  if the ball hits 0-left or 640-right it resets the ball
  41.     if balx >= 640 or balx <= 0:
  42.         balx, baly = ( 320, 240 )
  43.         balxspeed, balyspeed = random.choice( [( 1, 1 ), ( -1, 1 ), ( 1, -1 ), ( -1, -1 )] )
  44.  
  45.     keys = pygame.key.get_pressed()
  46.  
  47.     #  move the rectangle up or down
  48.     if keys[pygame.K_DOWN]:
  49.         yright += 2
  50.     if keys[pygame.K_UP]:
  51.         yright -= 2
  52.     if keys[pygame.K_w]:
  53.         yleft -= 2
  54.     if keys[pygame.K_s]:
  55.         yleft += 2
  56.  
  57.     #  make sure the rectangle yleft doesn't go further up or down than either 0 or 480
  58.     if yleft <= 0:
  59.         yleft = 0
  60.     if yleft >= 430:
  61.         yleft = 430
  62.     #  make sure the rectangle yright doesn't go further up or down than either 0 or 480
  63.     if yright <= 0:
  64.         yright = 0
  65.     if yright >= 430:
  66.         yright = 430
  67.  
  68.     if balx <= 10:
  69.         #  if ball hits rectangle do bounce
  70.         if baly >= yleft and baly <= yleft + 50:
  71.             balxspeed *= -1.5
  72.             balx = 11
  73.             if keys[pygame.K_w]:
  74.                 balyspeed = -abs( balyspeed )
  75.             if keys[pygame.K_s]:
  76.                 balyspeed = abs( balyspeed )
  77.  
  78.     if balx >= 630:
  79.         #  if ball hits rectangle do bounce
  80.         if baly >= yright and baly <= yright + 50:
  81.             balxspeed *= -1.5
  82.             balx = 629
  83.             if keys[pygame.K_UP]:
  84.                 balyspeed = -abs( balyspeed )
  85.             if keys[pygame.K_DOWN]:
  86.                 balyspeed = abs( balyspeed )
  87.  
  88.     screen.fill( ( 255, 255, 255 ) )
  89.     pygame.draw.circle( screen, ( 255, 0, 0 ), ( int( balx ), int( baly ) ), 8 )
  90.     pygame.draw.rect( screen, ( 0, 0, 0 ), ( 0, yleft, 10, 50 ) )
  91.     pygame.draw.rect( screen, ( 0, 0, 0 ), ( 630, yright, 10, 50 ) )
  92.     pygame.display.update()
  93.     fps.tick( 60 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement