Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/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()