Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. import sys
  2. import pygame
  3. from pygame import Rect
  4. import time
  5.  
  6. pygame.init()
  7. clock = pygame.time.Clock()
  8.  
  9. # Screen
  10. width = 800
  11. height = 600
  12. size = (width, height)
  13. screen = pygame.display.set_mode(size)
  14.  
  15. # Colors
  16. black = (0, 0, 0)
  17. gray = (128, 128, 128)
  18. white = (255, 255, 255)
  19. green = (0, 255, 0)
  20. blue = (0, 0, 128)
  21.  
  22. # Paddle
  23. paddlerect = Rect(350, 550, 100, 15) # left, top, width, height
  24.  
  25. # Ball
  26. ball_size = 30
  27. ball = pygame.transform.scale(pygame.image.load("images/intro_ball.gif"), (ball_size, ball_size))
  28. ballrect = ball.get_rect()
  29. speed_x = 5
  30. speed_y = 5
  31.  
  32. # Sounds
  33. pop = pygame.mixer.Sound('sounds/pop.wav')
  34. boing = pygame.mixer.Sound('sounds/boing.wav')
  35. bonk = pygame.mixer.Sound('sounds/bonk.wav')
  36.  
  37. # Custom events
  38. INTENSIFY = pygame.USEREVENT + 1
  39.  
  40. # Timer
  41. pygame.time.set_timer(INTENSIFY, 5000)
  42.  
  43.  
  44. def game_over():
  45. bonk.play()
  46.  
  47. font = pygame.font.Font('freesansbold.ttf', 32)
  48.  
  49. # create a text suface object,
  50. # on which text is drawn on it.
  51. text = font.render('GAME OVER !!', True, green, blue)
  52.  
  53. # create a rectangular object for the
  54. # text surface object
  55. textRect = text.get_rect()
  56.  
  57. # set the center of the rectangular object.
  58. textRect.center = (width // 2, height // 2)
  59.  
  60. # copying the text surface object
  61. # to the display surface object
  62. # at the center coordinate.
  63. screen.blit(text, textRect)
  64.  
  65. pygame.display.update()
  66.  
  67. while 1:
  68. for event in pygame.event.get():
  69. if event.type == pygame.QUIT:
  70. sys.exit()
  71.  
  72.  
  73. while 1:
  74. for event in pygame.event.get():
  75. if event.type == pygame.QUIT:
  76. sys.exit()
  77. elif event.type == INTENSIFY:
  78. paddlerect = paddlerect.inflate(-5, 0)
  79.  
  80. # if event.type == pygame.KEYDOWN:
  81. # if pygame.key.get_pressed()[pygame.K_a] or pygame.key.get_pressed()[pygame.K_LEFT]:
  82. # # speed_x -= 1
  83. # pass
  84.  
  85. # Handle left/right keypresses
  86. if pygame.key.get_pressed()[pygame.K_a] or pygame.key.get_pressed()[pygame.K_LEFT]:
  87. # speed_x -= 1
  88. paddlerect = paddlerect.move(-10, 0)
  89. if paddlerect.left < 0:
  90. paddlerect.left = 0
  91. elif pygame.key.get_pressed()[pygame.K_d] or pygame.key.get_pressed()[pygame.K_RIGHT]:
  92. # speed_x += 1
  93. paddlerect = paddlerect.move(10, 0)
  94. if paddlerect.right > width:
  95. paddlerect.right = width
  96.  
  97. # Update ball position
  98. ballrect = ballrect.move(speed_x, speed_y)
  99.  
  100. # Bounce off walls
  101. if ballrect.left < 0 or ballrect.right > width:
  102. speed_x = -speed_x
  103. pop.play()
  104. if ballrect.top < 0 or ballrect.bottom > height:
  105. speed_y = -speed_y
  106. pop.play()
  107.  
  108. # game over!
  109. if ballrect.bottom > height:
  110. game_over()
  111.  
  112. # Bounce off paddle?
  113. if ballrect.colliderect(paddlerect):
  114. speed_y = -speed_y
  115.  
  116. boing.play()
  117.  
  118. # Accelerate!
  119. speed_x = speed_x * 1.001
  120. speed_y = speed_y * 1.001
  121.  
  122. # draw stuff
  123. screen.fill(black)
  124. screen.blit(ball, ballrect)
  125. pygame.draw.rect(screen, white, paddlerect)
  126. pygame.display.flip()
  127.  
  128. millis_since_last_tick = clock.tick(60)
  129. if 1000/millis_since_last_tick < 50:
  130. print(f'Framerate dip: {1000/millis_since_last_tick:.0f}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement