-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleGame.py
53 lines (41 loc) · 1.5 KB
/
ExampleGame.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
# -*- coding: utf-8 -*-
"""
##############################################################################################################
@author Álvaro Fernández García
@version 1.4
@date 4, feb, 2019
A Ray-casting 2D render engine based on python3 and pyglet.
Inspired in retro FPS games.
Here is an example of its use.
##############################################################################################################
"""
import pyglet
from pyglet.window import key
from RayCastRenderEngine import RenderEngine, World, Player
# Create a player object (you can use toWorld function to set its initial position)
player = Player(x=RenderEngine.toWorld(3), y=RenderEngine.toWorld(2), alpha=270)
# Create a world object and load a map to it:
world = World()
world.load_map("example.map")
# Create the render engine with player and world objects:
engine = RenderEngine(player, world)
# Init engine:
window = engine.init()
# Tell to Pyglet that it must update view calling engine.render_scene() function:
@window.event
def on_draw():
engine.render_scene()
# Define Input events and main loop game (dt is deltaTime):
keys = key.KeyStateHandler()
window.push_handlers(keys)
def main_loop(dt):
if keys[key.W]:
engine.player.move_forward(dt)
if keys[key.A]:
engine.player.rotate_left(dt)
if keys[key.S]:
engine.player.move_backward(dt)
if keys[key.D]:
engine.player.rotate_right(dt)
# Run engine: (should call this and not pyglet.run()!)
engine.run(main_loop)