Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. import random
  4. import math
  5.  
  6. #--- Global constants ---
  7. BLACK = (0, 0, 0)
  8. WHITE = (255, 255, 255)
  9. GREEN = (0, 255, 0)
  10. RED = (255, 0, 0)
  11.  
  12. SCREEN_WIDTH = 700
  13. SCREEN_HEIGHT = 500
  14. WALL_HEIGHT = SCREEN_HEIGHT - 25
  15.  
  16. #set score to 0
  17. playerscore = 0
  18. aiscore = 0
  19. pygame.init()
  20.  
  21.  
  22.  
  23.  
  24. # Set the width and height of the screen [width, height]
  25. size = (700, 500)
  26. screen = pygame.display.set_mode(size)
  27.  
  28. pygame.display.set_caption("Packet Pong")
  29.  
  30. # Loop until the user clicks the close button.
  31. done = False
  32.  
  33. # Used to manage how fast the screen updates
  34. clock = pygame.time.Clock()
  35.  
  36. #Classes
  37.  
  38. class Player(pygame.sprite.Sprite):
  39. """ The class is the player-controlled sprite. """
  40.  
  41. # -- Methods
  42. def __init__(self, x, y):
  43. """Constructor function"""
  44. # Call the parent's constructor
  45. pygame.sprite.Sprite.__init__(self)
  46.  
  47. # Set height, width
  48. self.image = pygame.Surface([20, 65])
  49. self.image.fill(WHITE)
  50.  
  51. # Make our top-left corner the passed-in location.
  52. self.rect = self.image.get_rect()
  53. self.rect.x = x
  54. self.rect.y = y
  55.  
  56. # -- Attributes
  57. # Set speed vector
  58. self.change_x = 0
  59. self.change_y = 0
  60.  
  61. def changespeed(self, x, y):
  62. """ Change the speed of the player"""
  63. self.change_y += y
  64.  
  65. def update(self):
  66. """ Find a new position for the player"""
  67.  
  68. self.rect.y += self.change_y
  69. if self.rect.bottom >= (WALL_HEIGHT + 3) or self.rect.top <= 24:
  70. self.rect.y = self.rect.y - self.change_y
  71.  
  72. class Ball(pygame.sprite.Sprite):
  73. def __init__(self):
  74. pygame.sprite.Sprite.__init__(self)
  75. self.rect = Rect(200,200,20,20)
  76. circ_sur = pygame.Surface((15,15))
  77. self.image = pygame.draw.circle(circ_sur,(RED),(15/2,15/2),15/2)
  78. self.image = circ_sur.convert()
  79. self.image.set_colorkey((0,0,0))
  80. self.velx, self.vely = 5, 5
  81.  
  82. def draw(self):
  83. self.screen.blit(self.image, self.rect)
  84.  
  85. def reset(self):
  86. # to center
  87. self.rect.y = WALL_HEIGHT // 2
  88. self.rect.x = SCREEN_WIDTH // 2
  89. self.velx, self.vely = 5, 5
  90.  
  91. def update(self):
  92. global aiscore
  93. global playerscore
  94. # move
  95. self.rect.x += self.velx
  96. self.rect.y += self.vely
  97.  
  98. # bounce walls
  99. if self.rect.top <= 25:
  100. self.vely *= -1
  101. elif self.rect.bottom >= WALL_HEIGHT:
  102. self.vely *= -1
  103.  
  104. elif pygame.sprite.spritecollide(ai, ball_sprites_list, False):
  105. self.velx *= -1
  106. self.vely *= -1
  107.  
  108. elif pygame.sprite.spritecollide(player, ball_sprites_list, False):
  109. self.velx *= -1
  110. self.vely *= -1
  111.  
  112. # offscreen
  113. elif self.rect.x <= 0:
  114. aiscore = aiscore + 1
  115. self.reset()
  116. elif self.rect.x >= SCREEN_WIDTH:
  117. playerscore = playerscore + 1
  118. self.reset()
  119.  
  120. class AI(pygame.sprite.Sprite):
  121. """ The class is the computer-controlled sprite. """
  122.  
  123. # -- Methods
  124. def __init__(self, x, y):
  125. global vely
  126. """Constructor function"""
  127. # Call the parent's constructor
  128. pygame.sprite.Sprite.__init__(self)
  129.  
  130. # Set height, width
  131. self.image = pygame.Surface([20, 65])
  132. self.image.fill(WHITE)
  133.  
  134. # Make our top-left corner the passed-in location.
  135. self.rect = self.image.get_rect()
  136. self.rect.x = x
  137. self.rect.y = y
  138.  
  139. def update(self, ball):
  140. """ Find a new position for the paddle"""
  141. # Set speed vector
  142. self.change_x = 0
  143. self.change_y = ball.vely
  144. if self.rect.y == 3.1:
  145. self.rect.y = 3
  146. if self.rect.y == -3.1:
  147. self.rect.y = -3
  148. self.rect.y += self.change_y
  149. if self.rect.bottom >= (WALL_HEIGHT + 3) or self.rect.top <= 24:
  150. self.rect.y = self.rect.y - self.change_y
  151.  
  152. class Walltop(pygame.sprite.Sprite):
  153. """ The class is the wall sprite. """
  154.  
  155. # -- Methods
  156. def __init__(self, x, y):
  157. """Constructor function"""
  158. # Call the parent's constructor
  159. pygame.sprite.Sprite.__init__(self)
  160.  
  161. # Set height, width
  162. self.image = pygame.Surface([SCREEN_WIDTH, 25])
  163. self.image.fill(GREEN)
  164.  
  165. # Make our top-left corner the passed-in location.
  166. self.rect = self.image.get_rect()
  167. self.rect.x = x
  168. self.rect.y = y
  169.  
  170. def update(self):
  171. """ Do not move"""
  172. self.rect.x = self.rect.x
  173. self.rect.y = self.rect.y
  174.  
  175. class Wallbottom(pygame.sprite.Sprite):
  176. """ The class is the wall sprite. """
  177.  
  178. # -- Methods
  179. def __init__(self, x, y):
  180. """Constructor function"""
  181. # Call the parent's constructor
  182. pygame.sprite.Sprite.__init__(self)
  183.  
  184. # Set height, width
  185. self.image = pygame.Surface([SCREEN_WIDTH, 25])
  186. self.image.fill(GREEN)
  187.  
  188. # Make our top-left corner the passed-in location.
  189. self.rect = self.image.get_rect()
  190. self.rect.x = x
  191. self.rect.y = y
  192.  
  193. def update(self):
  194. """ Do not move"""
  195. self.rect.x = self.rect.x
  196. self.rect.y = self.rect.y
  197.  
  198. # Create sprite lists
  199. all_sprites_list = pygame.sprite.Group()
  200. walls_sprites_list = pygame.sprite.Group()
  201. ball_sprites_list = pygame.sprite.Group()
  202. # Create the sprites
  203. player = Player(0, 250)
  204. all_sprites_list.add(player)
  205.  
  206. ai = AI(680, 250)
  207. all_sprites_list.add(ai)
  208.  
  209. walltop = Walltop(0, 0)
  210. all_sprites_list.add(walltop)
  211. walls_sprites_list.add(walltop)
  212.  
  213. wallbottom = Wallbottom(0, WALL_HEIGHT)
  214. all_sprites_list.add(wallbottom)
  215. walls_sprites_list.add(wallbottom)
  216.  
  217. ball = Ball()
  218. ball_sprites_list.add(ball)
  219. all_sprites_list.add(ball)
  220.  
  221. # -------- Main Program Loop -----------
  222. while not done:
  223. # --- Main event loop
  224. for event in pygame.event.get():
  225. if event.type == pygame.QUIT:
  226. done = True
  227.  
  228. elif event.type == pygame.KEYDOWN:
  229. if event.key == pygame.K_UP:
  230. player.changespeed(0, -3)
  231. elif event.key == pygame.K_DOWN:
  232. player.changespeed(0, 3)
  233.  
  234. elif event.key == pygame.K_SPACE:
  235. ball.reset()
  236.  
  237. # Reset speed when key goes up
  238. elif event.type == pygame.KEYUP:
  239. if event.key == pygame.K_UP:
  240. player.changespeed(0, 3)
  241. elif event.key == pygame.K_DOWN:
  242. player.changespeed(0, -3)
  243.  
  244. screen.fill(BLACK)
  245.  
  246. #Draw player score
  247. font1 = pygame.font.Font(None, 50)
  248. text1 = font1.render("%d"%playerscore, True, WHITE)
  249. textpos1 = (100, 200)
  250. screen.blit(text1, textpos1)
  251.  
  252. #Draw AI score
  253. font2 = pygame.font.Font(None, 50)
  254. text2 = font1.render("%d"%aiscore, True, WHITE)
  255. textpos2 = (600, 200)
  256. screen.blit(text2, textpos2)
  257.  
  258. pygame.draw.line(screen, (GREEN), ((SCREEN_WIDTH//2), 25), (350, WALL_HEIGHT))
  259.  
  260. #Update
  261. player.update()
  262. ai.update(ball)
  263. ball.update()
  264. all_sprites_list.draw(screen)
  265. pygame.display.flip()
  266.  
  267. # --- Limit to 60 frames per second
  268. clock.tick(60)
  269.  
  270. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement