forked from 1egoman/empty-fork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·176 lines (118 loc) · 4.12 KB
/
main.py
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
# pygame
import pygame
from pygame.locals import *
# os
import os
# my files
import world
import main_menu
import entitys
import inventory as i
class app(object):
BG_COLOR = (180, 180, 180)
def __init__(self):
# init vars
self.w = 800
self.h = 500
self.running = True
# init pygame
pygame.init()
# create screen
self.s = pygame.display.set_mode((self.w, self.h), RESIZABLE)
pygame.display.set_caption("The Sandbox-Sandbox DEV0.1")
# go to menu
self.s, w = main_menu.go(self.s)
if not w: return
# create map, load world, and center it
self.wld = world.map(self.s, w=16, h=16)
# fill
self.s.fill((180,180,180))
rndr = self.wld._FONT.render("Loading...", True, (255,255,255))
self.s.blit(rndr, ( (self.w-rndr.get_width())/2, 200))
# create clock
self.clock = pygame.time.Clock()
# load map if possible
if os.listdir(os.path.join("saves", w)) != []:
self.wld.load_map(w)
else:
# create a new map, and save it
self.wld.save_map(w, notify=False)
self.wld.center_map((self.w,self.h))
# spawn a sandwich
# self.wld.spawn(0, 0, entitys.fire)
# loop
self.loop()
def loop(self):
# set mouse invisible
# pygame.mouse.set_visible(False)
while self.running:
# tick clock
self.clock.tick()
self.wld.FPS = self.clock.get_fps()
# events
for event in pygame.event.get():
if event.type == QUIT:
# quit program
self.running = False
return
elif event.type == VIDEORESIZE:
# resize screen
self.w, self.h = event.size
self.s = pygame.display.set_mode((self.w, self.h), RESIZABLE)
# center map on screen
self.wld.center_map(event.size)
elif event.type == KEYDOWN:
# "nudge keys"
if event.unicode == "i": self.wld.selected_tile = ( self.wld.selected_tile[0], self.wld.selected_tile[1]-1 )
if event.unicode == "l": self.wld.selected_tile = ( self.wld.selected_tile[0], self.wld.selected_tile[1]+1 )
if event.unicode == "k": self.wld.selected_tile = ( self.wld.selected_tile[0]-1, self.wld.selected_tile[1] )
if event.unicode == "o": self.wld.selected_tile = ( self.wld.selected_tile[0]+1, self.wld.selected_tile[1] )
# save/load
if event.unicode == "q": self.wld.save_map()
if event.unicode == "e": self.wld.load_map(self.wld._NAME)
# test
if event.unicode == "t":
self.wld.inventory.add_item(i.item(21,1))
# self.wld.tiles[0][0].tiles[-1] = 10
# back to main menu
if event.key == K_ESCAPE:
# save map
self.wld.save_map()
# pause clock
self.wld.clock.pause()
# go (back) to menu
self.s, w = main_menu.go(self.s)
if not w: return
# load map if possible
if os.listdir(os.path.join("saves", w)) != []:
self.wld.load_map(w)
else:
# create a new map, and save it
self.wld.flush_map()
self.wld.save_map(w, notify=False)
self.wld.center_map((self.w,self.h))
# uppause clock
self.wld.clock.unpause()
# debug (F1)
if event.key == 282:
self.wld._DIAGNOSTIC = not self.wld._DIAGNOSTIC
if self.wld._DIAGNOSTIC: self.wld.notify.msg("Diagnostics", "Diagnostics mode has been enabled.")
elif event.type == MOUSEMOTION:
# send mouse motion
self.wld.send_motion( *event.pos )
elif event.type == MOUSEBUTTONDOWN:
# send mouse motion
self.wld.send_mousebutton( event, "down" )
elif event.type == MOUSEBUTTONUP:
# send mouse motion
self.wld.send_mousebutton( event, "up" )
# draw background
self.s.fill(self.wld.BG_COLOR)
# render world
self.wld.render()
# flip screen
pygame.display.flip()
# make mouse visible again
pygame.mouse.set_visible(True)
if __name__ == '__main__':
a = app()