Advertisement
Ilikebugs

ICS 11 Summative

Apr 29th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.11 KB | None | 0 0
  1. '''File name: ICS 11 Summative
  2. Author: Rayton Lin
  3. Teacher: Mr.Saleem
  4. Date created: April 29th, 2019
  5. Purpose: Create a game using pygame.
  6. '''
  7. import pygame,random
  8. #Create the necessary classes and functions
  9. class Block(pygame.sprite.Sprite): #Class for the blocks that will be broken in each level
  10. #Constructor
  11. def __init__(self,color):
  12. super().__init__() #Call the Parent class constructor
  13.  
  14. #set the two mandatory attributes for a sprite
  15. self.image=pygame.Surface([24,20])
  16. self.image.fill(color)
  17. self.rect=self.image.get_rect()
  18.  
  19. class Ball(pygame.sprite.Sprite): #Class for the ball that will break the blocks.
  20. #Constructor
  21. def __init__(self,color):
  22. super().__init__() #Call the Parent class constructor
  23.  
  24. #set the two mandatory attributes for a sprite
  25. self.image=pygame.Surface([20,20])
  26. self.image.fill(color)
  27. self.rect=self.image.get_rect()
  28.  
  29. class Paddle(pygame.sprite.Sprite): #Class for the paddle that the player uses to ensure the ball doesn't go past the bottom of the screen.
  30. #Constructor
  31. def __init__(self,color):
  32. super().__init__() #Call the Parent class constructor
  33.  
  34. #set the two mandatory attributes for a sprite
  35. self.image=pygame.Surface([80,20])
  36. self.image.fill(color)
  37. self.rect=self.image.get_rect()
  38.  
  39. def paddleCollision(tx1,ty1,tx2,ty2,px1,py1,px2,py2): #Function to detect if the cube ball and paddle have collided
  40. if px2<tx1 or tx2<px1 or py2<ty1 or ty2<py1: #If the cube ball is the the right, left, bottom or top of the paddle, they haven't collided
  41. return False
  42. else:
  43. return True
  44.  
  45. #Define colors and screen size
  46. red=(255,0,0)
  47. green=(0,255,0)
  48. lgreen=(193,255,193) #Light Green
  49. blue=(0,0,255)
  50. white=(255,255,255)
  51. black=(0,0,0)
  52. #The above are the colours that will be used for the game
  53.  
  54.  
  55. size=width,height=600,600
  56. clock=pygame.time.Clock()
  57.  
  58. #Initialize pygame and create screen
  59. pygame.init()
  60. screen=pygame.display.set_mode(size)
  61.  
  62. #Define necessary variables
  63. blockCurrentColour=[0,0,255] #Colour of the blocks
  64. lives=3 #The number of lives that the player has throughout the game.
  65. score=0 #The score over the course of the game
  66. framesPlayed=0 #Tracks the number of frames that has been played. 60 frames=1 second.
  67.  
  68.  
  69. def firstScreen():
  70. font = pygame.font.Font(None, 35)
  71. goToLevel1=True
  72. run=True
  73. while run:
  74. for event in pygame.event.get():
  75. if event.type==pygame.QUIT:
  76. run=False
  77. goToLevel1=False
  78. if event.type==pygame.MOUSEBUTTONDOWN:
  79. run=False
  80. screen.fill(white)
  81. textImage = font.render("The goal of this game is to keep a ball bouncing", True, black)
  82. screen.blit(textImage,[0,00])
  83. textImage = font.render("using a paddle and the walls to hit and break all", True, black)
  84. screen.blit(textImage,[0,50])
  85. textImage = font.render("the blocks on the screen and move onto the next", True, black)
  86. screen.blit(textImage,[0,100])
  87. textImage = font.render("level.", True, black)
  88. screen.blit(textImage,[0,150])
  89. textImage = font.render("You have 3 lives, and you lose a life", True, black)
  90. screen.blit(textImage,[0,250])
  91. textImage = font.render("if you let a ball touch the bottom of the screen.", True, black)
  92. screen.blit(textImage,[0,300])
  93. textImage = font.render("To enter the game, press a mouse button.", True, black)
  94. screen.blit(textImage,[0,400])
  95. pygame.display.flip()
  96. clock.tick(60)
  97. if goToLevel1:
  98. level1()
  99. else:
  100. pygame.quit()
  101.  
  102.  
  103. def level1():
  104. global lives,score,framesPlayed #Makes the variables global so you can update them from inside the function
  105. remainingMobs=10 #Stores the number of remaining blocks to determine if the level is completed
  106.  
  107. #Create sprite groups and some block objects
  108. allSprites=pygame.sprite.Group()
  109. mobs=pygame.sprite.Group()
  110. player=Paddle(red)
  111. allSprites.add(player)
  112. ball=Ball(black)
  113. allSprites.add(ball)
  114. ballxspeed=3
  115. ballyspeed=3
  116. #Create 10 mobs and add them to the sprites' group
  117. for i in range(10):
  118. mob=Block(blockCurrentColour)
  119. blockCurrentColour[2]-=25
  120. if blockCurrentColour[2]<100:
  121. blockCurrentColour[2]=255
  122. mob.rect.x=200+20*i
  123. mob.rect.y=50
  124.  
  125. allSprites.add(mob)
  126. mobs.add(mob)
  127.  
  128.  
  129.  
  130. font=pygame.font.Font(None,20)
  131.  
  132. #Create variables determining the next screen
  133. goToLevel2=False
  134. goToLoseScreen=False
  135.  
  136. #main game loop
  137. run=True
  138. while run:
  139. #event loop
  140. for event in pygame.event.get():
  141. if event.type==pygame.QUIT:
  142. run=False
  143. if event.type==pygame.KEYDOWN:
  144. if event.key==pygame.K_p:#Skips the level to go to level 2
  145. goToLevel2=True
  146. run=False
  147. if event.key==pygame.K_w or event.key==pygame.K_UP:
  148. ballyspeed-=1
  149. if event.key==pygame.K_s or event.key==pygame.K_DOWN:
  150. ballyspeed+=1
  151. if event.key==pygame.K_d or event.key==pygame.K_RIGHT:
  152. ballxspeed+=1
  153. if event.key==pygame.K_a or event.key==pygame.K_LEFT:
  154. ballxspeed-=1
  155.  
  156. #game logic
  157. framesPlayed+=1
  158. mouseX=pygame.mouse.get_pos()[0]
  159. mouseY=height-20
  160. ball.rect.x+=ballxspeed
  161. ball.rect.y+=ballyspeed
  162. if ball.rect.right>=width or ball.rect.left<=0:
  163. ballxspeed*=(-1) #The ball will bounce off the wall
  164. if ball.rect.top<=0:
  165. ballyspeed*=(-1)
  166. if paddleCollision(player.rect.x,player.rect.y,player.rect.right,player.rect.bottom,ball.rect.x,ball.rect.y,ball.rect.right,ball.rect.bottom):
  167. ballyspeed=-abs(ballyspeed)
  168.  
  169. if ball.rect.y>player.rect.bottom:
  170. lives-=1
  171. ball.rect.x=0
  172. ball.rect.y=0
  173. ballxspeed=3
  174. ballyspeed=3
  175. player.rect.centerx=mouseX
  176. player.rect.centery=mouseY
  177. mob_hit_list=pygame.sprite.spritecollide(ball,mobs,True)
  178. for mob in range(len(mob_hit_list)):
  179. remainingMobs-=1
  180. score+=1
  181. if mob==0:
  182. ballyspeed*=(-1)
  183. if lives<=0:
  184. goToLoseScreen=True
  185. run=False
  186. elif remainingMobs==0:
  187. goToLevel2=True
  188. run=False
  189. #draw section
  190. screen.fill(lgreen)
  191.  
  192.  
  193. textImage=font.render("Current Score: %s"%(score),True,black)
  194. screen.blit(textImage,[0,0])
  195. textImage=font.render("Current Level: 1",True,black)
  196. screen.blit(textImage,[225,0])
  197. textImage=font.render("Current Number of Lives: %s"%(lives),True,black)
  198. screen.blit(textImage,[400,0])
  199. textImage=font.render("Time Played: %s seconds"%(framesPlayed//60),True,black)
  200. screen.blit(textImage,[0,10])
  201. allSprites.draw(screen)
  202.  
  203. pygame.display.flip()
  204. clock.tick(60)
  205. if goToLevel2:
  206. score=score
  207. level2()
  208. elif goToLoseScreen:
  209. loseScreen()
  210. else:
  211. pygame.quit()
  212.  
  213. def level2():
  214. global lives,score,framesPlayed #Makes the variables global so you can update them from inside the function
  215. remainingMobs=50 #Stores the number of remaining blocks to determine if the level is completed
  216.  
  217. #create sprite groups and some block objects
  218. allSprites=pygame.sprite.Group()
  219. mobs=pygame.sprite.Group()
  220. player=Paddle(red)
  221. allSprites.add(player)
  222. ball=Ball(black)
  223. ball.rect.x=0
  224. ball.rect.y=400
  225. allSprites.add(ball)
  226. ballxspeed=3
  227. ballyspeed=3
  228. #Create 50 mobs and add them to the sprites' group
  229. for i in range(50):
  230. mob=Block(blockCurrentColour)
  231. if i<25:
  232. mob.rect.x=(i) * 24
  233. mob.rect.y=30
  234. elif i<31:
  235. mob.rect.x=(i-25) * 24
  236. mob.rect.y=70
  237. elif i<37:
  238. mob.rect.x=600-((i-30) * 24)
  239. mob.rect.y=70
  240. else:
  241. mob.rect.x=(i-31)%24 * 24
  242. mob.rect.y=110
  243. blockCurrentColour[2]-=25
  244. if blockCurrentColour[2]<100:
  245. blockCurrentColour[2]=255
  246. allSprites.add(mob)
  247. mobs.add(mob)
  248.  
  249. font=pygame.font.Font(None,20)
  250.  
  251. #Create variables determining the next screen
  252. goToWinScreen=False
  253. goToLoseScreen=False
  254.  
  255. #main game loop
  256. run=True
  257. while run:
  258. #event loop
  259. for event in pygame.event.get():
  260. if event.type==pygame.QUIT:
  261. run=False
  262. if event.type==pygame.KEYDOWN:
  263. if event.key==pygame.K_w or event.key==pygame.K_UP:
  264. ballyspeed-=1
  265. if event.key==pygame.K_s or event.key==pygame.K_DOWN:
  266. ballyspeed+=1
  267. if event.key==pygame.K_d or event.key==pygame.K_RIGHT:
  268. ballxspeed+=1
  269. if event.key==pygame.K_a or event.key==pygame.K_LEFT:
  270. ballxspeed-=1
  271. #game logic
  272. framesPlayed+=1
  273. mouseX=pygame.mouse.get_pos()[0]
  274. mouseY=height-20
  275. ball.rect.x+=ballxspeed
  276. ball.rect.y+=ballyspeed
  277. if ball.rect.right>=width or ball.rect.left<=0:
  278. ballxspeed*=(-1) #The ball will bounce off the wall
  279. if ball.rect.top<=0:
  280. ballyspeed*=(-1)
  281. if paddleCollision(player.rect.x,player.rect.y,player.rect.right,player.rect.bottom,ball.rect.x,ball.rect.y,ball.rect.right,ball.rect.bottom):
  282. ballyspeed=-abs(ballyspeed)
  283.  
  284. if ball.rect.y>height:
  285. lives-=1
  286. ball.rect.x=0
  287. ball.rect.y=400
  288. ballxspeed=3
  289. ballyspeed=3
  290. player.rect.centerx=mouseX
  291. player.rect.centery=mouseY
  292. mob_hit_list=pygame.sprite.spritecollide(ball,mobs,True)
  293. for mob in range(len(mob_hit_list)):
  294. remainingMobs-=1
  295. score+=1
  296. if mob==0:
  297. ballyspeed*=(-1)
  298.  
  299. if lives==0:
  300. goToLoseScreen=True
  301. run=False
  302. elif remainingMobs==0:
  303. goToWinScreen=True
  304. run=False
  305. #draw section
  306. screen.fill(lgreen)
  307. textImage=font.render("Current Score: %s"%(score),True,black)
  308. screen.blit(textImage,[0,0])
  309. textImage=font.render("Current Level: 2",True,black)
  310. screen.blit(textImage,[225,0])
  311. textImage=font.render("Current Number of Lives: %s"%(lives),True,black)
  312. screen.blit(textImage,[400,0])
  313. textImage=font.render("Time Played: %s seconds"%(framesPlayed//60),True,black)
  314. screen.blit(textImage,[0,10])
  315. allSprites.draw(screen)
  316.  
  317. pygame.display.flip()
  318. clock.tick(60)
  319. if goToWinScreen:
  320. winScreen()
  321. elif goToLoseScreen:
  322. loseScreen()
  323. else:
  324. pygame.quit()
  325.  
  326. def winScreen():
  327. global lives
  328. global score
  329. global framesPlayed
  330. font = pygame.font.Font(None, 35)
  331. goToLevel1=True
  332. run=True
  333. while run:
  334. for event in pygame.event.get():
  335. if event.type==pygame.QUIT:
  336. run=False
  337. goToLevel1False
  338. if event.type==pygame.MOUSEBUTTONDOWN:
  339. run=False
  340. screen.fill(green)
  341. textImage = font.render("YOU HAVE WON THE GAME! Final Score: %s" %(score), True, black)
  342. screen.blit(textImage,[50,200])
  343. textImage = font.render("Final Time: %s seconds" %(framesPlayed//60), True, black)
  344. screen.blit(textImage,[175,300])
  345. textImage = font.render("To replay the game, press a mouse button.", True, black)
  346. screen.blit(textImage,[50,400])
  347. pygame.display.flip()
  348. clock.tick(60)
  349. if goToLevel1:
  350. lives=3
  351. score=0
  352. level1()
  353. else:
  354. pygame.quit()
  355.  
  356. def loseScreen():
  357. global lives
  358. global score
  359. global framesPlayed
  360. font = pygame.font.Font(None, 35)
  361. goToLevel1=True
  362. run=True
  363. while run:
  364. for event in pygame.event.get():
  365. if event.type==pygame.QUIT:
  366. run=False
  367. goToLevel1=False
  368. if event.type==pygame.MOUSEBUTTONDOWN:
  369. run=False
  370. screen.fill(red)
  371. textImage = font.render("YOU HAVE LOST THE GAME! Final Score: %s" %(score), True, black)
  372. screen.blit(textImage,[50,200])
  373. textImage = font.render("Final Time: %s seconds" %(framesPlayed//60), True, black)
  374. screen.blit(textImage,[175,300])
  375. textImage = font.render("To replay the game, press a mouse button.", True, black)
  376. screen.blit(textImage,[50,400])
  377. pygame.display.flip()
  378. clock.tick(60)
  379. if goToLevel1:
  380. lives=3
  381. score=0
  382. framesPlayed=0
  383. level1()
  384. else:
  385. pygame.quit()
  386. firstScreen()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement