Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 89 lines (70 sloc) 2.023 kb
30bab2c @practicingruby Hello world
practicingruby authored
1 require 'ray'
0d33290 @practicingruby Get rectangle to follow mouse
practicingruby authored
2
0c26745 @practicingruby Red rectangle at 200x200
practicingruby authored
3 Ray.game "Test" do
30bab2c @practicingruby Hello world
practicingruby authored
4 register { add_hook :quit, method(:exit!) }
0c26745 @practicingruby Red rectangle at 200x200
practicingruby authored
5
6 scene :square do
b685cd9 @practicingruby Make covering a white square with a red square cause the white square to...
practicingruby authored
7 @rect = Ray::Polygon.rectangle([0, 0, 20, 20], Ray::Color.red)
0d33290 @practicingruby Get rectangle to follow mouse
practicingruby authored
8 @rect.pos = [200,200]
9
7da2104 @practicingruby Display randomized white rectangles around the map
practicingruby authored
10 max_x = window.size.width - 20
11 max_y = window.size.height - 20
12
13 @goodies = 20.times.map do
14 x = rand(max_x) + 10
15 y = rand(max_y) + 10
16
17 g = Ray::Polygon.rectangle([0,0,10,10])
18 g.pos = [x,y]
19
20 g
21 end
22
41110cc @practicingruby Add five blue squares at random locations
practicingruby authored
23 max_x = window.size.width - 30
24 max_y = window.size.height - 30
25
26 @baddies = 5.times.map do
27 x = rand(max_x) + 15
28 y = rand(max_y) + 15
29 g = Ray::Polygon.rectangle([0,0,15,15], Ray::Color.blue)
30 g.pos += [x,y]
31 g
32 end
33
ff03287 @practicingruby Get rectangle to move in all directions
practicingruby authored
34 always do
b4a395a @practicingruby Keep red square trapped in box
practicingruby authored
35 if @rect.pos.x - 2 > 0
36 @rect.pos += [-2, 0] if holding?(:left)
37 end
38
39 if @rect.pos.x + 2 < window.size.width
40 @rect.pos += [2, 0] if holding?(:right)
41 end
42
43 if @rect.pos.y - 2 > 0
44 @rect.pos += [0, -2] if holding?(:up)
45 end
46
47 if @rect.pos.y + 2 < window.size.height
48 @rect.pos += [0, 2] if holding?(:down)
49 end
0c26745 @practicingruby Red rectangle at 200x200
practicingruby authored
50
b685cd9 @practicingruby Make covering a white square with a red square cause the white square to...
practicingruby authored
51 @goodies.reject! { |e|
52 [e.pos.x, e.pos.y, 10, 10].to_rect.inside?([@rect.pos.x, @rect.pos.y, 20, 20])
53 }
5595b3f @practicingruby Add a lose condition
practicingruby authored
54
cdf0fe1 @practicingruby Make blue squares chase red squares
practicingruby authored
55 @baddies.each do |e|
56 if e.pos.x < @rect.pos.x
57 e.pos += [rand*2.5,0]
58 else
59 e.pos -= [rand*2.5,0]
60 end
61
62 if e.pos.y < @rect.pos.y
63 e.pos += [0, rand*2.5]
64 else
65 e.pos -= [0, rand*2.5]
66 end
67 end
68
5595b3f @practicingruby Add a lose condition
practicingruby authored
69 @game_over ||= @baddies.any? { |e|
70 [e.pos.x, e.pos.y, 15, 15].to_rect.collide?([@rect.pos.x, @rect.pos.y, 20,20])
71 }
b685cd9 @practicingruby Make covering a white square with a red square cause the white square to...
practicingruby authored
72 end
b4a395a @practicingruby Keep red square trapped in box
practicingruby authored
73
0c26745 @practicingruby Red rectangle at 200x200
practicingruby authored
74 render do |win|
2766d52 @practicingruby Make the screen display 'you win' if all goodies are removed
practicingruby authored
75 if @goodies.empty?
76 win.draw text("YOU WIN", :at => [100,100], :size => 60)
5595b3f @practicingruby Add a lose condition
practicingruby authored
77 elsif @game_over
78 win.draw text("YOU LOSE", :at => [100,100], :size => 60)
2766d52 @practicingruby Make the screen display 'you win' if all goodies are removed
practicingruby authored
79 else
80 @goodies.each { |g| win.draw(g) }
41110cc @practicingruby Add five blue squares at random locations
practicingruby authored
81 @baddies.each { |g| win.draw(g) }
2766d52 @practicingruby Make the screen display 'you win' if all goodies are removed
practicingruby authored
82 win.draw @rect
83 end
0c26745 @practicingruby Red rectangle at 200x200
practicingruby authored
84 end
30bab2c @practicingruby Hello world
practicingruby authored
85 end
125f615 @practicingruby Get rectangle to move left on left key press
practicingruby authored
86
0c26745 @practicingruby Red rectangle at 200x200
practicingruby authored
87 scenes << :square
30bab2c @practicingruby Hello world
practicingruby authored
88 end
Something went wrong with that request. Please try again.