Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. bg="bg.jpg"
  2. ball="ball.png"
  3.  
  4. import pygame, time, sys
  5.  
  6. from pygame.locals import *
  7.  
  8. pygame.init()
  9. screen = pygame.display.set_mode((600,400),0,32)
  10. pygame.display.set_caption('Breakout')
  11.  
  12. background = pygame.image.load(bg).convert()
  13. ball = pygame.image.load(ball).convert_alpha()
  14. ballRect = ball.get_rect()
  15.  
  16. #set up a list for speed variable
  17. speed = [1,1]
  18. #create a variable for black
  19. black = 0, 0, 0
  20.  
  21. p1Paddle = pygame.Rect (10, 370, 50, 10)
  22. PADDLE_COLOR = pygame.color.Color("red")
  23. paddleSpeedX = 0
  24. #Game Loop
  25.  
  26. while True:
  27. for event in pygame.event.get():
  28. if event.type==QUIT:
  29. pygame.quit()
  30. sys.exit()
  31. #moving the paddle left and right
  32. if event.type == KEYDOWN:
  33. if event.key == K_LEFT and p1Paddle.left >0:
  34. paddleSpeedX = -2
  35. elif event.key == K_RIGHT and p1Paddle.right <600:
  36. paddleSpeedX = 2
  37. else:
  38. paddleSpeedX = 0
  39.  
  40.  
  41. p1Paddle.move_ip(paddleSpeedX, 0)
  42.  
  43.  
  44.  
  45. # move object
  46. ballRect.move_ip(speed)
  47. #check if ball touches edge
  48. if ballRect.left<0 or ballRect.right > 600:
  49. #reverse direction but keep moving
  50. speed[0] = -speed[0]
  51. if ballRect.top < 0 or ballRect.bottom > 400:
  52. speed[1] = -speed[1]
  53. #collision with paddle
  54. if p1Paddle.colliderect(ballRect) and speed[1] > 0:
  55. speed[1] = -speed[1]
  56. time.sleep(0.005)
  57. screen.blit(background, (0,0))
  58. screen.blit(ball, ballRect)
  59. screen.fill(PADDLE_COLOR, p1Paddle)
  60.  
  61. pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement