-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.rb
130 lines (103 loc) · 2.32 KB
/
Main.rb
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require "rubygems"
require "gosu"
class Player
attr_reader :x, :y
def initialize(window, startx, starty, image, speed, maxspeed)
@image = Gosu::Image.new(window, image, false)
@burn = Gosu::Sample.new(window, "engine.aif")
@speed = speed
@maxspeed = maxspeed
@playing = @burn.play(0)
@startx = startx
@starty = starty
reset
end
def chase(target)
@angle = Gosu::angle(@x, @y, target.x, target.y)
accelerate
end
def reset
@x = @startx
@y = @starty
@angle = 0.0
@vel_x = 0.0
@vel_y = 0.0
end
def turn_left
@angle -= 4.5
end
def turn_right
@angle += 4.5
end
def move
@x += @vel_x
@y += @vel_y
end
def accelerate
unless @playing.playing?
@playing = @burn.play
end
if @vel_x + offset_x < @maxspeed
@vel_x += offset_x
end
if @vel_y + offset_y < @maxspeed
@vel_y += offset_y
end
end
def deccelerate
if @vel_x - Gosu::offset_x(@angle, 0.5) > 0
@vel_x -= Gosu::offset_x(@angle, 0.5)
end
if @vel_y - Gosu::offset_x(@angle, 0.5) > 0
@vel_y -= Gosu::offset_x(@angle, 0.5)
end
end
def draw
@image.draw_rot(@x, @y, 1, @angle + 90)
end
protected
def offset_x
Gosu::offset_x(@angle, @speed)
end
def offset_y
Gosu::offset_y(@angle, @speed)
end
end
include Gosu
class GameWindow < Gosu::Window
def initialize
super(1280, 1024, false)
self.caption = "Will's first spriting with Gosu!"
@player = Player.new(self, 100, 100, "forward.png", 0.5, 7.5)
@enemy = Player.new(self, 400, 400, "enemy.png", 0.2, 5)
@input = { :up => [KbUp, GpButton0],
:left => [KbLeft, GpLeft],
:right => [KbRight, GpRight] }
end
def update
@player.turn_left if pressed?(:left)
@player.turn_right if pressed?(:right)
@player.accelerate if pressed?(:up)
@player.deccelerate if button_down?(KbDown)
if button_down? KbSpace
@player.reset
@enemy.reset
end
@player.move
@enemy.chase(@player)
@enemy.move
end
def draw
@player.draw
@enemy.draw
end
def button_down(id)
close if id == KbEscape
end
protected
def pressed?(key)
@input[key].detect { |k| button_down?(k) }
end
end
window = GameWindow.new
window.show