-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameApp.py
142 lines (109 loc) · 4.42 KB
/
GameApp.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
from kivy.config import Config
Config.set('graphics', 'resizable', '0')
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '600')
Config.set('modules', 'monitor', '')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.graphics import Rectangle
from kivy.graphics.texture import Texture
from kivy.core.window import Window
from PixelMap import PixelMap
from MapBuffer import MapBuffer
from Player import Player
FPS = 120
class MapGame(Widget):
def __init__(self, **kwargs):
super(MapGame, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self._keyboard.bind(on_key_up=self._on_keyboard_up)
def _keyboard_closed(self):
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard.unbind(on_key_up=self._on_keyboard_up)
self._keyboard = None
def _on_keyboard_up(self, keyboard, keycode):
if keycode[1] == 'z':
self.player.reset_speed('up')
elif keycode[1] == 's':
self.player.reset_speed('down')
elif keycode[1] == 'q':
self.player.reset_speed('left')
elif keycode[1] == 'd':
self.player.reset_speed('right')
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'z':
self.player.set_speed('up')
elif keycode[1] == 's':
self.player.set_speed('down')
elif keycode[1] == 'q':
self.player.set_speed('left')
elif keycode[1] == 'd':
self.player.set_speed('right')
def init_game(self):
self.world = PixelMap(3000, 300)
self.player = Player(50, 0., 0.)
self.view = MapBuffer(self.world, self.player.x, self.player.y)
self.texture = Texture.create(size=(601, 601))
def update(self, dt):
self.player.update_position(dt)
self.refresh_texture()
self.canvas.clear()
with self.canvas:
Rectangle(texture = self.texture, pos=(0,0), size = (601,601))
def refresh_texture(self):
data = self.view.get_view(self.player.y, self.player.x).tobytes()
self.texture.blit_buffer(data, bufferfmt="ubyte", colorfmt="luminance")
class MapApp(App):
def build(self):
game = MapGame()
game.init_game()
Clock.schedule_interval(game.update, 1.0 / FPS)
return game
if __name__ == '__main__':
MapApp().run()
# default_font = pygame.font.get_default_font()
# font_renderer = pygame.font.Font(default_font, 45)
# displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
# pygame.display.set_caption("Procedural map generation")
# FramePerSec = pygame.time.Clock()
# world = PixelMap(3000, 300)
# player = Player(50, 0., 0.)
# view = MapBuffer(world, player.x, player.y)
# displaysurface.fill((255,255,255))
# displaysurface.blit(view.get_surface(player.x, player.y), (0, 0))
# mask = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
# mask.fill((0,0,0, 255))
# pygame.draw.circle(mask, (255,255,255, 0), (WIDTH//2, HEIGHT//2), min(HEIGHT,WIDTH)//2)
# running = True
# while running:
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# running = False
# if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_z:
# player.set_speed('up')
# if event.key == pygame.K_s:
# player.set_speed('down')
# if event.key == pygame.K_q:
# player.set_speed('left')
# if event.key == pygame.K_d:
# player.set_speed('right')
# if event.type == pygame.KEYUP:
# if event.key == pygame.K_z:
# player.reset_speed('up')
# if event.key == pygame.K_s:
# player.reset_speed('down')
# if event.key == pygame.K_q:
# player.reset_speed('left')
# if event.key == pygame.K_d:
# player.reset_speed('right')
# dT = FramePerSec.tick(FPS)
# player.update_position(dT/1000)
# label = font_renderer.render(str(round(FramePerSec.get_fps())), True, (255,255,255))
# displaysurface.blit(view.get_surface(player.x, player.y), (0, 0))
# displaysurface.blit(mask, (0,0))
# displaysurface.blit(label, label.get_rect())
# pygame.display.update()