Commit deb0bf5f authored by Kevin Whitaker's avatar Kevin Whitaker
Browse files

commit of old project

parents
#!/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()
menu.png
http://opengameart.org/content/solar-system
# Copyright (c)2012 Eyecreate Studios
import random
class universe:
def __init__(self):
self.width=200
self.height=200
random.seed(None)
self.winLocation=[random.randint(0,self.width),random.randint(0,self.height)]
self.grid = [[0 for col in range(self.width)] for row in range(self.height)]
self.startLocation=self.getCloseLocation(self.winLocation[0],self.winLocation[1])
#Uncomment these if you like cheating
#print self.startLocation
#print self.winLocation
#print "distx:"+str(abs(self.startLocation[0]-self.winLocation[0]))+" disty:"+str(abs(self.startLocation[1]-self.winLocation[1]))
#print "approx fuel to destination:"+str(abs(self.startLocation[0]-self.winLocation[0])+abs(self.startLocation[1]-self.winLocation[1]))
def getView(self,cX,cY,width,height):
screen = [[0 for row in range(5)] for col in range(8)] #for now, grid size is hardcoded.
rangeX = range(cX-4,cX+4)
rangeY = range(cY-2,cY+3)
# check if view is outside of bounds
if cX-4<0: rangeX = range(width)
if cY-2<0: rangeY = range(height)
if cX+4>self.width: rangeX = range(self.width-width,self.width)
if cY+3>self.height: rangeY = range(self.height-height,self.height)
for rIndex in rangeX:
for cIndex in rangeY:
screen[rIndex-rangeX[0]][cIndex-rangeY[0]]=self.grid[rIndex][cIndex]
return screen,[cX-rangeX[0],cY-rangeY[0]]
def getCloseLocation(self,locX,locY):
return self.quickCoordinateCheck(random.randint(locX-(self.width/4),locX+(self.width/4)),random.randint(locY-(self.height/4),locY+(self.height/4)),self.width/2)
def getStartLocation(self):
return self.startLocation
def getWinLocation(self):
return self.winLocation
def quickCoordinateCheck(self,numX,numY,offset):
#make sure ship start is on map and within fuel distance.
retX=numX
retY=numY
if numX<0: retX=0
if numX>self.width: retX=self.width-offset
if numY<0: retY=0
if numY>self.height: retY=self.height-offset
return [retX,retY]
def newRandomPlanet(self):
return planet(None)
def generatePlanets(self):
for rIndex in range(len(self.grid)):
for cIndex in range(len(self.grid[rIndex])):
if random.randint(0,10)>7:
self.grid[rIndex][cIndex]=self.newRandomPlanet()
self.grid[self.winLocation[0]][self.winLocation[1]]=planet("winner")
class planet:
def __init__(self,pType):
self.pType=pType
def getType(self):
return self.pType
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment