Simple pong game with pygame

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
avgprogramerjoe avgprogramerjoe is offline Offline Aug 14th, 2007, 10:10 pm |
1
This is a simple game of pong with made using pygame. You will need to make a picture of the pong for it to run properly. Simply make a cirlce in paint, 25x25.


Joe
Quick reply to this message  
Python Syntax
  1. #Import and initialize
  2. import pygame, random
  3. from pygame.locals import *
  4.  
  5. global wins, comp, main, deaths, KeepGoing, box_x, box_y
  6.  
  7. pygame.init()
  8.  
  9. KeepGoing = True
  10. deaths = 0
  11. wins = 0
  12. main = 1
  13. comp = 0
  14.  
  15. def check_key():
  16.  
  17. global box_y, main
  18.  
  19. keystate = pygame.key.get_pressed()
  20. if box_y > 0:
  21.  
  22. if keystate[pygame.locals.K_UP]:
  23. box_y -= 5
  24.  
  25. if box_y < 430:
  26.  
  27. if keystate[pygame.locals.K_DOWN]:
  28. box_y += 5
  29.  
  30. if keystate[pygame.locals.K_ESCAPE]:
  31. main = 1
  32.  
  33. def main_seq():
  34. global main, KeepGoing, winning, deaths, wins
  35.  
  36. pygame.init()
  37.  
  38. deaths = 0
  39. wins = 0
  40. color = (200, 200, 150)
  41. display = True
  42.  
  43. screen = pygame.display.set_mode((640,480))
  44. pygame.display.set_caption("Hello, world!")
  45.  
  46. intro = "Welcome to pong, please click here to start."
  47. myFont = pygame.font.SysFont("Comic Sans MS", 15)
  48. label = myFont.render(intro, 1, (255,25,0))
  49.  
  50. background = pygame.Surface(screen.get_size())
  51. background = background.convert()
  52. background.fill((0,0,0))
  53.  
  54. while display:
  55.  
  56. keystate = pygame.key.get_pressed()
  57.  
  58. if keystate[pygame.locals.K_ESCAPE]:
  59. display = False
  60. KeepGoing = False
  61. for event in pygame.event.get():
  62. if event.type == pygame.QUIT:
  63. KeepGoing = False
  64. display = False
  65. winning = False
  66.  
  67. mouse = pygame.mouse.get_pos()
  68.  
  69. if mouse[0] >= 220 and mouse[0] <= 545 and mouse[1] >= 200 and mouse[1] <= 250:
  70.  
  71. if event.type == pygame.MOUSEBUTTONDOWN:
  72. color = (250, 250, 250)
  73. else:
  74. color = (225, 225, 225)
  75.  
  76. if event.type == MOUSEBUTTONUP:
  77. display = False
  78. main = False
  79. main = 0
  80.  
  81. else:
  82. color = (200, 200, 200)
  83.  
  84. bar = pygame.Surface((325, 50))
  85. bar = bar.convert()
  86. bar.fill(color)
  87.  
  88. screen.blit(background, (0,0))
  89. screen.blit(bar, (180, 200))
  90. screen.blit(label, (200, 220))
  91. pygame.display.flip()
  92.  
  93. def winning_game():
  94.  
  95. global winning, comp, main, deaths, box_y, KeepGoing
  96.  
  97. #Display configuration
  98. screen = pygame.display.set_mode((640,480))
  99. pygame.display.set_caption("Hello, world!")
  100.  
  101. #Entities (just background for now)
  102. background = pygame.Surface(screen.get_size())
  103. background = background.convert()
  104. background.fill((0,0,0))
  105.  
  106. box = pygame.Surface((10, 50))
  107. box = box.convert()
  108. box.fill((200,200,150))
  109.  
  110. paddle = pygame.Surface((10, 50))
  111. paddle = paddle.convert()
  112. paddle.fill((200,200,150))
  113.  
  114. pong_blit = pygame.image.load("pong.bmp")
  115. pong_blit = pong_blit.convert()
  116.  
  117. box_x = 625
  118. box_y = 300
  119.  
  120. pong_x = random.randrange(60, 300)
  121. pong_y = random.randrange(60, 400)
  122.  
  123. paddle_x = 20
  124. paddle_y = 0
  125.  
  126. x = 5
  127. y = 5
  128.  
  129. bounce = True
  130.  
  131. text = "Computer: "+ str(deaths) + " User: " + str(wins)
  132. myFont = pygame.font.SysFont("Comic Sans MS", 12)
  133. label = myFont.render(text, 1, (255,255,0))
  134.  
  135. #Assign values to key variables
  136. clock = pygame.time.Clock()
  137. winning = True
  138. dir = 0
  139. counter = 11
  140. pong_dir = pong_y
  141.  
  142. while winning:
  143.  
  144. clock.tick(30)
  145.  
  146. box_x_right = box_x + 10
  147. box_y_bottom = box_y + 50
  148. pong_x_right = pong_x + 25
  149. pong_y_bottom = pong_y + 25
  150.  
  151. check_key()
  152.  
  153. if main == 1:
  154. winning = False
  155. if box_x <= pong_x_right and box_x + 10 >= pong_x_right and box_y <= pong_y_bottom and box_y + 50 >= pong_y_bottom:
  156. x = -x
  157.  
  158. if x < 0:
  159. x -= 0.15
  160. y -= 0.1
  161. else:
  162. x += 0.15
  163. y += 0.1
  164.  
  165. if box_x <= pong_x and box_x + 10 >= pong_x and box_y <= pong_y and box_y + 50 >= pong_y:
  166. x = -x
  167.  
  168. if x < 0:
  169. x -= 1
  170. y -= 0.75
  171. else:
  172. x += 1
  173. y += 0.75
  174.  
  175. if paddle_x <= pong_x_right and paddle_x + 10 >= pong_x_right and paddle_y <= pong_y_bottom and paddle_y + 50 >= pong_y_bottom:
  176. x = -x
  177. faster = ( 1.00 * random.randrange(1, 80))/ 100
  178. faster_1 = ( 1.10 * random.randrange(1, 120))/ 100
  179.  
  180. if x < 0:
  181. x -= faster
  182. y -= faster_1
  183. else:
  184. x += faster
  185. y += faster_1
  186.  
  187. if paddle_x <= pong_x and paddle_x + 10 >= pong_x and paddle_y <= pong_y and paddle_y + 50 >= pong_y:
  188. x = -x
  189.  
  190. if x < 0:
  191. x -= 1
  192. y -= 0.75
  193. else:
  194. x += 1
  195. y += 0.75
  196.  
  197. dir = 0
  198.  
  199. if pong_y >= 455:
  200. y = -y
  201.  
  202. if pong_y <= 0:
  203. y = -y
  204.  
  205. speed = [6, -6]
  206.  
  207. if pong_x > 350 and pong_y <= random.randrange(1, 480):
  208. rand = random.randrange(1, 5)
  209.  
  210. if rand == 3:
  211. counter = -5
  212.  
  213. if pong_x < 300:
  214. counter = 11
  215.  
  216. if counter > 10:
  217.  
  218. if paddle_y > pong_y:
  219.  
  220. if paddle_y > 0 :
  221. dir = speed[1]
  222. elif paddle_y < 0:
  223. dir = 0
  224.  
  225. if paddle_y < pong_y:
  226.  
  227. if paddle_y < 430:
  228. dir = speed[0]
  229. elif paddle_y > 430:
  230. dir = 0
  231.  
  232. elif pong_dir > pong_y and pong_x < 350 and pong_x > 100 and pong_y > 100 and pong_y < 500:
  233. dir = speed[1]
  234.  
  235. elif pong_dir < pong_y and pong_x < 350 and pong_x > 100 and pong_y > 100 and pong_y < 500:
  236. dir = speed[0]
  237. counter += 1
  238. paddle_y += dir
  239.  
  240. if pong_x >= 615:
  241. winning = False
  242. main = 0
  243. comp = 0
  244. elif pong_x <= 0 :
  245. winning = False
  246. main = 0
  247. comp = 1
  248.  
  249. pong_x += x
  250. pong_y += y
  251. pong_dir = pong_y
  252.  
  253. for event in pygame.event.get():
  254.  
  255. if event.type == pygame.QUIT:
  256. KeepGoing = False
  257. winning = False
  258.  
  259.  
  260. screen.blit(background, (0,0))
  261. screen.blit(label, (400,20))
  262. screen.blit(box, (box_x, box_y))
  263. screen.blit(paddle, (paddle_x, paddle_y))
  264. screen.blit(pong_blit, (pong_x, pong_y))
  265. pygame.display.flip()
  266.  
  267.  
  268. def game():
  269.  
  270. global deaths, wins, comp, main, KeepGoing
  271.  
  272. main = 1
  273.  
  274. while KeepGoing:
  275.  
  276. if main == 0:
  277.  
  278. if comp == 0:
  279. deaths += 1
  280. elif comp == 1:
  281. wins += 1
  282.  
  283. elif main == 1:
  284. main_seq()
  285. winning_game()
  286.  
  287. if __name__ == "__main__":
  288.  
  289. game()
0
vegaseat vegaseat is offline Offline | Aug 16th, 2007
Just a note, the use of globals is a bad habit. With a little extra effort you can assign them to function arguments and return statements.
 
0
km0r3 km0r3 is offline Offline | Oct 15th, 2009
Ingenious but very cool! I definitely like this for wasting some time in school ;)
 
0
masterofpuppets masterofpuppets is offline Offline | Oct 17th, 2009
nice program bro, keep up the good work
 
 

Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC