Skip to content
LostPlanet.py 5.99 KiB
Newer Older
Kevin Whitaker's avatar
Kevin Whitaker committed
#!/usr/bin/python

# Copyright (c)2012 Eyecreate Studios
# Ludum Dare 23 Entry : Tiny World
# You have limited fuel to find your home planet. Explore the stars in order to find your home.

import pygame
import os
import universe
import threading

windowWidth = 800
windowHeight = 480
gridWidth = 8
gridHeight = 5
universeGrid = None
shipX = None
shipY = None
shipDir = 0
moveState = False
fuelBlocks = 200
gameState = "menu"
screen = None
quit = False
genThread=None
defaultFont =  None
smallDefaultFont = None
currentMap = [[]]
#surfaces
menuSurf = []
gameLoadSurf = []
inGameSurf = []

def init():
	global universeGrid,screen,shipX,shipY,defaultFont,smallDefaultFont	
	universeGrid = universe.universe()
	shipX,shipY = universeGrid.getStartLocation()
	pygame.init()
	pygame.display.set_caption("Lost Planet")
	defaultFont = pygame.font.SysFont(None,32)
	smallDefaultFont = pygame.font.SysFont(None,20)
	screen = pygame.display.set_mode((windowWidth,windowHeight))
	menuSurf.append(pygame.image.load("res"+os.sep+"logo.png"))
	gameLoadSurf.append(pygame.image.load("res"+os.sep+"loading.png"))
	inGameSurf.append(pygame.image.load("res"+os.sep+"planets"+os.sep+"planet.png"))
	inGameSurf.append(pygame.image.load("res"+os.sep+"ship.png"))
	inGameSurf.append(pygame.image.load("res"+os.sep+"planets"+os.sep+"homeplanet.png"))

def main():
	global quit,screen,shipX,shipY,gameState
	while not quit:
		for event in pygame.event.get():
			if gameState == "menu":	
				processMenuEvent(event,screen)
			if gameState == "startgame":
				processPreGameEvent(event,screen)
			if gameState == "ingame":
				processInGameEvent(event,screen)
			if gameState == "win" or gameState =="lose":
				processPostGameEvent(event,screen)
			if event.type == pygame.QUIT:
				quit=True
		if gameState == "menu":
			screen.fill((0,0,0))
			screen.blit(menuSurf[0],(windowWidth/2-menuSurf[0].get_width()/2,windowHeight/2-menuSurf[0].get_height()/2))
			screen.blit(defaultFont.render("Press Enter to Start and use arrowkeys to move",True,(255,255,255)),(windowWidth/6,3*(windowHeight/4)))
			screen.blit(smallDefaultFont.render("You have limited fuel to find your home planet. Explore the stars in order to find your home.",True,(255,255,255)),(windowWidth/7,2*(windowHeight/3)))
		if gameState == "startgame":
			screen.fill((0,0,0))
			screen.blit(gameLoadSurf[0],(windowWidth/2-gameLoadSurf[0].get_width()/2,windowHeight/2-gameLoadSurf[0].get_height()/2))
			if not genThread.is_alive():
				gameState="ingame"
		if gameState == "ingame":
			screen.fill((0,0,0))
			drawSpace(screen)
			drawHUD(screen)
		if gameState == "win":
			screen.fill((0,0,0))
			screen.blit(defaultFont.render("You Win!",True,(255,255,255)),(windowWidth/4,windowHeight/4))
		if gameState == "lose":
			screen.fill((0,0,0))
			screen.blit(defaultFont.render("You Lose",True,(255,255,255)),(windowWidth/4,windowHeight/4))
		pygame.display.flip()

def processInGameEvent(event,screen):
	global gameState,shipDir,moveState,shipX,shipY,fuelBlocks
	if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
		fuelBlocks=200
		universeGrid = universe.universe()
		shipX,shipY = universeGrid.getStartLocation()
		gameState="menu"
	if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT and moveState == False:
		moveState=True
		shipDir=90
		shipX=shipX-1
		fuelBlocks=fuelBlocks-1
		moveCheck()
	if event.type == pygame.KEYUP and event.key == pygame.K_LEFT:
		moveState=False
	if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT and moveState == False:
		moveState=True
		shipDir=270
		shipX=shipX+1
		fuelBlocks=fuelBlocks-1
		moveCheck()
	if event.type == pygame.KEYUP and event.key == pygame.K_RIGHT:
		moveState=False
	if event.type == pygame.KEYDOWN and event.key == pygame.K_UP and moveState == False:
		moveState=True
		shipDir=0
		shipY=shipY-1
		fuelBlocks=fuelBlocks-1
		moveCheck()
	if event.type == pygame.KEYUP and event.key == pygame.K_UP:
		moveState=False
	if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN and moveState == False:
		moveState=True
		shipDir=180
		shipY=shipY+1
		fuelBlocks=fuelBlocks-1
		moveCheck()
	if event.type == pygame.KEYUP and event.key == pygame.K_DOWN:
		moveState=False
		
def processPreGameEvent(event,screen):
	global gameState

def processPostGameEvent(event,screen):
	global gameState
	if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
		gameState="menu"

def processMenuEvent(event, screen):
	global gameState,quit,genThread
	if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
		gameState = "startgame"
		genThread=threading.Thread(target=universeGrid.generatePlanets)
		genThread.start()
	if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
		quit=True

def drawSpace(screen):
	#check if is planet using type()
	global currentMap
	currentMap,shipLoc = universeGrid.getView(shipX,shipY,gridWidth,gridHeight)
	for col in range(gridWidth):
		for row in range(gridHeight):
			if type(currentMap[col][row])==type(universe.planet(None)):
				if currentMap[col][row].getType()=="winner":
					screen.blit(pygame.transform.scale(inGameSurf[2],(100,100)),(100*col,100*row))
				else:	
					screen.blit(pygame.transform.scale(inGameSurf[0],(100,100)),(100*col,100*row))
			if col==shipLoc[0] and row==shipLoc[1]:
				screen.blit(pygame.transform.rotate(pygame.transform.scale(inGameSurf[1],(100,100)),shipDir),(100*col,100*row))

def drawHUD(screen):
	screen.blit(defaultFont.render("Fuel:"+str(fuelBlocks),True,(255,255,255)),(0,0))

def moveCheck():
	global shipX,shipY,gameState,fuelBlocks,universeGrid
	if(shipX<0 ): shipX=0 #hardcoded values shold be removed
	if(shipX>500): shipX=500
	if(shipY<0): shipY=0
	if(shipY>500): shipY=500
	#game state check
	if(shipX==universeGrid.getWinLocation()[0] and shipY==universeGrid.getWinLocation()[1]):
		fuelBlocks=200
		universeGrid = universe.universe()
		shipX,shipY = universeGrid.getStartLocation()
		gameState="win"
	if(fuelBlocks<=0):
		fuelBlocks=200
		universeGrid = universe.universe()
		shipX,shipY = universeGrid.getStartLocation()
		gameState="lose"

if __name__ == "__main__":
	init()
	main()