Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #class imports
- from maze import Maze
- from player import seeker
- from player2 import seeker2
- from snitch import snitch
- #add in the pygame imports
- import random, sys, pygame
- from pygame.locals import *
- maze = Maze()
- seekername = "Seeker 1"
- seekername2 = "Seeker 2"
- seeker = seeker(seekername, 2, 2)
- seeker2 = seeker2(seekername2, 8, 8)
- snitch = snitch("X", 5, 5)
- FPS = 144 # frames per second to update the screen
- WINWIDTH = 1000 # width of the program's window, in pixels
- WINHEIGHT = 800 # height in pixels
- HALF_WINWIDTH = int(WINWIDTH / 2) #you need to know 1/2 sizes so you can
- HALF_WINHEIGHT = int(WINHEIGHT / 2) #place things centrally
- # The total width and height of each tile in pixels.
- TILEWIDTH = 32
- TILEHEIGHT = 32
- TILEFLOORHEIGHT = 32
- GREEN = (0, 255, 0)
- WHITE = (255, 255, 255)
- BGCOLOR = GREEN
- TEXTCOLOR = WHITE
- #A dictionary of the images used.
- IMAGESDICT = {'floor': pygame.image.load("Images/floor.gif"),
- 'wall': pygame.image.load("Images/wall.gif"),
- 'seeker': pygame.image.load("Images/wizard.gif"),
- 'seeker2': pygame.image.load("Images/wizard.gif"),
- 'snitch': pygame.image.load("Images/sprout.gif")}
- TILEMAPPING = { '#':IMAGESDICT['wall'],
- ' ':IMAGESDICT['floor'],
- seekername:IMAGESDICT['seeker'],
- seekername2:IMAGESDICT['seeker2'],
- "X": IMAGESDICT['snitch']}
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- DISPLAYSURF = pygame.display.set_mode((WINWIDTH, WINHEIGHT))
- pygame.display.set_caption('Quidditch Game')
- BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
- ####################################################################################################
- #Moves the seeker player to the left
- def MSLeft():
- moveSnitch()#Moves the snitch in a random direction
- x = maze.getCharAtPos(seeker.getRow(), seeker.getCol()-1)
- if x == "#":
- print ("That's a wall.")#If the player tries to move into a wall this is printed
- elif x == seekername2:
- print ("That's another player.")#If the player tries to move into another player this is printed
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")#Once the player catches the snitch this is printed
- eatSprouts()
- maze.clearAtPos(seeker.getRow(), seeker.getCol())
- seeker.moveLeft()
- maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
- print (seeker.toString())
- #Moves the seeker player to the right
- def MSRight():
- moveSnitch()
- x = maze.getCharAtPos(seeker.getRow(), seeker.getCol() + 1)
- if x == "#":
- print ("That's a wall.")
- elif x == seekername2:
- print ("That's another player")
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")
- eatSprouts()
- maze.clearAtPos(seeker.getRow(), seeker.getCol())
- seeker.moveRight()
- maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
- print (seeker.toString())
- def MSUp():
- moveSnitch()
- x = maze.getCharAtPos(seeker.getRow()-1, seeker.getCol())
- if x == "#":
- print ("That's a wall!")
- elif x == seekername2:
- print ("That's another player")
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")
- eatSprouts()
- maze.clearAtPos(seeker.getRow(), seeker.getCol())
- seeker.moveUp()
- maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
- print (seeker.toString())
- def MSDown():
- moveSnitch()
- x = maze.getCharAtPos(seeker.getRow()+1, seeker.getCol())
- if x == "#":
- print ("That's a wall!")
- elif x == seekername2:
- print ("That's another player")
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")
- eatSprouts()
- maze.clearAtPos(seeker.getRow(), seeker.getCol())
- seeker.moveDown()
- maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
- print (seeker.toString())
- #seeker 2 MOVEMENT
- def MS2Left():
- moveSnitch()
- x = maze.getCharAtPos(seeker2.getRow(), seeker2.getCol()-1)
- if x == "#":
- print ("That's a wall!")
- elif x == seekername:
- print ("That's another player")
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")
- seeker2.eatSprouts()
- eatSprouts()
- maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
- seeker2.moveLeft()
- maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
- print (seeker2.toString())
- def MS2Right():
- moveSnitch()
- x = maze.getCharAtPos(seeker2.getRow(), seeker2.getCol() + 1)
- if x == "#":
- print ("That's a wall!")
- elif x == seekername:
- print ("That's another player")
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")
- seeker2.eatSprouts()
- eatSprouts()
- maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
- seeker2.moveRight()
- maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
- print (seeker2.toString())
- def MS2Up():
- moveSnitch()
- x = maze.getCharAtPos(seeker2.getRow()-1, seeker2.getCol())
- if x == "#":
- print ("That's a wall!")
- elif x == seekername:
- print ("That's another player")
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")
- seeker2.eatSprouts()
- eatSprouts()
- maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
- seeker2.moveUp()
- maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
- print (seeker2.toString())
- def MS2Down():
- moveSnitch()
- x = maze.getCharAtPos(seeker2.getRow()+1, seeker2.getCol())
- if x == "#":
- print ("That's a wall!")
- elif x == seekername:
- print ("That's another player")
- else:
- if x == "X":
- print ("The Golden Snitch has been caught")
- seeker2.eatSprouts()
- eatSprouts()
- maze.clearAtPos(seeker2.getRow(), seeker2.getCol())
- seeker2.moveDown()
- maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
- print (seeker2.toString())
- #######################################################################################
- #This function moves the snitch in a random direction
- def moveSnitch():
- direction = random.randint(0,3)
- if direction == 0:
- snitchUp()
- if direction == 1:
- snitchDown()
- if direction == 2:
- snitchLeft()
- if direction == 3:
- snitchRight()
- #This moves the snitch down
- def snitchDown():
- x = maze.getCharAtPos(snitch.getRow()+1, snitch.getCol())
- if x == "#":
- print ("Bounce")
- else:
- maze.clearAtPos(snitch.getRow(), snitch.getCol())
- snitch.moveDown()
- maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
- #This moves the snitch up
- def snitchUp():
- x = maze.getCharAtPos(snitch.getRow()-1, snitch.getCol())
- if x == "#":
- print ("Bounce")
- else:
- maze.clearAtPos(snitch.getRow(), snitch.getCol())
- snitch.moveUp()
- maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
- #This moves the snitch left
- def snitchLeft():
- x = maze.getCharAtPos(snitch.getRow(), snitch.getCol()-1)
- if x == "#":
- print ("Bounce")
- else:
- maze.clearAtPos(snitch.getRow(), snitch.getCol())
- snitch.moveLeft()
- maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
- #This moves the snitch right
- def snitchRight():
- x = maze.getCharAtPos(snitch.getRow(), snitch.getCol()+1)
- if x == "#":
- print ("Bounce")
- else:
- maze.clearAtPos(snitch.getRow(), snitch.getCol())
- snitch.moveRight()
- maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
- def main():
- global FPSCLOCK, DISPLAYSURF, IMAGESDICT, TILEMAPPING, BASICFONT
- print (maze.toString())
- maze.placeseeker(seekername, 2,2)
- maze.placeseeker(seekername2, 8,8)
- maze.placeseeker("X",5,5)
- drawMap(maze)
- while True:
- #thread 1 - look for an action
- for event in pygame.event.get(): # event handling loop
- if event.type == QUIT:
- # Player clicked the "X" at the corner of the window.
- terminate()
- elif event.type == KEYDOWN:
- if event.key == K_RIGHT:
- MSRight()
- elif event.key == K_UP:
- MSUp()
- elif event.key == K_DOWN:
- MSDown()
- elif event.key == K_LEFT:
- MSLeft()
- elif event.key == K_w:
- MS2Up()
- elif event.key == K_a:
- MS2Left()
- elif event.key == K_s:
- MS2Down()
- elif event.key == K_d:
- MS2Right()
- elif event.key == K_SPACE:
- restart()
- else:
- pass
- mapNeedsRedraw = True
- #thread 2: redraw the screen
- DISPLAYSURF.fill(BGCOLOR) #draws the turquoise background
- #if something has changed, redraw....
- if mapNeedsRedraw:
- mapSurf = drawMap(maze)
- mapNeedsRedraw = False
- mapSurfRect = mapSurf.get_rect()
- mapSurfRect.center = (HALF_WINWIDTH, HALF_WINHEIGHT)
- # Draw the map on the DISPLAYSURF object.
- DISPLAYSURF.blit(mapSurf, mapSurfRect)
- pygame.display.update() # draw DISPLAYSURF to the screen.
- FPSCLOCK.tick()
- def drawMap(maze):
- #draw the tile sprites onto this surface.
- #this creates the visual map!
- mapSurfWidth = maze.getWidth() * TILEWIDTH
- mapSurfHeight = maze.getHeight() * TILEHEIGHT
- mapSurf = pygame.Surface((mapSurfWidth, mapSurfHeight))
- mapSurf.fill(BGCOLOR)
- for h in range(maze.getHeight()):
- for w in range(maze.getWidth()):
- thisTile = pygame.Rect((w * TILEWIDTH, h * TILEFLOORHEIGHT, TILEWIDTH, TILEHEIGHT))
- if maze.getCharAtPos(h, w) in TILEMAPPING:
- #checks in the TILEMAPPING directory above to see if there is a
- #matching picture, then renders it
- baseTile = TILEMAPPING[maze.getCharAtPos(h,w)]
- # Draw the tiles for the map.
- mapSurf.blit(baseTile, thisTile)
- return mapSurf
- def eatSprouts():
- #changing level
- maze.eatSprouts()
- #replacing characters
- seeker.setRow(2)
- seeker.setCol(2)
- seeker2.setRow(8)
- seeker2.setCol(8)
- snitch.setRow(5)
- snitch.setCol(5)
- maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
- maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
- maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
- #redrawing level
- drawMap(maze)
- #level number counter
- levelCount()
- def levelCount():
- print("LEVEL UP: " + str(maze.mazeIndex+1))
- def restart():
- try:
- maze.__init__()
- seeker.setRow(2)
- seeker.setCol(2)
- seeker2.setRow(8)
- seeker2.setCol(8)
- snitch.setRow(5)
- snitch.setCol(5)
- maze.placeseeker(seeker.getSeeker_char(), seeker.getRow(), seeker.getCol())
- maze.placeseeker(seeker2.getSeeker_char(), seeker2.getRow(), seeker2.getCol())
- maze.placeseeker(snitch.getSeeker_char(), snitch.getRow(), snitch.getCol())
- drawMap(maze)
- except:
- print("ERROR WHILST RESTARTING GAME")
- def terminate():
- #shutdown routine
- pygame.quit()
- sys.exit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement