Skip to content

Commit 312ca75

Browse files
author
Artur Ostręga
committed
Extend example
1 parent 40d53de commit 312ca75

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

example/assets/images/guy.png

25.6 KB
Loading

example/src/looks/guy_look.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'yeah/sprite_look' #
2+
3+
class GuyLook < Yeah::SpriteLook #
4+
#class GuyLook < ImageLook
5+
self.image = "guy.png" #
6+
self.size = 64, 64
7+
8+
self.animations = {
9+
walk_up: (0..8),
10+
walk_left: (9..17),
11+
walk_down: (18..26),
12+
walk_right: { frames: (27..35), mirror_x: false },
13+
stand_up: 0,
14+
stand_left: 9,
15+
stand_down: 18,
16+
stand_right: 27
17+
}
18+
end

example/src/spaces/world.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
require 'things/duck' #
21
require 'things/cloud' #
2+
require 'things/duck' #
3+
require 'things/guy' #
34

45
class World < Yeah::Space #
56
#class World < Space
67
self.size = 640, 360
78
self.color = 0.5, 0.7, 0.5
89

10+
self.things = {
11+
Cloud => [
12+
-> { [width * 0.6, height * 0.8] },
13+
-> { [width * 0.2, height * 0.7] },
14+
-> { [width * 0.1, height * 0.9] }
15+
],
16+
17+
Duck => [
18+
[10, 10]
19+
],
20+
21+
Guy => [
22+
-> { [width * 0.8, height * 0.8] }
23+
]
24+
}
25+
926
def initialize(game)
1027
super
1128

1229
things << Cloud.new(game, x: width * 0.6, y: height * 0.8)
1330
things << Cloud.new(game, x: width * 0.2, y: height * 0.7)
1431
things << Cloud.new(game, x: width * 0.1, y: height * 0.9)
1532
things << Duck.new(game, x: 10, y: 10)
33+
things << Guy.new(game, x: width * 0.8, y: height * 0.8)
1634
#things << Duck.new(game, position: Vec2.divide(size, 2))
1735
end
1836
end

example/src/things/cloud.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'yeah/thing' #
12
require 'looks/cloud_look' #
23

34
class Cloud < Yeah::Thing #

example/src/things/duck.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ class Duck < Yeah::Thing #
66
self.look = DuckLook #
77

88
def act(elapsed)
9-
if keyboard.pressing? :left
10-
self.x -= speed * elapsed
11-
end
12-
13-
if keyboard.pressing? :right
14-
self.x += speed * elapsed
15-
end
16-
179
if keyboard.released? :space
1810
puts "Quack!"
1911
space.color = rand, rand, rand
@@ -28,6 +20,8 @@ def act(elapsed)
2820
display.width += 10
2921
display.height += 10
3022
end
23+
24+
self.x += speed * elapsed
3125
end
3226

3327
def speed

example/src/things/guy.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'yeah/thing' #
2+
require 'looks/guy_look' #
3+
4+
class Guy < Yeah::Thing
5+
#class Guy < Thing
6+
self.look = GuyLook #
7+
8+
def act(elapsed)
9+
walked = false
10+
11+
if keyboard.pressing? :left
12+
look.animation = :walk_left
13+
self.x -= speed * elapsed
14+
walked = true
15+
end
16+
17+
if keyboard.pressing? :right
18+
look.animation = :walk_right
19+
self.x += speed * elapsed
20+
walked = true
21+
end
22+
23+
if keyboard.pressing? :down
24+
look.animation = :walk_down
25+
self.y -= speed * elapsed
26+
walked = true
27+
end
28+
29+
if keyboard.pressing? :up
30+
look.animation = :walk_up
31+
self.y += speed * elapsed
32+
walked = true
33+
end
34+
35+
unless walked
36+
look.animation = :stand_left if keyboard.released? :left
37+
look.animation = :stand_right if keyboard.released? :right
38+
look.animation = :stand_down if keyboard.released? :down
39+
look.animation = :stand_up if keyboard.released? :up
40+
end
41+
end
42+
43+
def speed
44+
50
45+
end
46+
end

0 commit comments

Comments
 (0)