Advertisement
Guest User

run_game.py

a guest
Jun 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.01 KB | None | 0 0
  1. #class imports
  2. from maze import Maze
  3. from player import seeker
  4. from player2 import seeker2
  5. from snitch import snitch
  6.  
  7. #add in the pygame imports
  8. import random, sys, pygame
  9. from pygame.locals import *
  10.  
  11.  
  12. maze = Maze()
  13. seekername = "Seeker 1"
  14. seekername2 = "Seeker 2"
  15. seeker = seeker(seekername, 2, 2)
  16. seeker2 = seeker2(seekername2, 8, 8)
  17. snitch = snitch("X", 5, 5)
  18.  
  19.  
  20.  
  21.  
  22. FPS = 144 # frames per second to update the screen
  23. WINWIDTH = 1000 # width of the program's window, in pixels
  24. WINHEIGHT = 800 # height in pixels
  25. HALF_WINWIDTH = int(WINWIDTH / 2) #you need to know 1/2 sizes so you can
  26. HALF_WINHEIGHT = int(WINHEIGHT / 2) #place things centrally
  27.  
  28. # The total width and height of each tile in pixels.
  29. TILEWIDTH = 32
  30. TILEHEIGHT = 32
  31. TILEFLOORHEIGHT = 32
  32.  
  33. GREEN = (0, 255, 0)
  34. WHITE = (255, 255, 255)
  35. BGCOLOR = GREEN
  36. TEXTCOLOR = WHITE
  37.  
  38. #A dictionary of the images used.
  39.  
  40. IMAGESDICT = {'floor': pygame.image.load("Images/floor.gif"),
  41. 'wall': pygame.image.load("Images/wall.gif"),
  42. 'seeker': pygame.image.load("Images/wizard.gif"),
  43. 'seeker2': pygame.image.load("Images/wizard.gif"),
  44. 'snitch': pygame.image.load("Images/sprout.gif")}
  45.  
  46. TILEMAPPING = { '#':IMAGESDICT['wall'],
  47. ' ':IMAGESDICT['floor'],
  48. seekername:IMAGESDICT['seeker'],
  49. seekername2:IMAGESDICT['seeker2'],
  50. "X": IMAGESDICT['snitch']}
  51.  
  52. pygame.init()
  53. FPSCLOCK = pygame.time.Clock()
  54. DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
  55. pygame.display.set_caption('Quidditch Game')
  56. BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
  57.  
  58. ####################################################################################################
  59.  
  60.  
  61. #Moves the seeker player to the left
  62. def MSLeft():
  63. moveSnitch()#Moves the snitch in a random direction
  64. x = maze.getCharAtPos(seeker.getRow(), seeker.getCol()-1)
  65. if x == "#":
  66. print ("That's a wall.")#If the player tries to move into a wall this is printed
  67. elif x == seekername2:
  68. print ("That's another player.")#If the player tries to move into another player this is printed
  69. else:
  70. if x == "X":
  71. print ("The Golden Snitch has been caught")#Once the player catches the snitch this is printed
  72. eatSprouts()
  73. maze.clearAtPos(seeker.getRow(), seeker.getCol())
  74. seeker.moveLeft()
  75. maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
  76. print (seeker.toString())
  77.  
  78. #Moves the seeker player to the right
  79. def MSRight():
  80. moveSnitch()
  81. x = maze.getCharAtPos(seeker.getRow(), seeker.getCol() + 1)
  82. if x == "#":
  83. print ("That's a wall.")
  84. elif x == seekername2:
  85. print ("That's another player")
  86. else:
  87. if x == "X":
  88. print ("The Golden Snitch has been caught")
  89. eatSprouts()
  90. maze.clearAtPos(seeker.getRow(), seeker.getCol())
  91. seeker.moveRight()
  92. maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
  93. print (seeker.toString())
  94.  
  95.  
  96. def MSUp():
  97. moveSnitch()
  98. x = maze.getCharAtPos(seeker.getRow()-1, seeker.getCol())
  99. if x == "#":
  100. print ("That's a wall!")
  101. elif x == seekername2:
  102. print ("That's another player")
  103. else:
  104. if x == "X":
  105. print ("The Golden Snitch has been caught")
  106. eatSprouts()
  107. maze.clearAtPos(seeker.getRow(), seeker.getCol())
  108. seeker.moveUp()
  109. maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
  110. print (seeker.toString())
  111.  
  112.  
  113. def MSDown():
  114. moveSnitch()
  115. x = maze.getCharAtPos(seeker.getRow()+1, seeker.getCol())
  116. if x == "#":
  117. print ("That's a wall!")
  118. elif x == seekername2:
  119. print ("That's another player")
  120. else:
  121. if x == "X":
  122. print ("The Golden Snitch has been caught")
  123. eatSprouts()
  124. maze.clearAtPos(seeker.getRow(), seeker.getCol())
  125. seeker.moveDown()
  126. maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
  127. print (seeker.toString())
  128.  
  129.  
  130. #seeker 2 MOVEMENT
  131.  
  132. def MS2Left():
  133. moveSnitch()
  134. x = maze.getCharAtPos(seeker2.getRow(), seeker2.getCol()-1)
  135. if x == "#":
  136. print ("That's a wall!")
  137. elif x == seekername:
  138. print ("That's another player")
  139. else:
  140. if x == "X":
  141. print ("The Golden Snitch has been caught")
  142. seeker2.eatSprouts()
  143. eatSprouts()
  144. maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
  145. seeker2.moveLeft()
  146. maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
  147. print (seeker2.toString())
  148.  
  149. def MS2Right():
  150. moveSnitch()
  151. x = maze.getCharAtPos(seeker2.getRow(), seeker2.getCol() + 1)
  152. if x == "#":
  153. print ("That's a wall!")
  154. elif x == seekername:
  155. print ("That's another player")
  156. else:
  157. if x == "X":
  158. print ("The Golden Snitch has been caught")
  159. seeker2.eatSprouts()
  160. eatSprouts()
  161. maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
  162. seeker2.moveRight()
  163. maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
  164. print (seeker2.toString())
  165.  
  166.  
  167.  
  168.  
  169. def MS2Up():
  170. moveSnitch()
  171. x = maze.getCharAtPos(seeker2.getRow()-1, seeker2.getCol())
  172. if x == "#":
  173. print ("That's a wall!")
  174. elif x == seekername:
  175. print ("That's another player")
  176. else:
  177. if x == "X":
  178. print ("The Golden Snitch has been caught")
  179. seeker2.eatSprouts()
  180. eatSprouts()
  181. maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
  182. seeker2.moveUp()
  183. maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
  184. print (seeker2.toString())
  185.  
  186. def MS2Down():
  187. moveSnitch()
  188. x = maze.getCharAtPos(seeker2.getRow()+1, seeker2.getCol())
  189. if x == "#":
  190. print ("That's a wall!")
  191. elif x == seekername:
  192. print ("That's another player")
  193. else:
  194. if x == "X":
  195. print ("The Golden Snitch has been caught")
  196. seeker2.eatSprouts()
  197. eatSprouts()
  198. maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
  199. seeker2.moveDown()
  200. maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
  201. print (seeker2.toString())
  202.  
  203. #######################################################################################
  204.  
  205. #This function moves the snitch in a random direction
  206. def moveSnitch():
  207. direction = random.randint(0,3)
  208. if direction == 0:
  209. snitchUp()
  210. if direction == 1:
  211. snitchDown()
  212. if direction == 2:
  213. snitchLeft()
  214. if direction == 3:
  215. snitchRight()
  216.  
  217. #This moves the snitch down
  218. def snitchDown():
  219. x = maze.getCharAtPos(snitch.getRow()+1, snitch.getCol())
  220. if x == "#":
  221. print ("Bounce")
  222. else:
  223. maze.clearAtPos(snitch.getRow(), snitch.getCol())
  224. snitch.moveDown()
  225. maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
  226.  
  227. #This moves the snitch up
  228. def snitchUp():
  229. x = maze.getCharAtPos(snitch.getRow()-1, snitch.getCol())
  230. if x == "#":
  231. print ("Bounce")
  232. else:
  233. maze.clearAtPos(snitch.getRow(), snitch.getCol())
  234. snitch.moveUp()
  235. maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
  236.  
  237. #This moves the snitch left
  238. def snitchLeft():
  239. x = maze.getCharAtPos(snitch.getRow(), snitch.getCol()-1)
  240. if x == "#":
  241. print ("Bounce")
  242. else:
  243. maze.clearAtPos(snitch.getRow(), snitch.getCol())
  244. snitch.moveLeft()
  245. maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
  246.  
  247. #This moves the snitch right
  248. def snitchRight():
  249. x = maze.getCharAtPos(snitch.getRow(), snitch.getCol()+1)
  250. if x == "#":
  251. print ("Bounce")
  252. else:
  253. maze.clearAtPos(snitch.getRow(), snitch.getCol())
  254. snitch.moveRight()
  255. maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
  256.  
  257.  
  258. def main():
  259. global FPSCLOCK, DISPLAYSURF, IMAGESDICT, TILEMAPPING, BASICFONT
  260. print (maze.toString())
  261. maze.placeseeker(seekername, 2,2)
  262. maze.placeseeker(seekername2, 8,8)
  263. maze.placeseeker("X",5,5)
  264. drawMap(maze)
  265.  
  266. while True:
  267.  
  268. #thread 1 - look for an action
  269. for event in pygame.event.get(): # event handling loop
  270. if event.type == QUIT:
  271. # Player clicked the "X" at the corner of the window.
  272. terminate()
  273. elif event.type == KEYDOWN:
  274. if event.key == K_RIGHT:
  275. MSRight()
  276. elif event.key == K_UP:
  277. MSUp()
  278. elif event.key == K_DOWN:
  279. MSDown()
  280. elif event.key == K_LEFT:
  281. MSLeft()
  282. elif event.key == K_w:
  283. MS2Up()
  284. elif event.key == K_a:
  285. MS2Left()
  286. elif event.key == K_s:
  287. MS2Down()
  288. elif event.key == K_d:
  289. MS2Right()
  290. elif event.key == K_SPACE:
  291. restart()
  292. else:
  293. pass
  294. mapNeedsRedraw = True
  295.  
  296. #thread 2: redraw the screen
  297. DISPLAYSURF.fill(BGCOLOR) #draws the turquoise background
  298. #if something has changed, redraw....
  299. if mapNeedsRedraw:
  300. mapSurf = drawMap(maze)
  301. mapNeedsRedraw = False
  302.  
  303. mapSurfRect = mapSurf.get_rect()
  304. mapSurfRect.center = (HALF_WINWIDTH, HALF_WINHEIGHT)
  305.  
  306. # Draw the map on the DISPLAYSURF object.
  307. DISPLAYSURF.blit(mapSurf, mapSurfRect)
  308.  
  309. pygame.display.update() # draw DISPLAYSURF to the screen.
  310. FPSCLOCK.tick()
  311.  
  312. def drawMap(maze):
  313. #draw the tile sprites onto this surface.
  314. #this creates the visual map!
  315. mapSurfWidth = maze.getWidth() * TILEWIDTH
  316. mapSurfHeight = maze.getHeight() * TILEHEIGHT
  317. mapSurf = pygame.Surface((mapSurfWidth, mapSurfHeight))
  318. mapSurf.fill(BGCOLOR)
  319. for h in range(maze.getHeight()):
  320. for w in range(maze.getWidth()):
  321. thisTile = pygame.Rect((w * TILEWIDTH, h * TILEFLOORHEIGHT, TILEWIDTH, TILEHEIGHT))
  322. if maze.getCharAtPos(h, w) in TILEMAPPING:
  323. #checks in the TILEMAPPING directory above to see if there is a
  324. #matching picture, then renders it
  325. baseTile = TILEMAPPING[maze.getCharAtPos(h,w)]
  326.  
  327. # Draw the tiles for the map.
  328. mapSurf.blit(baseTile, thisTile)
  329. return mapSurf
  330.  
  331. def eatSprouts():
  332. #changing level
  333. maze.eatSprouts()
  334.  
  335. #replacing characters
  336. seeker.setRow(2)
  337. seeker.setCol(2)
  338. seeker2.setRow(8)
  339. seeker2.setCol(8)
  340. snitch.setRow(5)
  341. snitch.setCol(5)
  342. maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
  343. maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
  344. maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
  345.  
  346. #redrawing level
  347. drawMap(maze)
  348.  
  349. #level number counter
  350. levelCount()
  351.  
  352.  
  353. def levelCount():
  354. print("LEVEL UP: " + str(maze.mazeIndex+1))
  355.  
  356. def restart():
  357. try:
  358. maze.__init__()
  359. seeker.setRow(2)
  360. seeker.setCol(2)
  361. seeker2.setRow(8)
  362. seeker2.setCol(8)
  363. snitch.setRow(5)
  364. snitch.setCol(5)
  365. maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
  366. maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
  367. maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
  368. drawMap(maze)
  369. except:
  370. print("ERROR WHILST RESTARTING GAME")
  371.  
  372. def terminate():
  373. #shutdown routine
  374. pygame.quit()
  375. sys.exit()
  376.  
  377.  
  378. if __name__ == '__main__':
  379. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement