just some tiles and movement
Getty Ritter
7 years ago
1 | local state = { | |
2 | keys = { | |
3 | w = false, | |
4 | a = false, | |
5 | s = false, | |
6 | d = false, | |
7 | }, | |
8 | char = { | |
9 | x = 60, | |
10 | y = 60, | |
11 | }, | |
12 | } | |
13 | local consts = { | |
14 | speed = 2, | |
15 | tileSize = 20, | |
16 | } | |
17 | local sprites = {} | |
18 | ||
19 | function love.load() | |
20 | sprites['character'] = love.graphics.newImage('tiles/character.png') | |
21 | sprites['water'] = love.graphics.newImage('tiles/water.png') | |
22 | sprites['grass'] = love.graphics.newImage('tiles/grass.png') | |
23 | end | |
24 | ||
25 | function love.update() | |
26 | if state.keys.w then | |
27 | state.char.y = state.char.y - consts.speed | |
28 | end | |
29 | if state.keys.s then | |
30 | state.char.y = state.char.y + consts.speed | |
31 | end | |
32 | if state.keys.a then | |
33 | state.char.x = state.char.x - consts.speed | |
34 | end | |
35 | if state.keys.d then | |
36 | state.char.x = state.char.x + consts.speed | |
37 | end | |
38 | end | |
39 | ||
40 | function love.keypressed(key) | |
41 | if key == 'q' then | |
42 | love.event.quit() | |
43 | elseif key == 'w' or key == 'up' then | |
44 | state.keys.w = true | |
45 | elseif key == 'a' or key == 'left' then | |
46 | state.keys.a = true | |
47 | elseif key == 's' or key == 'down' then | |
48 | state.keys.s = true | |
49 | elseif key == 'd' or key == 'right' then | |
50 | state.keys.d = true | |
51 | end | |
52 | end | |
53 | ||
54 | function love.keyreleased(key) | |
55 | if key == 'w' or key == 'up' then | |
56 | state.keys.w = false | |
57 | elseif key == 'a' or key == 'left' then | |
58 | state.keys.a = false | |
59 | elseif key == 's' or key == 'down' then | |
60 | state.keys.s = false | |
61 | elseif key == 'd' or key == 'right' then | |
62 | state.keys.d = false | |
63 | end | |
64 | end | |
65 | ||
66 | function love.draw() | |
67 | love.graphics.setColor(0, 0, 0) | |
68 | love.graphics.rectangle('fill', 0, 0, | |
69 | love.graphics.getWidth(), | |
70 | love.graphics.getHeight()) | |
71 | ||
72 | love.graphics.setColor(255, 255, 255) | |
73 | for x = 0, 39, 1 do | |
74 | for y = 0, 29, 1 do | |
75 | love.graphics.draw(sprites.grass, | |
76 | x * 20, | |
77 | y * 20) | |
78 | end | |
79 | end | |
80 | love.graphics.draw(sprites.character, | |
81 | state.char.x, | |
82 | state.char.y) | |
83 | end |
Binary diff not shown
Binary diff not shown
Binary diff not shown