Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Treasure Seeker
- # by Adam Binks
- import random, pygame, sys, time, pygame._view
- from pygame.locals import *
- FPS = 30
- WINDOWWIDTH = 640
- WINDOWHEIGHT = 480
- CELLSIZE = 20
- HALFCELLSIZE = int(CELLSIZE / 2)
- assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size."
- assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size."
- XCELLNUM = int(WINDOWWIDTH / CELLSIZE)
- YCELLNUM = int(WINDOWHEIGHT / CELLSIZE)
- SKYGAP = 5 # number of rows of sky above the grass layer
- GRASSLAYER = SKYGAP + 1 # y coord of grass layer
- LEFT = 'left'
- RIGHT = 'right'
- UP = 'up'
- COLOUR = 'colour'
- RECT = 'rect'
- SURF = 'surf'
- LIVES = 'lives'
- DIRECTION = 'direction'
- VELOCITY = 'velocity'
- BOMBSPLACED = 'bombsplaced'
- MOVESPEED = 5 # horizontal move speed
- JUMPSPEED = 2
- JUMPHEIGHT = -10
- FALLSPEED = -JUMPHEIGHT # is really FALLHEIGHT
- MAXLIVES = 3
- MAXBOMBS = 2 # inclusive
- FUSELENGTH = 2 # time before it goes BOOM in seconds
- NUMGEMS = 5 # per level
- grassImg = pygame.image.load('grass.png')
- dirtImg = pygame.image.load('dirt.png')
- rockImg = pygame.image.load('rock.png')
- gemImg = pygame.image.load('gem.png')
- bombImg = pygame.image.load('bomb.png')
- fireImg = pygame.image.load('fire.png')
- titleImg = pygame.image.load('title.gif')
- redPlayerRight = pygame.image.load('redPlayerRight.gif')
- redPlayerLeft = pygame.image.load('redPlayerLeft.gif')
- bluePlayerRight = pygame.image.load('bluePlayerRight.gif')
- bluePlayerLeft = pygame.image.load('bluePlayerLeft.gif')
- player1Rect = redPlayerRight.get_rect()
- player2Rect = bluePlayerLeft.get_rect()
- P1StartPos = [(3 * CELLSIZE), ((SKYGAP + 1) * CELLSIZE)]
- P2StartPos = [((XCELLNUM * CELLSIZE) - (2 * CELLSIZE)), ((SKYGAP + 1) * CELLSIZE)]
- P1score = P2score = 0
- bigRedMan = pygame.transform.scale(redPlayerRight, (200, 200))
- redManRect = bigRedMan.get_rect()
- redManRect.center = (int(WINDOWWIDTH / 4), int(WINDOWHEIGHT / 3) * 2)
- bigBlueMan = pygame.transform.scale(bluePlayerLeft, (200, 200))
- blueManRect = bigBlueMan.get_rect()
- blueManRect.center = (int(WINDOWWIDTH / 4) * 3, int(WINDOWHEIGHT / 3) * 2)
- # Set up blocks
- FIRE = -1 # ^ NON SOLID
- AIR = 0 # v
- DIRT = 1 # ^
- ROCK = 3 # | SOLID
- GRASS = 4 # |
- GEM = 5 # |
- BOMB = 6 # v
- # Colours R G B
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- RED = (255, 0, 0)
- DARKRED = (255, 10, 10)
- BLUE = ( 0, 0, 255)
- GREEN = ( 0, 255, 0)
- ORANGE = (255, 165, 0)
- DARKGREEN = ( 0, 155, 0)
- DARKGREY = ( 60, 60, 60)
- LIGHTGREY = (180, 180, 180)
- BROWN = (139, 69, 19)
- BROWN2 = (160, 82, 45)
- SKYBLUE = (176, 226, 255)
- TITLEBG = (139, 130, 126)
- SKYCOLOUR = SKYBLUE
- DIRTCOLOUR = BROWN
- DIRT2COLOUR = BROWN2
- ROCKCOLOUR = DARKGREY
- GRASSCOLOUR = GREEN
- GEMCOLOUR = RED
- BOMBCOLOUR = BLACK
- FIRECOLOUR = ORANGE
- BGCOLOUR = LIGHTGREY
- def main():
- global FPSCLOCK, DISPLAYSURF, BASICFONT, BIGFONT, jumpSound, clickSound, explosionSound, P1score, P2score, P1name, P2name
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
- BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
- BIGFONT = pygame.font.Font('freesansbold.ttf', 42)
- pygame.display.set_icon(rockImg)
- pygame.display.set_caption('Treasure Seeker')
- # Sounds
- jumpSound = pygame.mixer.Sound('jump.ogg')
- clickSound = pygame.mixer.Sound('click.ogg')
- explosionSound = pygame.mixer.Sound('explosion.ogg')
- button = 0
- while button != 1:
- button = showStartScreen()
- if button == 2: button = showCredits()
- if button == 3: howToPlay()
- while True:
- winner = runGame(P1score, P2score)
- P1score, P2score = showGameOverScreen(winner)
- def runGame(P1score, P2score):
- # set up variables
- global muted
- P1 = {RECT: player1Rect, SURF: redPlayerRight, DIRECTION: None, VELOCITY: 0, COLOUR: RED, LIVES: MAXLIVES, BOMBSPLACED: 0}
- P2 = {RECT: player2Rect, SURF: bluePlayerLeft, DIRECTION: None, VELOCITY: 0, COLOUR: BLUE, LIVES: MAXLIVES, BOMBSPLACED: 0}
- P1[RECT].bottomleft = P1StartPos
- P2[RECT].bottomleft = P2StartPos
- bombs = []
- muted = False
- debugMode = False
- mainBoard = generateTerrain()
- drawBoard(mainBoard, P1, P2, P1score, P2score, True)
- pygame.display.update()
- pygame.time.wait(2000)
- while True: # main game loop
- checkForQuit()
- for event in pygame.event.get(): # event handling loop
- if event.type == KEYDOWN:
- if (event.key == K_LEFT):
- P1[DIRECTION] = LEFT
- P1[SURF] = redPlayerLeft
- elif (event.key == K_RIGHT):
- P1[DIRECTION] = RIGHT
- P1[SURF] = redPlayerRight
- elif (event.key == K_UP):
- if not muted: jumpSound.play()
- if isOnGround(mainBoard, P1[RECT]):
- P1[VELOCITY] = JUMPHEIGHT
- elif (event.key == K_PERIOD):
- if P1[BOMBSPLACED] < MAXBOMBS:
- mainBoard, newBombDict, P1 = placeBomb(mainBoard, P1)
- bombs.append(newBombDict)
- elif (event.key == K_d):
- P2[DIRECTION] = RIGHT
- P2[SURF] = bluePlayerRight
- elif (event.key == K_a):
- P2[DIRECTION] = LEFT
- P2[SURF] = bluePlayerLeft
- elif (event.key == K_w):
- if not muted: jumpSound.play()
- if isOnGround(mainBoard, P2[RECT]):
- P2[VELOCITY] = JUMPHEIGHT
- elif (event.key == K_q):
- if P2[BOMBSPLACED] < MAXBOMBS:
- mainBoard, newBombDict, P2 = placeBomb(mainBoard, P2)
- bombs.append(newBombDict)
- elif (event.key == K_m):
- muted = not muted
- elif (event.key == K_0):
- debugMode = not debugMode
- elif (event.key == K_r): # reset player positions
- if debugMode:
- P1[RECT].bottomleft = P1StartPos
- P2[RECT].bottomleft =P2StartPos
- elif event.type == KEYUP:
- if event.key == K_LEFT and P1[DIRECTION] == LEFT:
- P1[DIRECTION] = None
- elif event.key == K_RIGHT and P1[DIRECTION] == RIGHT:
- P1[DIRECTION] = None
- if event.key == K_a and P2[DIRECTION] == LEFT:
- P2[DIRECTION] = None
- elif event.key == K_d and P2[DIRECTION] == RIGHT:
- P2[DIRECTION] = None
- mainBoard, bombs = explodeBombs(mainBoard, bombs, P1, P2)
- P1copy = P1
- P2copy = P2
- P1 = checkForDeath(mainBoard, P1)
- if P1 == 'dead':
- P1copy[SURF] = pygame.transform.rotate(P1copy[SURF], 90)
- drawBoard(mainBoard, P1copy, P2copy, P1score, P2score)
- gemSurf = BIGFONT.render('P1 exploded!', 1, DARKGREY)
- gemRect = gemSurf.get_rect()
- gemRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 5))
- DISPLAYSURF.blit(gemSurf, gemRect)
- pygame.display.update()
- pygame.time.wait(2000)
- return 'P2'
- if P1 == 'gem':
- P2copy[SURF] = pygame.transform.rotate(P2copy[SURF], 90)
- drawBoard(mainBoard, P1copy, P2copy, P1score, P2score)
- gemSurf = BIGFONT.render('P1 found a gem!', 1, DARKGREY)
- gemRect = gemSurf.get_rect()
- gemRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 5))
- DISPLAYSURF.blit(gemSurf, gemRect)
- pygame.display.update()
- pygame.time.wait(2000)
- return 'P1'
- P2 = checkForDeath(mainBoard, P2)
- if P2 == 'dead':
- P2copy[SURF] = pygame.transform.rotate(P2copy[SURF], 90)
- drawBoard(mainBoard, P1, P2copy, P1score, P2score)
- gemSurf = BIGFONT.render('P2 exploded!', 1, DARKGREY)
- gemRect = gemSurf.get_rect()
- gemRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 5))
- DISPLAYSURF.blit(gemSurf, gemRect)
- pygame.display.update()
- pygame.time.wait(2000)
- return 'P1'
- if P2 == 'gem':
- P1copy[SURF] = pygame.transform.rotate(P1copy[SURF], 90)
- drawBoard(mainBoard, P1copy, P1copy, P1score, P2score)
- gemSurf = BIGFONT.render('P2 found a gem!', 1, DARKGREY)
- gemRect = gemSurf.get_rect()
- gemRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 5))
- DISPLAYSURF.blit(gemSurf, gemRect)
- pygame.display.update()
- pygame.time.wait(2000)
- return 'P2'
- P1 = movePlayer(mainBoard, P1)
- P2 = movePlayer(mainBoard, P2)
- drawBoard(mainBoard, P1, P2, P1score, P2score)
- if debugMode: drawDebug(mainBoard, P1, P2)
- pygame.display.update()
- FPSCLOCK.tick(FPS)
- pygame.time.get_ticks()
- def showStartScreen():
- DISPLAYSURF.fill(ORANGE)
- pygame.draw.rect(DISPLAYSURF, TITLEBG, (20, 20, WINDOWWIDTH - 40, WINDOWHEIGHT - 40), 0)
- titleRect = titleImg.get_rect()
- titleRect.midbottom = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) - 40)
- playSurf = BASICFONT.render('Play', 1, LIGHTGREY)
- playRect = playSurf.get_rect()
- playRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
- creditsSurf = BASICFONT.render('Credits', 1, LIGHTGREY)
- creditsRect = playSurf.get_rect()
- creditsRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 25)
- helpSurf = BASICFONT.render('Help', 1, LIGHTGREY)
- helpRect = playSurf.get_rect()
- helpRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 50)
- quitSurf = BASICFONT.render('Quit', 1, LIGHTGREY)
- quitRect = quitSurf.get_rect()
- quitRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 75)
- DISPLAYSURF.blit(bigRedMan, redManRect)
- DISPLAYSURF.blit(bigBlueMan, blueManRect)
- DISPLAYSURF.blit(titleImg, titleRect)
- DISPLAYSURF.blit(playSurf, playRect)
- DISPLAYSURF.blit(creditsSurf, creditsRect)
- DISPLAYSURF.blit(helpSurf, helpRect)
- DISPLAYSURF.blit(quitSurf, quitRect)
- pygame.display.update()
- pygame.time.wait(200)
- pygame.mouse.get_pressed()
- while True:
- mousePosition = pygame.mouse.get_pos()
- if pygame.mouse.get_pressed()[0]:
- if playRect.collidepoint(mousePosition):
- clickSound.play()
- return 1
- if creditsRect.collidepoint(mousePosition):
- clickSound.play()
- return 2
- if helpRect.collidepoint(mousePosition):
- clickSound.play()
- return 3
- if quitRect.collidepoint(mousePosition):
- clickSound.play()
- terminate()
- checkForQuit()
- def showGameOverScreen(winner):
- global P1score, P2score
- DISPLAYSURF.fill(RED)
- pygame.draw.rect(DISPLAYSURF, DARKGREY, (20, 20, WINDOWWIDTH - 40, WINDOWHEIGHT - 40), 0)
- titleSurf = BIGFONT.render('Game Over!', 1, RED)
- titleRect = titleSurf.get_rect()
- titleRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) - 150)
- if winner == 'P1':
- man = bigRedMan
- manRect = redManRect
- P1score += 1
- else:
- man = bigBlueMan
- manRect = blueManRect
- P2score += 1
- manRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
- backSurf = BASICFONT.render('Play again?', 1, LIGHTGREY)
- backRect = backSurf.get_rect()
- backRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 125)
- quitSurf = BASICFONT.render('Quit', 1, LIGHTGREY)
- quitRect = quitSurf.get_rect()
- quitRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 150)
- winnerSurf = BASICFONT.render('%s won' %(winner), 1, WHITE)
- winnerRect = winnerSurf.get_rect()
- winnerRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) - 120)
- P1scoreSurf = BIGFONT.render('P1:' + str(P1score), 1, WHITE)
- P1scoreRect = P1scoreSurf.get_rect()
- P1scoreRect.midleft = (50, int(WINDOWHEIGHT / 2))
- P2scoreSurf = BIGFONT.render('P2:' + str(P2score), 1, WHITE)
- P2scoreRect = P2scoreSurf.get_rect()
- P2scoreRect.midright = (WINDOWWIDTH - 50, int(WINDOWHEIGHT / 2))
- DISPLAYSURF.blit(P1scoreSurf, P1scoreRect)
- DISPLAYSURF.blit(P2scoreSurf, P2scoreRect)
- DISPLAYSURF.blit(backSurf, backRect)
- DISPLAYSURF.blit(quitSurf, quitRect)
- DISPLAYSURF.blit(titleSurf, titleRect)
- DISPLAYSURF.blit(man, manRect)
- DISPLAYSURF.blit(winnerSurf, winnerRect)
- pygame.display.update()
- while True:
- mousePosition = pygame.mouse.get_pos()
- if pygame.mouse.get_pressed()[0]:
- if backRect.collidepoint(mousePosition):
- clickSound.play()
- return (P1score, P2score)
- if quitRect.collidepoint(mousePosition):
- clickSound.play()
- terminate()
- checkForQuit()
- def showCredits():
- DISPLAYSURF.fill(BLUE)
- pygame.draw.rect(DISPLAYSURF, BLACK, (20, 20, WINDOWWIDTH - 40, WINDOWHEIGHT - 40), 0)
- creditsSurf = BIGFONT.render('A game by Adam Binks', 1, WHITE)
- creditsRect = creditsSurf.get_rect()
- creditsRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) - 50)
- twitterSurf = BASICFONT.render('Follow me on Twitter! @thebinks98', 1, LIGHTGREY)
- twitterRect = twitterSurf.get_rect()
- twitterRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
- backSurf = BASICFONT.render('Back', 1, LIGHTGREY)
- backRect = backSurf.get_rect()
- backRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 3))
- gemRect = gemImg.get_rect()
- gemRect.center = (int(WINDOWWIDTH / 2), 2 * int(WINDOWHEIGHT / 3))
- DISPLAYSURF.blit(creditsSurf, creditsRect)
- DISPLAYSURF.blit(twitterSurf, twitterRect)
- DISPLAYSURF.blit(backSurf, backRect)
- DISPLAYSURF.blit(gemImg, gemRect)
- pygame.display.update()
- pygame.mouse.get_pressed()
- while True:
- mousePosition = pygame.mouse.get_pos()
- if pygame.mouse.get_pressed()[0]:
- if backRect.collidepoint(mousePosition):
- clickSound.play()
- return 0
- checkForQuit()
- def howToPlay():
- DISPLAYSURF.fill(ORANGE)
- pygame.draw.rect(DISPLAYSURF, BLACK, (20, 20, WINDOWWIDTH - 40, WINDOWHEIGHT - 40), 0)
- playSurf = BASICFONT.render('Back', 1, LIGHTGREY)
- playRect = playSurf.get_rect()
- playRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 3))
- questionMarkSurf = BIGFONT.render('?', 1, BLUE)
- questionMarkRect = questionMarkSurf.get_rect()
- questionMarkRect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) - 30)
- line1Surf = BASICFONT.render('P1 moves with <^>v keys and places a bomb with .', 1, LIGHTGREY)
- line1Rect = line1Surf.get_rect()
- line1Rect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2))
- line2Surf = BASICFONT.render('P2 moves with WASD keys and places a bomb with Q', 1, LIGHTGREY)
- line2Rect = line2Surf.get_rect()
- line2Rect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 25)
- line3Surf = BASICFONT.render('Find a gem or blow up your opponent to win! (Press M to mute.)', 1, LIGHTGREY)
- line3Rect = line3Surf.get_rect()
- line3Rect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 50)
- line4Surf = BASICFONT.render('TIP: Stuck in a hole? Jump on an unexploded bomb!', 1, LIGHTGREY)
- line4Rect = line4Surf.get_rect()
- line4Rect.center = (int(WINDOWWIDTH / 2), int(WINDOWHEIGHT / 2) + 75)
- DISPLAYSURF.blit(line1Surf, line1Rect)
- DISPLAYSURF.blit(line2Surf, line2Rect)
- DISPLAYSURF.blit(line3Surf, line3Rect)
- DISPLAYSURF.blit(line4Surf, line4Rect)
- DISPLAYSURF.blit(playSurf, playRect)
- DISPLAYSURF.blit(questionMarkSurf, questionMarkRect)
- pygame.display.update()
- pygame.mouse.get_pressed()
- while True:
- mousePosition = pygame.mouse.get_pos()
- if pygame.mouse.get_pressed()[0]:
- if playRect.collidepoint(mousePosition):
- clickSound.play()
- return 1
- checkForQuit()
- def movePlayer(board, P):
- # jumping
- if P[VELOCITY] < 0:
- if board[toBoard(P[RECT].right - 1)][toBoard(P[RECT].top + P[VELOCITY])] in (AIR, FIRE, GEM)\
- and board[toBoard(P[RECT].left + 1)][toBoard(P[RECT].top + P[VELOCITY])] in (AIR, FIRE, GEM): # if space above is free move there
- P[RECT].move_ip(0, P[VELOCITY])
- P[VELOCITY] += 1
- else: P[VELOCITY] = 0 # else velocity = 0 so start falling next turn
- # falling
- if not isOnGround(board, P[RECT]):
- if P[VELOCITY] > 0:
- if P[VELOCITY] < FALLSPEED: # velocity is not maximum increment it
- P[VELOCITY] += 1
- P[RECT].move_ip(0, P[VELOCITY])
- for i in reversed(range(1, P[VELOCITY])):
- while board[toBoard(P[RECT].left + 1)][toBoard(P[RECT].bottom - 1)]\
- or board[toBoard(P[RECT].right - 1)][toBoard(P[RECT].bottom - 1)]not in (AIR, FIRE, GEM):
- P[RECT].move_ip(0, -i)
- if P[RECT].bottom - 1 % 20 in (9, 19):
- P[RECT].move_ip(0, 1)
- for i in reversed(range(1, P[VELOCITY])):
- if board[toBoard(P[RECT].left + 1)][toBoard(P[RECT].bottom + i)] in (AIR, FIRE, GEM)\
- and board[toBoard(P[RECT].right - 1)][toBoard(P[RECT].bottom + i)] in (AIR, FIRE, GEM): #if i pixels above ground fall i pixels
- P[RECT].move_ip(0, i)
- break # break if can fall, else continue trying smaller values of i
- if P[VELOCITY] == 0:
- P[VELOCITY] = 1
- # moving left/right
- if P[DIRECTION] == LEFT:
- if P[RECT].left+1 - MOVESPEED > 0 and board[toBoard(P[RECT].left + 1 - MOVESPEED)][toBoard(P[RECT].top)] in (AIR, FIRE, GEM)\
- and board[toBoard(P[RECT].left + 1 - MOVESPEED)][toBoard(P[RECT].bottom - 1)] in (AIR, FIRE, GEM):
- P[RECT].move_ip(-MOVESPEED, 0)
- elif P[DIRECTION] == RIGHT:
- if P[RECT].right-1 + MOVESPEED < WINDOWWIDTH and board[toBoard(P[RECT].right - 1 + MOVESPEED)][toBoard(P[RECT].top)] in (AIR, FIRE, GEM)\
- and board[toBoard(P[RECT].right - 1 + MOVESPEED)][toBoard(P[RECT].bottom - 1)] in (AIR, FIRE, GEM):
- P[RECT].move_ip(MOVESPEED, 0)
- # standing
- if isOnGround(board, P[RECT]): P[VELOCITY] = 0
- return P
- def checkForDeath(board, P):
- Px, Py = P[RECT].midtop
- Px2, Py2 = P[RECT].midbottom
- if board[toBoard(Px)][toBoard(Py)] == FIRE:
- P[LIVES] -= 1
- P[RECT].bottomleft = P1StartPos
- if P[LIVES] == 0:
- return 'dead'
- if board[toBoard(Px)][toBoard(Py)] == GEM or board[toBoard(Px2)][toBoard(Py2)] == GEM:
- return 'gem'
- return P
- def placeBomb(board, P):
- pos = (toBoard(P[RECT].centerx), toBoard(P[RECT].centery)) # tuple of board coords
- board[toBoard(P[RECT].centerx)][toBoard(P[RECT].centery)] = BOMB
- bombValues = {'coords': pos, 'placeTime': time.time(), 'whoPlaced': P}
- P[BOMBSPLACED] += 1
- return (board, bombValues, P)
- def explodeBombs(board, bombs, P1, P2):
- for bomb in bombs[:]:
- if time.time() - FUSELENGTH > bomb['placeTime']:
- if not muted: explosionSound.play()
- bombx, bomby = bomb['coords']
- x = y = True
- if bombx > XCELLNUM - 3: x = False
- if bomby > YCELLNUM - 3: y = False # these variables check if explosion is outside board
- if board[bombx - 1][bomby ] not in (ROCK, BOMB, GEM): board[bombx - 1][bomby ] = FIRE
- if y and board[bombx ][bomby + 1] not in (ROCK, BOMB, GEM): board[bombx ][bomby + 1] = FIRE
- if x and board[bombx + 1][bomby ] not in (ROCK, BOMB, GEM): board[bombx + 1][bomby ] = FIRE
- if board[bombx ][bomby - 1] not in (ROCK, BOMB, GEM): board[bombx ][bomby - 1] = FIRE
- if board[bombx - 1][bomby - 1] not in (ROCK, BOMB, GEM): board[bombx - 1][bomby - 1] = FIRE
- if x and y and board[bombx + 1][bomby + 1] not in (ROCK, BOMB, GEM): board[bombx + 1][bomby + 1] = FIRE
- if x and board[bombx + 1][bomby - 1] not in (ROCK, BOMB, GEM): board[bombx + 1][bomby - 1] = FIRE
- if y and board[bombx - 1][bomby + 1] not in (ROCK, BOMB, GEM): board[bombx - 1][bomby + 1] = FIRE
- if board[bombx - 2][bomby ] not in (ROCK, BOMB, GEM): board[bombx - 2][bomby ] = FIRE
- if y and board[bombx ][bomby + 2] not in (ROCK, BOMB, GEM): board[bombx ][bomby + 2] = FIRE
- if x and board[bombx + 2][bomby ] not in (ROCK, BOMB, GEM): board[bombx + 2][bomby ] = FIRE
- if board[bombx ][bomby - 2] not in (ROCK, BOMB, GEM): board[bombx ][bomby - 2] = FIRE
- board[bombx][bomby] = FIRE
- if bomb['whoPlaced'] == P1:
- P1[BOMBSPLACED] -= 1
- elif bomb['whoPlaced'] == P2:
- P2[BOMBSPLACED] -= 1
- bombs.remove(bomb)
- return (board, bombs)
- def generateTerrain():
- checkForQuit()
- board = [] # Create data structure
- for x in range(XCELLNUM):
- column = []
- for y in range(YCELLNUM):
- if y <= SKYGAP:
- column.append(AIR)
- if y == GRASSLAYER:
- column.append(GRASS)
- if y > GRASSLAYER:
- randblock = random.choice([DIRT, DIRT, ROCK])
- column.append(randblock)
- board.append(column)
- for i in range(NUMGEMS):
- board[random.randint(3, XCELLNUM - 2)][random.randint(int(YCELLNUM / 2), YCELLNUM - 4)] = GEM
- return board
- def drawBoard(board, P1, P2, P1score, P2score, isFirstTime=False):
- DISPLAYSURF.fill(BGCOLOUR)
- pygame.draw.rect(DISPLAYSURF, SKYCOLOUR, (0, 0, WINDOWWIDTH, (SKYGAP + 1) * CELLSIZE), 0)
- # draw blocks
- for cellx in range(len(board)):
- for celly in range(len(board[0])):
- if board[cellx][celly] != 0:
- board[cellx][celly] = drawCell(cellx, celly, board[cellx][celly])
- # draw objects
- drawObjects(P1, P2)
- if isFirstTime:
- P1Label = BASICFONT.render('P1', 1, DARKGREY)
- P1LabelRect = P1Label.get_rect()
- P1LabelRect.midbottom = (P1[RECT].centerx, P1[RECT].top - 5)
- P2Label = BASICFONT.render('P2', 1, DARKGREY)
- P2LabelRect = P2Label.get_rect()
- P2LabelRect.midbottom = (P2[RECT].centerx, P2[RECT].top - 5)
- DISPLAYSURF.blit(P1Label, P1LabelRect)
- DISPLAYSURF.blit(P2Label, P2LabelRect)
- # draw lives
- for i in range(1, P1[LIVES] + 1):
- pos = Rect(2 + (i * 20), 5, CELLSIZE, CELLSIZE)
- DISPLAYSURF.blit(redPlayerRight, pos)
- for i in range(2, P2[LIVES] + 2):
- pos2 = Rect(WINDOWWIDTH - (2 + (i * 20)), 5, CELLSIZE, CELLSIZE)
- DISPLAYSURF.blit(bluePlayerLeft, pos2)
- # draw scores
- P1scoreSurf = BASICFONT.render(str(P1score), 1, DARKGREY)
- P1scoreRect = P1scoreSurf.get_rect()
- P1scoreRect.topleft = (5, 5)
- DISPLAYSURF.blit(P1scoreSurf, P1scoreRect)
- P2scoreSurf = BASICFONT.render(str(P2score), 1, DARKGREY)
- P2scoreRect = P2scoreSurf.get_rect()
- P2scoreRect.topright = (WINDOWWIDTH - 5, 5)
- DISPLAYSURF.blit(P2scoreSurf, P2scoreRect)
- def drawObjects(P1, P2):
- # draw players
- DISPLAYSURF.blit(P1[SURF], P1[RECT]) # P1
- DISPLAYSURF.blit(P2[SURF], P2[RECT]) # P2
- def drawCell(cellx, celly, block):
- # blocks only, not players
- left, top = boardToPixels(cellx, celly)
- if block == DIRT:
- dirtRect = dirtImg.get_rect()
- dirtRect.topleft = (left, top)
- DISPLAYSURF.blit(dirtImg, dirtRect)
- return DIRT
- elif block == ROCK:
- rockRect = rockImg.get_rect()
- rockRect.topleft = (left, top)
- DISPLAYSURF.blit(rockImg, rockRect)
- return ROCK
- elif block == GRASS:
- grassRect = grassImg.get_rect()
- grassRect.topleft = (left, top)
- DISPLAYSURF.blit(grassImg, grassRect)
- return GRASS
- elif block == GEM:
- gemRect = gemImg.get_rect()
- gemRect.topleft = (left, top)
- DISPLAYSURF.blit(gemImg, gemRect)
- return GEM
- elif block == BOMB:
- bombRect = bombImg.get_rect()
- bombRect.topleft = (left, top)
- DISPLAYSURF.blit(bombImg, bombRect)
- return BOMB
- elif block == FIRE:
- fireRect = fireImg.get_rect()
- fireRect.topleft = (left, top)
- DISPLAYSURF.blit(fireImg, fireRect)
- if random.randint(0, 20) > 18: return AIR # fire goes out, becomes air
- return FIRE
- def drawDebug(board, P1, P2):
- P1ground, P2ground = isOnGround(board, P1[RECT]), isOnGround(board, P2[RECT])
- P1x, P1y = pixelsToBoard(P1[RECT].center)
- debugText = BASICFONT.render("P1 midbottom: %s, P1 bottomright %s, P1 dir: %s, P2 dir: %s" \
- %(P1[RECT].midbottom, P1[RECT].bottomright, P1[DIRECTION], P2[DIRECTION]), 1, WHITE)
- debugText2 = BASICFONT.render("P1 onGround = %s, P2 onGround = %s, board[P1x][P1y+1]: %s" %(P1ground, P2ground, board[P1x][P1y+1]), 1, WHITE)
- debugText3 = BASICFONT.render("P1 board coords: %s, P2 board coords: %s, P1 vel: %s, P2 vel %s" \
- %(pixelsToBoard(P1[RECT].center), pixelsToBoard(P2[RECT].center), P1[VELOCITY], P2[VELOCITY]), 1, WHITE)
- DISPLAYSURF.blit(debugText, (0, 300))
- DISPLAYSURF.blit(debugText2, (0, 320))
- DISPLAYSURF.blit(debugText3, (0, 340))
- def boardToPixels(cellX, cellY):
- # !! returns TOP LEFT point of cell !!
- left = (cellX * CELLSIZE)
- top = (cellY * CELLSIZE)
- return [left, top]
- def pixelsToBoard(coords):
- # returns cell that x and y pixel is in when passed (x, y).
- pixX, pixY = coords
- return [int(pixX / CELLSIZE), int(pixY / CELLSIZE)]
- def toBoard(coord):
- # same as pixelsToBoard() but takes a single coord
- return (int(coord / CELLSIZE))
- def isOnGround(board, rect):
- if rect.bottom % 20 != 0: #if not on a horizontal gridline return False
- return False
- x, y = pixelsToBoard((rect.centerx, rect.bottom - 1))
- if board[x][y+1] not in (AIR, FIRE, GEM):
- return True
- return False
- def checkForKeyPress():
- checkForQuit()
- keyUpEvents = pygame.event.get(KEYUP)
- if len(keyUpEvents) == 0:
- return False
- return True
- def checkForQuit():
- for event in pygame.event.get(QUIT): # get all the QUIT events
- terminate() # terminate if any QUIT events are present
- for event in pygame.event.get(KEYUP): # get all the KEYUP events
- if event.key == K_ESCAPE:
- terminate() # terminate if the KEYUP event was for the Esc key
- pygame.event.post(event) # put the other KEYUP event objects back
- def terminate():
- pygame.quit()
- sys.exit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment