-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
executable file
·169 lines (156 loc) · 4.49 KB
/
game.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#encoding utf-8
require "rubygems"
require './models/utils'
require "rubygame"
require "./models/gameobject"
require "./models/background"
require "./models/block"
require "./models/paddle"
require "./models/collisionsupervisor"
require "./models/ball"
require "./models/playerdata"
class Game
include Math
def initialize
#TODO start threads that run thecollsion detection
@clock = Rubygame::Clock.new
@clock.target_framerate = 75
@screen = Rubygame::Screen.new [1000,1000], 0 ,[Rubygame::HWSURFACE,Rubygame::DOUBLEBUF]
@background = Background.new @screen.width, @screen.height
@blockground = Background.new @screen.width, @screen.height
@balls = []
@queue = Rubygame::EventQueue.new
@pressed_keys = []
@blocks = []
@colors = [[00,255,56],[00,40,255],[255,174,0],[255,28,00],[224,00,255]]
@players = []
@players[0] = PlayerData.new('pl1', @colors[2])
@players[1] = PlayerData.new('pl2', @colors[1])
create_court
@blocks += create_breakables [@screen.width/2,@screen.width/2]
@paddles = [Paddle.new(@players[0],500, 110, 60, 15, 0,false)]
@paddles << Paddle.new(@players[1],500, 890, 60, 15, 0,false)
30.times {@balls << (Ball.new 750-rand(500),750-rand(500) , 3, 0,-1) }
@collisiondetector = CollisionSupervisor.new @balls, @blocks, @background, @paddles
@first_frame = true
@start = 0
end
def run!
loop do
update
handle_events
draw
resetmotion
@paddles.each do |paddle|
paddle.cooldown
end
@clock.tick
end
end
def update
@collisiondetector.collide!
end
def draw
@background.draw @screen
draw_paddles
if @collisiondetector.renew or @first_frame
draw_blocks
end
draw_balls
@screen.flip
end
def resetmotion
@balls.each do |ball|
ball.resetmotion
end
end
def draw_paddles
@paddles.each do |paddle|
paddle.draw @screen
end
end
def handle_events
@queue.each do |event|
case event
when Rubygame::QuitEvent
Rubygame.quit
exit
when Rubygame::KeyDownEvent
@pressed_keys << event.key
when Rubygame::KeyUpEvent
@pressed_keys.delete_if {|key| key == event.key}
end
end
# TODO loop trough array and execute actions
@pressed_keys.each do |key|
if key == Rubygame::K_LEFT
@paddles[0].move_left
elsif key == Rubygame::K_RIGHT
@paddles[0].move_right
elsif key == Rubygame::K_DOWN
@paddles[0].teleport_down
end
if key == Rubygame::K_A
@paddles[1].move_left
elsif key == Rubygame::K_D
@paddles[1].move_right
elsif key == Rubygame::K_S
@paddles[1].teleport_down
end
end
end
def draw_blocks
@background.redraw
for block in @blocks do
block.draw @background.surface
end
@first_frame = false
end
def draw_balls
for ball in @balls do
ball.draw @screen
end
end
def create_court
@blocks += hexagon_factory([@screen.width/2,@screen.width/2],Block,@screen.width,20,[[40,40,40]])
end
def create_breakables centre, sym=[rand(4),rand(4),rand(3),rand(4),rand(4)]
blocks=[]
for n in 1..sym[0] do
blocks += hexsymetric_factoy([0,45*n],centre,Block,45,4)
end
for n in 1..sym[1]
blocks += hexsymetric_factoy([39,22.5+45*n],centre,Block,45,4)
end
for n in 2..sym[2] do
blocks += hexsymetric_factoy([-39,22.5+45*n],centre,Block,45,4)
end
for n in 3..sym[3] do
blocks += hexsymetric_factoy([78,45*n],centre,Block,45,4)
end
for n in 4..sym[4] do
blocks += hexsymetric_factoy([-78,45*n],centre,Block,45,4)
end
blocks += hexsymetric_factoy([0,0],centre,Block,45,4)
return blocks
end
def hexagon_factory position, blocktype, width, thickness,colors,breakable=false
blocks=[]
for n in 1..6 do
color = colors[rand(colors.length)]
blocks << blocktype.new((width/2.5)*sin(2*PI*n/6)+position[0],(width/2.5)*cos(2*PI*n/6)+position[1], width/2.11, thickness, 60*(n-1) + 60,color ,breakable)
end
return blocks
end
def hexsymetric_factoy position, centre, blocktype, width, thickness
blocks=[]
for n in 1..6 do
xpos=position[0]*cos(2*PI*n/6) - position[1]*sin(2*PI*n/6)+centre[0]
ypos=position[0]*sin(2*PI*n/6) + position[1]*cos(2*PI*n/6)+centre[1]
blocks += hexagon_factory([xpos,ypos],blocktype,width,thickness,@colors,true)
end
return blocks
end
end
g = Game.new
g.run!