cars 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a9fab95e185db37d037c6aa02d53daef7d0266a
4
+ data.tar.gz: 494fb1f9325d8b573130dcbddde13a926e00f005
5
+ SHA512:
6
+ metadata.gz: fe5e941540932e8575ba35f75ec0ad049a3fa56dcdda29469188ef4f9bc8feb1717f2d9d54b6614db6f08d1d5d45015b214b21507e21cde233d27395e048db76
7
+ data.tar.gz: 1954d8f5f775c433885de7f1e004c7fa4ed323ca0b6ddbe0a32bf3224215ca07a4c117507d396c0017311a4b923bca683d4efdbb7bb3af5c2d9bf8da5243b84d
@@ -0,0 +1,2 @@
1
+ .DS_Store
2
+ *.gem
@@ -0,0 +1,90 @@
1
+ # Gosu Cars
2
+
3
+ ## Content
4
+
5
+ * [Description](#description)
6
+
7
+ * [Requirements](#requirements)
8
+
9
+ * [Run project](#run-project)
10
+
11
+ * [Controls](#controls)
12
+
13
+ * [Contribute](#contribute)
14
+
15
+ * [Author](#author)
16
+
17
+ * [License](#license)
18
+
19
+ <a name="description"/>
20
+
21
+ ## Description
22
+
23
+ This project is a simple car game in ruby using gosu (2d game library) packed in a gem
24
+
25
+ * [Ruby](https://www.ruby-lang.org/en/)
26
+
27
+ * [Gosu](https://www.libgosu.org/index.html)
28
+
29
+ <a name="requirements"/>
30
+
31
+ ## Requirements
32
+
33
+ In order to execute this project you need to install:
34
+
35
+ * [Ruby 2.2.3](https://www.ruby-lang.org/es/news/2015/08/18/ruby-2-2-3-released/)
36
+
37
+ You can check this with `ruby -v`
38
+
39
+ * [Gosu](https://www.libgosu.org/ruby.html)
40
+
41
+ Gosu installation prerequisites depend on OS
42
+
43
+ <a name="run-project"/>
44
+
45
+ ## Run project
46
+
47
+ 1. Clone the repo
48
+
49
+ $ git clone https://github.com/yovasx2/gosu-cars
50
+
51
+ 2. Move into it and install dependencies
52
+
53
+ $ cd gosu-cars && ruby -Ilib ./bin/cars
54
+
55
+ <a name="controls"/>
56
+
57
+ ## Controls
58
+
59
+ 1. `Esc` to exit
60
+ 2. `r` to restart
61
+ 3. `⇦` to move blue car to left
62
+ 4. `⇨` to move blue car to right
63
+
64
+ <a name="contribute"/>
65
+
66
+ ## Contribute
67
+
68
+ You can contribute to this project in many forms:
69
+
70
+ * [Reporting bugs](https://github.com/yovasx2/gosu-cars/issues)
71
+
72
+ * [Writing issues](https://github.com/yovasx2/gosu-cars/issues)
73
+
74
+ * Adding [features](https://github.com/yovasx2/gosu-cars/pulls) (examples)
75
+
76
+ <a name="author"/>
77
+
78
+ ## Author
79
+
80
+ 1. Giovanni Alberto García
81
+
82
+ * <a href="mailto:delirable@gmail.com">delirable@gmail.com</a>
83
+
84
+ * [github.com/yovasx2](http://github.com/yovasx2)
85
+
86
+ <a name="license"/>
87
+
88
+ ## License
89
+
90
+ [MIT License](http://choosealicense.com/licenses/mit/)
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pry'
4
+ require 'cars'
5
+
6
+ Cars::Window.new.show
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'cars'
3
+ s.version = '0.0.4'
4
+ s.date = '2017-04-07'
5
+ s.summary = 'A simple 2D cars game with gosu'
6
+ s.description = 'A 2D cars game with gosu'
7
+ s.authors = ['Giovanni Alberto García']
8
+ s.email = 'delirable@gmail.com'
9
+ s.files = `git ls-files`.split($/)
10
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
11
+ s.homepage = 'http://rubygems.org/gems/cars'
12
+ s.license = 'MIT'
13
+ s.add_runtime_dependency 'gosu', '~> 0.11', '0.11.3.1'
14
+ s.add_development_dependency 'pry', '~> 0.10', '>= 0.10.4'
15
+ s.required_ruby_version = '>= 1.8.6'
16
+ end
@@ -0,0 +1,8 @@
1
+ require 'gosu'
2
+
3
+ require 'cars/window'
4
+ require 'cars/bump_generator'
5
+ require 'cars/enemy'
6
+ require 'cars/enemy_generator'
7
+ require 'cars/player'
8
+ require 'cars/z_order'
@@ -0,0 +1,31 @@
1
+ module Cars
2
+ class BumpGenerator
3
+ def initialize(window)
4
+ @left = Gosu::Image.new(File.join(Gosu::Image::MEDIA_PATH, 'left.png'))
5
+ @right = Gosu::Image.new(File.join(Gosu::Image::MEDIA_PATH, 'right.png'))
6
+ @window = window
7
+ @y1, @y2 =-window.height, 0
8
+ end
9
+
10
+ def update
11
+ @y1 += @window.enemies.level*3
12
+ @y2 += @window.enemies.level*3
13
+ # If bump is not visible, queue it inthe top
14
+ @y2 = -@window.height if @y2 >= @window.height
15
+ @y1 = -@window.height if @y1 >= @window.height
16
+ end
17
+
18
+ def draw
19
+ left_padding = @window.padding_road - @left.width
20
+ right_padding = @window.back.width - @window.padding_road
21
+
22
+ # Hidden
23
+ @left.draw(left_padding, @y1, ZOrder::DECORATION)
24
+ @right.draw(right_padding, @y1, ZOrder::DECORATION)
25
+
26
+ # Visible
27
+ @left.draw(left_padding, @y2, ZOrder::DECORATION)
28
+ @right.draw(right_padding, @y2, ZOrder::DECORATION)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,43 @@
1
+ module Cars
2
+ class Enemy
3
+ attr_reader :x, :y
4
+
5
+ def initialize(window)
6
+ @window = window
7
+ @car = Gosu::Image.new(File.join(Gosu::Image::MEDIA_PATH,'red.png'))
8
+ col = rand(3)
9
+ # Center car in road
10
+ @x = window.padding_road + rand(window.road.width*2-@car.width)
11
+ @y = -@car.height
12
+ end
13
+
14
+ def update(level)
15
+ speed = level * 2
16
+ @y += speed
17
+ end
18
+
19
+ def draw
20
+ @car.draw(@x, @y, ZOrder::CAR)
21
+ end
22
+
23
+ def passed?
24
+ @y >= @window.height
25
+ end
26
+
27
+ def height
28
+ @car.height
29
+ end
30
+
31
+ def width
32
+ @car.width
33
+ end
34
+
35
+ def collision?(other)
36
+ x_collision = other.x < @x && @x < other.x + @car.width ||
37
+ other.x < @x + @car.width && @x + @car.width < other.x + @car.width
38
+ y_collision = other.y < @y && @y < other.y + @car.height ||
39
+ other.y < @y + @car.height && @y + @car.height < other.y + @car.height
40
+ x_collision && y_collision
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,52 @@
1
+ module Cars
2
+ class EnemyGenerator
3
+ attr_reader :score, :level
4
+
5
+ def initialize(window)
6
+ @window = window
7
+ @gap = 0
8
+ @enemies = []
9
+ @score = 0
10
+ @level = 1
11
+ end
12
+
13
+ def update
14
+ add_enemy if @gap % @window.height == 0
15
+ @gap += 2
16
+ update_enemies(level)
17
+ delete_passed_enemies
18
+ @level = 1 + @score / 50
19
+ end
20
+
21
+ def draw
22
+ draw_enemies
23
+ end
24
+
25
+ def collision?(player)
26
+ collided = false
27
+ @enemies.each do |enemy|
28
+ collided ||= enemy.collision?(player)
29
+ end
30
+ collided
31
+ end
32
+
33
+ private
34
+
35
+ def add_enemy
36
+ @enemies << Enemy.new(@window)
37
+ end
38
+
39
+ def delete_passed_enemies
40
+ @score += 10 unless @enemies.select { |enemy| enemy.passed? }.empty?
41
+ @enemies.reject! { |enemy| enemy.passed? }
42
+ end
43
+
44
+ def draw_enemies
45
+ @enemies.each { |enemy| enemy.draw }
46
+ end
47
+
48
+ def update_enemies(level)
49
+ @enemies.each { |enemy| enemy.update(level) }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,63 @@
1
+ module Cars
2
+ class Player
3
+ attr_reader :x, :y, :alive
4
+
5
+ def initialize(window)
6
+ @window = window
7
+ @car = Gosu::Image.new(File.join(Gosu::Image::MEDIA_PATH, 'blue.png'))
8
+ reset
9
+ end
10
+
11
+ def turn_left
12
+ if @x > @window.padding_road
13
+ @speed += 8 * @window.delta * @window.enemies.level
14
+ @x -= @speed if @x > @window.padding_road + @speed
15
+ @moved_at = Gosu::milliseconds
16
+ end
17
+ end
18
+
19
+ def turn_right
20
+ if @x < @window.padding_road + @window.road.width*2 - @car.width
21
+ @speed += 8 * @window.delta * @window.enemies.level
22
+ @x += @speed if @x < @window.padding_road + @window.road.width*2 - @car.width - @speed
23
+ @moved_at = Gosu::milliseconds
24
+ end
25
+ end
26
+
27
+ def update
28
+ turn_left if Gosu.button_down? Gosu::KB_LEFT
29
+ turn_right if Gosu.button_down? Gosu::KB_RIGHT
30
+ reset_speed
31
+ end
32
+
33
+ def draw
34
+ @car.draw(@x, @y, ZOrder::CAR)
35
+ end
36
+
37
+ def height
38
+ @car.height
39
+ end
40
+
41
+ def width
42
+ @car.width
43
+ end
44
+
45
+ def kill
46
+ @alive = false
47
+ end
48
+
49
+ private
50
+
51
+ def reset
52
+ @x = @window.padding_road + rand(@window.road.width * 2 - @car.width)
53
+ @y = @window.height - @car.height - 20
54
+ @alive = true
55
+ @moved_at = Gosu::milliseconds
56
+ @speed = @window.enemies.level
57
+ end
58
+
59
+ def reset_speed
60
+ @speed = @window.enemies.level if(Gosu::milliseconds > @moved_at + @window.delta)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,72 @@
1
+ module Cars
2
+ class Window < Gosu::Window
3
+ attr_reader :back, :padding_road, :road, :delta, :enemies
4
+
5
+ WIDTH, HEIGHT = 800, 598
6
+
7
+ def initialize
8
+ super(WIDTH, HEIGHT)
9
+ caption = "Cars"
10
+ reset
11
+ end
12
+
13
+ def update
14
+ update_delta
15
+ if @player.alive
16
+ @bumps.update
17
+ @enemies.update
18
+ @player.update
19
+ @player.kill if @enemies.collision?(@player)
20
+ else
21
+ reset if button_down? Gosu::KB_R
22
+ end
23
+ end
24
+
25
+ def draw
26
+ draw_assets
27
+ @bumps.draw
28
+ @enemies.draw
29
+ @player.draw
30
+ @font.draw('Game Over', 300, 200, ZOrder::UI, 2, 2, Gosu::Color::BLACK) unless @player.alive
31
+ @font.draw('Score', 20, 10, ZOrder::UI)
32
+ @font.draw('%06i' % @enemies.score, 20, 30, ZOrder::UI)
33
+ @font.draw('Level' % 0, 20, 70, ZOrder::UI)
34
+ @font.draw('%02i' % @enemies.level, 20, 90, ZOrder::UI)
35
+ end
36
+
37
+ def button_down(id)
38
+ id == Gosu::KB_ESCAPE ? close : super
39
+ end
40
+
41
+ private
42
+
43
+ def load_assets
44
+ @back = Gosu::Image.new(File.join(Gosu::Image::MEDIA_PATH, 'back.png'))
45
+ @road = Gosu::Image.new(File.join(Gosu::Image::MEDIA_PATH, 'road.png'))
46
+ @font ||= Gosu::Font.new(20)
47
+
48
+ # Left paddings
49
+ @padding_road = (@back.width - @road.width*2)/2
50
+ end
51
+
52
+ def draw_assets
53
+ # Draw images
54
+ @back.draw(0, 0, ZOrder::BACKGROUND)
55
+ @road.draw(@padding_road, 0, ZOrder::DECORATION, 2)
56
+ end
57
+
58
+ def reset
59
+ load_assets
60
+ @bumps = BumpGenerator.new(self)
61
+ @enemies = EnemyGenerator.new(self)
62
+ @player = Player.new(self)
63
+ @last_time = 0
64
+ end
65
+
66
+ def update_delta
67
+ current_time = Time.now.to_i
68
+ @delta = [current_time - @last_time, 0.25].min
69
+ @last_time = current_time
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,9 @@
1
+ module ZOrder
2
+ BACKGROUND, DECORATION, CAR, UI = *0..3
3
+ end
4
+
5
+ module Gosu
6
+ class Image
7
+ MEDIA_PATH = File.expand_path("../../media", __FILE__)
8
+ end
9
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cars
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Giovanni Alberto García
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gosu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ - - '='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.11.3.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.11'
30
+ - - '='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.11.3.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: pry
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.10'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.10.4
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.10'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.10.4
53
+ description: A 2D cars game with gosu
54
+ email: delirable@gmail.com
55
+ executables:
56
+ - cars
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".gitignore"
61
+ - README.md
62
+ - bin/cars
63
+ - cars.gemspec
64
+ - lib/cars.rb
65
+ - lib/cars/bump_generator.rb
66
+ - lib/cars/enemy.rb
67
+ - lib/cars/enemy_generator.rb
68
+ - lib/cars/player.rb
69
+ - lib/cars/window.rb
70
+ - lib/cars/z_order.rb
71
+ - lib/media/back.png
72
+ - lib/media/blue.png
73
+ - lib/media/left.png
74
+ - lib/media/red.png
75
+ - lib/media/right.png
76
+ - lib/media/road.png
77
+ homepage: http://rubygems.org/gems/cars
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 1.8.6
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.11
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A simple 2D cars game with gosu
101
+ test_files: []