mirror of
https://github.com/SleepingInsomniac/asteroids
synced 2024-11-15 19:48:28 +01:00
Add wrap around rendering
This commit is contained in:
parent
27aeb7acea
commit
86f413a2af
5 changed files with 37 additions and 12 deletions
|
@ -1,11 +1,13 @@
|
|||
class Asteroid < VectorSprite
|
||||
property size : Float64 = 1.0
|
||||
|
||||
def initialize
|
||||
super
|
||||
@frame = VectorSprite.generate_circle(12, size: 10.0, jitter: 3.0)
|
||||
@frame = VectorSprite.generate_circle(15, size: 10.0, jitter: 3.0)
|
||||
end
|
||||
|
||||
def draw(renderer)
|
||||
frame = project_points(@frame)
|
||||
frame = project_points(points: @frame, scale: Vector2.new(@size, @size))
|
||||
renderer.draw_color = SDL::Color[128, 128, 128, 255]
|
||||
draw_frame(renderer, frame)
|
||||
end
|
||||
|
|
|
@ -7,6 +7,22 @@ require "./ship"
|
|||
require "./asteroid"
|
||||
require "./bullet"
|
||||
|
||||
WIDTH = 600
|
||||
HEIGHT = 400
|
||||
SCALE = 3
|
||||
|
||||
module LxGame
|
||||
def draw_point(renderer, x, y)
|
||||
x = x % WIDTH
|
||||
y = y % HEIGHT
|
||||
|
||||
x = WIDTH + x if x < 0
|
||||
y = HEIGHT + y if y < 0
|
||||
|
||||
renderer.draw_point(x, y)
|
||||
end
|
||||
end
|
||||
|
||||
class Asteroids < Game
|
||||
@ship : Ship
|
||||
@asteroids = [] of Asteroid
|
||||
|
@ -23,6 +39,8 @@ class Asteroids < Game
|
|||
@asteroids << Asteroid.build do |a|
|
||||
a.position = Vector2.new(x: rand(0.0..width.to_f), y: rand(0.0..height.to_f))
|
||||
a.velocity = Vector2.new(x: rand(-20.0..20.0), y: rand(-20.0..20.0))
|
||||
a.rotation_speed = rand(-5.0..5.0)
|
||||
a.size = rand(0.5..4.0)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -78,5 +96,5 @@ class Asteroids < Game
|
|||
end
|
||||
end
|
||||
|
||||
game = Asteroids.new(200, 120, 4)
|
||||
game = Asteroids.new(WIDTH, HEIGHT, SCALE)
|
||||
game.run!
|
||||
|
|
|
@ -5,15 +5,16 @@ module LxGame
|
|||
property width : Int32
|
||||
property height : Int32
|
||||
property scale : Int32
|
||||
property title : String
|
||||
|
||||
@fps_lasttime : Float64 = Time.monotonic.total_milliseconds # the last recorded time.
|
||||
@fps_current : UInt32 = 0 # the current FPS.
|
||||
@fps_frames : UInt32 = 0 # frames passed since the last recorded fps.
|
||||
@last_time : Float64 = Time.monotonic.total_milliseconds
|
||||
|
||||
def initialize(@width, @height, @scale = 1)
|
||||
def initialize(@width, @height, @scale = 1, @title = self.class.name)
|
||||
SDL.init(SDL::Init::VIDEO)
|
||||
@window = SDL::Window.new("SDL test", @width * @scale, @height * @scale)
|
||||
@window = SDL::Window.new(@title, @width * @scale, @height * @scale)
|
||||
@renderer = SDL::Renderer.new(@window, flags: SDL::Renderer::Flags::SOFTWARE)
|
||||
@renderer.scale = {@scale, @scale}
|
||||
end
|
||||
|
@ -33,7 +34,7 @@ module LxGame
|
|||
@fps_lasttime = et
|
||||
@fps_current = @fps_frames
|
||||
@fps_frames = 0
|
||||
@window.title = String.build { |io| io << "SDL test - " << @fps_current << " fps" }
|
||||
@window.title = String.build { |io| io << @title << " - " << @fps_current << " fps" }
|
||||
end
|
||||
|
||||
update((et - @last_time) / 1000.0)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module LxGame
|
||||
# Draw a line using Bresenham’s Algorithm
|
||||
def draw_line(renderer : SDL::Renderer, p1 : Vector2, p2 : Vector2, draw_points = false, thickness = 1)
|
||||
def draw_line(renderer : SDL::Renderer, p1 : Vector2, p2 : Vector2, draw_points = false, point_color = SDL::Color[255, 0, 0, 255])
|
||||
return draw_line(renderer, p2, p1) if p1.x > p2.x
|
||||
x1, y1, x2, y2 = p1.x.to_i, p1.y.to_i, p2.x.to_i, p2.y.to_i
|
||||
|
||||
|
@ -14,7 +14,7 @@ module LxGame
|
|||
x, y = x1, y1
|
||||
|
||||
loop do
|
||||
renderer.draw_point(x, y)
|
||||
draw_point(renderer, x, y)
|
||||
break if x == x2 && y == y2
|
||||
|
||||
d2 = d + d
|
||||
|
@ -31,9 +31,13 @@ module LxGame
|
|||
end
|
||||
|
||||
if draw_points
|
||||
renderer.draw_color = SDL::Color[255, 0, 0, 255]
|
||||
renderer.draw_point(x1, y1)
|
||||
renderer.draw_point(x2, y2)
|
||||
renderer.draw_color = point_color
|
||||
draw_point(renderer, x1, y1)
|
||||
draw_point(renderer, x2, y2)
|
||||
end
|
||||
end
|
||||
|
||||
def draw_point(renderer, x1, y1)
|
||||
renderer.draw_point(x1, y1)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ module LxGame
|
|||
|
||||
def self.generate_circle(num_points : Int, size = 1.0, jitter = 0.0) : Array(Vector2)
|
||||
0.upto(num_points).map do |n|
|
||||
angle = (2 * Math::PI) * (n / 12)
|
||||
angle = (2 * Math::PI) * (n / num_points)
|
||||
|
||||
x = size + rand(-jitter..jitter)
|
||||
|
||||
|
|
Loading…
Reference in a new issue