Repository navigation
Expand file tree
/
Copy pathsimon.rb
More file actions
executable file
·107 lines (99 loc) · 3.8 KB
/
Copy pathsimon.rb
File metadata and controls
executable file
·107 lines (99 loc) · 3.8 KB
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
#!/usr/bin/env ruby
require 'pp'
require 'gosu'
class MyWindow < Gosu::Window
def initialize
@width = 600
@height = 600
super(@width,@height,false)
self.caption = "Simon"
@font = Gosu::Font.new(self,"Arial",14)
@pattern = []
@userPattern = []
@pattern.push(Random.rand(4)+1)
@squareWidth = 200
@score = 0
@showPattern = 0
@repreat = false
end
def update()
if @showPattern != -1
sleep(0.75)
end
end
def button_down(id)
if id == Gosu::KbDown
pp @pattern
end
if button_down? Gosu::KbEscape
close
end
if @showPattern == -1 and button_down? Gosu::MsLeft
x = self.mouse_x.to_i
y = self.mouse_y.to_i
if x > @width/2-@squareWidth and x < @width/2 and y < @height/2 and y > @height/2-@squareWidth
square = 1
end
if x < @width/2+@squareWidth and x > @width/2 and y < @height/2 and y > @height/2-@squareWidth
square = 2
end
if x > @width/2-@squareWidth and x < @width/2 and y > @height/2 and y < @height/2+@squareWidth
square = 3
end
if x < @width/2+@squareWidth and x > @width/2 and y > @height/2 and y < @height/2+@squareWidth
square = 4
end
@userPattern.push(square)
if @userPattern != @pattern[0...@userPattern.length]
puts "You got #{@score}"
close
end
if @userPattern == @pattern
@score += 1
@pattern.push(Random.rand(4)+1)
@userPattern = []
@showPattern = 0
end
end
end
def draw()
white = Gosu::Color.argb(0xffffffff)
black = Gosu::Color.argb(0xff000000)
green = Gosu::Color.argb(0xff00ff00)
red = Gosu::Color.argb(0xffff0000)
blue = Gosu::Color.argb(0xff0000ff)
yellow = Gosu::Color.argb(0xffffff00)
draw_quad(@width/2, @height/2, green, @width/2-@squareWidth, @height/2, green, @width/2-@squareWidth,@height/2-@squareWidth, green, @width/2, @height/2-@squareWidth, green)
draw_quad(@width/2, @height/2, red, @width/2+@squareWidth, @height/2, red, @width/2+@squareWidth,@height/2-@squareWidth, red, @width/2, @height/2-@squareWidth, red)
draw_quad(@width/2, @height/2, yellow, @width/2-@squareWidth, @height/2, yellow, @width/2-@squareWidth,@height/2+@squareWidth, yellow, @width/2, @height/2+@squareWidth, yellow)
draw_quad(@width/2, @height/2, blue, @width/2+@squareWidth, @height/2, blue, @width/2+@squareWidth,@height/2+@squareWidth, blue, @width/2, @height/2+@squareWidth, blue)
if @showPattern != -1 and @showPattern != @pattern.length
square = @pattern[@showPattern]
if @showPattern > 0 and not @repeat and square == @pattern[@showPattern-1]
@showPattern -= 1
@repeat = true
elsif square == 1
draw_quad(@width/2, @height/2, white, @width/2-@squareWidth, @height/2, white, @width/2-@squareWidth,@height/2-@squareWidth, white, @width/2, @height/2-@squareWidth, white)
@repeat = false
elsif square == 2
draw_quad(@width/2, @height/2, white, @width/2+@squareWidth, @height/2, white, @width/2+@squareWidth,@height/2-@squareWidth, white, @width/2, @height/2-@squareWidth, white)
@repeat = false
elsif square == 3
draw_quad(@width/2, @height/2, white, @width/2-@squareWidth, @height/2, white, @width/2-@squareWidth,@height/2+@squareWidth, white, @width/2, @height/2+@squareWidth, white)
@repeat = false
elsif square == 4
draw_quad(@width/2, @height/2, white, @width/2+@squareWidth, @height/2, white, @width/2+@squareWidth,@height/2+@squareWidth, white, @width/2, @height/2+@squareWidth, white)
@repeat = false
end
@showPattern += 1
else
@showPattern = -1
end
@font.draw("#{@score}",10,10,1.0,1.0,1.0,Gosu::Color::WHITE)
end
def needs_cursor?
true
end
end
window = MyWindow.new
window.show