pacman_semestral 0.1.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 499cfb014cdf5384a0f483a3ddfb5286eb6cb9e2
4
+ data.tar.gz: acbe831ce691c2c04c7f6d11beafcd220ae12f42
5
+ SHA512:
6
+ metadata.gz: 8c2a2333793b1c5d9babecbce5f6c6359a78c15ff8ecbc55e88e68b9dd67cd5f880ea41ffdd99d66d554856f2e12bd177c5c38beb08de68bd191b616b267ac1c
7
+ data.tar.gz: 57e80fd0ee6058fa292e5c86ffbbcb989946e97b497ada2a76158abaff983edb6ca255719689b55dad94a62e8935c26800890c14570ff27064e88e73e676a874
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pacman_semestral.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # PacmanSemestral
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pacman_semestral`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pacman_semestral'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pacman_semestral
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pacman_semestral. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pacman_semestral"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/mapa.txt ADDED
@@ -0,0 +1,24 @@
1
+ #######################
2
+ #**********#**********#
3
+ #@###*####*#*####*###@#
4
+ #*###*####*#*####*###*#
5
+ #*********************#
6
+ #*###*#*#######*#*###*#
7
+ #*****#*#######*#*****#
8
+ #####*#****#****#*#####
9
+ #####*#### # ####*#####
10
+ #####*# #*#####
11
+ #####*# ### ### #*#####
12
+ * # M # *
13
+ #####*# ####### #*#####
14
+ #####*# #*#####
15
+ #####*# ####### #*#####
16
+ #####*# ####### #*#####
17
+ #**********#**********#
18
+ #*###*####*#*####*###*#
19
+ #@**#******P******#**@#
20
+ ###*#*#*#######*#*#*###
21
+ #*****#****#****#*****#
22
+ #*########*#*########*#
23
+ #*********************#
24
+ #######################
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bash
2
+
3
+ require 'pacman_semestral.rb'
4
+
5
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/area.rb ADDED
@@ -0,0 +1,109 @@
1
+ require 'pacman.rb'
2
+
3
+ ##
4
+ # Trida Area reprezentuje mapu, kde se odehrava hra
5
+ class Area
6
+ ##
7
+ # Konstruktor vytvoti novou mapu
8
+ def initialize
9
+ @crofts = []
10
+ @map = 'mapa.txt'
11
+ load_map
12
+ end
13
+
14
+ def load_map
15
+ File.open(@map, 'r') do |f|
16
+ f.each_line do |line|
17
+ @crofts.push(line_parse(line))
18
+ end
19
+ end
20
+ end
21
+
22
+ def line_parse(line)
23
+ c = CROFTWIDTH
24
+ t_l_c = []
25
+ line.split('').each do |char|
26
+ left = t_l_c.length * c
27
+ top = @crofts.length * c + Y_START
28
+ pac_or_gh(char, left, top) if char == 'P' || char == 'M'
29
+ t_l_c.push(Croft.new(left, top, char))
30
+ end
31
+ t_l_c
32
+ end
33
+
34
+ def pac_or_gh(char, left, top)
35
+ if char == 'P'
36
+ @pc_pos = { x: left, y: top }
37
+ elsif char == 'M'
38
+ @gh_pos = { x: left, y: top }
39
+ end
40
+ end
41
+
42
+ # Pole policek pro vykrelseni mapy
43
+ attr_reader :crofts
44
+
45
+ # vychozi pozice pacmana na mape
46
+ attr_reader :pc_pos
47
+
48
+ # vychozi pozice duchu na mape
49
+ attr_reader :gh_pos
50
+
51
+ ##
52
+ # Overuje, jestli souradnice koliduji s polickem na mape
53
+ # * *Args* :
54
+ # - +l_c+ -> poloha na ose X
55
+ # - +t_c+ -> poloha na ose Y
56
+ # * *Returns* :
57
+ # - true -> souradnice nekoliduji
58
+ # - false -> souradnice koliduji
59
+ def colidate(l_c, t_c)
60
+ @crofts.each do |line_croft|
61
+ line_croft.each do |croft|
62
+ return false if croft.type == '#' && colision?(l_c, t_c, croft, 1)
63
+ end
64
+ end
65
+ true
66
+ end
67
+
68
+ def colision?(l_c, t_c, croft, param)
69
+ c = CROFTWIDTH
70
+ c_l_c = l_c - (croft.left + c / 2)
71
+ c_t_c = t_c - (croft.top + c / 2)
72
+ return true if c_l_c.abs < c / param && c_t_c.abs < c / param
73
+ false
74
+ end
75
+
76
+ ##
77
+ # Overuje, jestli souradnice koliduji s bonusem na mape
78
+ # * *Args* :
79
+ # - +l_c+ -> poloha na ose X
80
+ # - +t_c+ -> poloha na ose Y
81
+ # * *Returns* :
82
+ # - 0 -> souradnice nekoliduji
83
+ # - 1 -> souradnice koliduji s malym bonusem
84
+ # - 10 -> souradnice koliduji s velkym bonusem
85
+ def bonus(l_c, t_c)
86
+ @crofts.each do |line_croft|
87
+ line_croft.each do |croft|
88
+ if !croft.visited && colision?(l_c, t_c, croft, 2)
89
+ is_bonus_val = bonus?(croft)
90
+ return is_bonus_val if is_bonus_val > 0
91
+ end
92
+ end
93
+ end
94
+ 0
95
+ end
96
+
97
+ def bonus?(croft)
98
+ if croft.type == '*'
99
+ croft.set_visited
100
+ return 1
101
+ elsif croft.type == '@'
102
+ croft.set_visited
103
+ return 10
104
+ end
105
+ 0
106
+ end
107
+
108
+ private :load_map, :bonus?, :colision?, :pac_or_gh, :line_parse
109
+ end
data/lib/croft.rb ADDED
@@ -0,0 +1,40 @@
1
+ ##
2
+ # Trida Croft reprezentuje policko na mape.
3
+ class Croft
4
+ ##
5
+ # Vytvori novou instanci policka
6
+ # * *Args* :
7
+ # - +x+ -> poloha na ose X
8
+ # - +y+ -> poloha na ose Y
9
+ # - +char+ -> typ policka
10
+ def initialize(x, y, char)
11
+ @left = x
12
+ @top = y
13
+ @type = char
14
+ @visited = false
15
+ end
16
+
17
+ # Reprezentuje poradi policka v poli grafickych obrazcu
18
+ attr_reader :index
19
+
20
+ # Reprezentuje typ policka
21
+ attr_reader :type
22
+
23
+ # Reprezentuje polohu policka na ose X
24
+ attr_reader :left
25
+
26
+ # Reprezentuje polohu policka na ose X
27
+ attr_reader :top
28
+
29
+ # Reprezentuje hodnotu, ktera rika jesli bylo policko
30
+ # s bonusem navstiveno hracem
31
+ attr_reader :visited
32
+
33
+ attr_writer :index
34
+
35
+ ##
36
+ # Nastavi promennou visited na true
37
+ def set_visited
38
+ @visited = true
39
+ end
40
+ end
data/lib/figure.rb ADDED
@@ -0,0 +1,223 @@
1
+ require 'pacman.rb'
2
+
3
+ ##
4
+ # Trida Figure reprezentuje postavy ve hre.
5
+ class Figure
6
+ ##
7
+ # Vytvori novou postavu umestenou na mape v poloze +left+ a +top+,
8
+ # pohybujici se rychlosti +speed+ a vychozim smerem pohybu +r+.
9
+ # * *Args* :
10
+ # - +left+ -> poloha na ose X
11
+ # - +top+ -> poloha na ose Y
12
+ # - +speed+ -> poloha na ose rychlost pohybu
13
+ # - +r+ -> vychozi smer pohybu
14
+ def initialize(left, top, speed, r)
15
+ @directions = [:left, :right, :up, :down]
16
+ @left = left
17
+ @top = top
18
+ @future_left = @left
19
+ @future_top = @top
20
+ @speed = speed
21
+ @direction = @directions[r]
22
+ end
23
+
24
+ # Smer pohybu
25
+ attr_writer :direction
26
+
27
+ def modulo
28
+ if @left > WIDTH
29
+ @left = 0
30
+ elsif @left < 0
31
+ @left = WIDTH
32
+ end
33
+ end
34
+
35
+ ##
36
+ # Overi, jestli se postava puze dale pohybovat soucasnym smerem
37
+ # v oblasti +area+
38
+ # * *Args* :
39
+ # - +area+ -> mapa hry
40
+ # * *Returns* :
41
+ # - true -> postava se muze dal pohybovat stejnym smrem
42
+ # - false -> postava musi zmeni smer pohybu
43
+ def can_move(area)
44
+ future_move
45
+ area.colidate(@future_left + CROFTWIDTH / 2, @future_top + CROFTWIDTH / 2)
46
+ end
47
+
48
+ ##
49
+ # Posune hrace soucasnym smerem
50
+ def move
51
+ real_move
52
+ modulo
53
+ future_make
54
+ end
55
+
56
+ def real_move
57
+ if @direction == :left
58
+ @left -= speed
59
+ elsif @direction == :right
60
+ @left += speed
61
+ elsif @direction == :up
62
+ @top -= speed
63
+ else
64
+ @top += speed
65
+ end
66
+ end
67
+
68
+ def future_make
69
+ @future_left = @left
70
+ @future_top = @top
71
+ end
72
+
73
+ def future_move
74
+ future_make
75
+ if @direction == :left
76
+ @future_left -= speed
77
+ elsif @direction == :right
78
+ @future_left += speed
79
+ elsif @direction == :up
80
+ @future_top -= speed
81
+ else
82
+ @future_top += speed
83
+ end
84
+ end
85
+
86
+ private :future_move, :future_make, :real_move, :modulo
87
+
88
+ # Pozice na ose X
89
+ attr_reader :left
90
+
91
+ # Pozice na ose Y
92
+ attr_reader :top
93
+
94
+ # Rychlost pohybu
95
+ attr_reader :speed
96
+
97
+ attr_reader :direction
98
+ end
99
+
100
+ ##
101
+ # Trida Ghost reprezentuje pocitacem ovladane duchy.
102
+ class Ghost < Figure
103
+ ##
104
+ # Vytvori noveho ducha umesteneho na mape v poloze +left+ a +top+,
105
+ # pohybujiciho se rychlosti +speed+ a vychozim smerem pohybu +r+.
106
+ # * *Args* :
107
+ # - +left+ -> poloha na ose X
108
+ # - +top+ -> poloha na ose Y
109
+ # - +speed+ -> poloha na ose rychlost pohybu
110
+ # - +r+ -> vychozi smer pohybu
111
+ def initialize(left, top, speed, r)
112
+ super(left, top, speed, r)
113
+ @am_i_blue = false
114
+ end
115
+
116
+ # reprezentuje stav ducha
117
+ attr_reader :am_i_blue
118
+
119
+ # duch ma moznost zmenit rychlost pohybu
120
+ attr_writer :am_i_blue, :speed
121
+
122
+ ##
123
+ # Pokud se duch srazi s hracem +p+ tak ho bud sni (hra konci),
124
+ # nebo je snezen a vrati se na vychozi umisteni.
125
+ # * *Args* :
126
+ # - +p+ -> postava pacmana
127
+ # * *Returns* :
128
+ # - GAME_OVER -> pacman byl snezen a hra konci
129
+ # - I GO HOME -> duch byl snezen a vraci se
130
+ def eat_pacman(p)
131
+ c = CROFTWIDTH
132
+ return unless (@left - p.left).abs < c - 18 && (@top - p.top).abs < c - 18
133
+ if !am_i_blue
134
+ 'GAME_OVER'
135
+ else
136
+ 'I GO HOME'
137
+ end
138
+ end
139
+
140
+ ##
141
+ # Vyzkousi kam je mozne se pohybovat a nastavi novy smer pohybu
142
+ # * *Args* :
143
+ # - +area+ -> mapa hry
144
+ def try_set_direction(area)
145
+ original_direction = @direction
146
+ possible_moves = []
147
+ @directions.each do |dir|
148
+ @direction = dir
149
+ possible_moves.push(dir) if can_move(area)
150
+ end
151
+ select_direction(original_direction, possible_moves)
152
+ end
153
+
154
+ def select_direction(original_direction, possible_moves)
155
+ if possible_moves.length == 1 || possible_moves.empty?
156
+ @direction = possible_moves[0]
157
+ else
158
+ r = Random.rand(possible_moves.length)
159
+ while oposit?(original_direction, possible_moves[r])
160
+ r = Random.rand(possible_moves.length)
161
+ end
162
+ @direction = possible_moves[r]
163
+ end
164
+ end
165
+
166
+ def oposit?(org, dir)
167
+ return true if compare_l_r(org, dir) || compare_t_d(org, dir)
168
+ false
169
+ end
170
+
171
+ def compare_t_d(org, dir)
172
+ td1 = org == :up && dir == :down
173
+ td2 = org == :down && dir == :up
174
+ return true if td1 || td2
175
+ false
176
+ end
177
+
178
+ def compare_l_r(org, dir)
179
+ lr1 = org == :left && dir == :right
180
+ lr2 = org == :right && dir == :left
181
+ return true if lr1 || lr2
182
+ false
183
+ end
184
+
185
+ private :compare_l_r, :compare_t_d, :oposit?, :select_direction
186
+ end
187
+
188
+ ##
189
+ # Trida Player predstavuje postavu ovladanou hracem (PACMANA)
190
+ class Player < Figure
191
+ ##
192
+ # Vytvori noveho hrace umesteneho na mape v poloze +left+ a +top+,
193
+ # pohybujiciho se rychlosti +speed+ a vychozim smerem pohybu +r+.
194
+ # * *Args* :
195
+ # - +left+ -> poloha na ose X
196
+ # - +top+ -> poloha na ose Y
197
+ # - +speed+ -> poloha na ose rychlost pohybu
198
+ # - +r+ -> vychozi smer pohybu
199
+ def initialize(left, top, speed, r)
200
+ super(left, top, speed, r)
201
+ @score = 0
202
+ end
203
+
204
+ ##
205
+ # Zkontroluje, jestli hrac sebral bonus v mape +area+.
206
+ # Pokud ano, zvysi skore.
207
+ # * *Args* :
208
+ # - +area+ -> mapa hry
209
+ def collect_bonus(area)
210
+ score = area.bonus(@left + CROFTWIDTH / 2, @top + CROFTWIDTH / 2)
211
+ @score += score
212
+ score
213
+ end
214
+
215
+ ##
216
+ # Pricte skore za znezeni ducha.
217
+ def you_eat_them
218
+ @score += 100
219
+ end
220
+
221
+ # reprezentuje hodnotu skore, ktereho hrac dosahl
222
+ attr_reader :score
223
+ end
data/lib/mapa.txt ADDED
@@ -0,0 +1,24 @@
1
+ #######################
2
+ #**********#**********#
3
+ #@###*####*#*####*###@#
4
+ #*###*####*#*####*###*#
5
+ #*********************#
6
+ #*###*#*#######*#*###*#
7
+ #*****#*#######*#*****#
8
+ #####*#****#****#*#####
9
+ #####*#### # ####*#####
10
+ #####*# #*#####
11
+ #####*# ### ### #*#####
12
+ * # M # *
13
+ #####*# ####### #*#####
14
+ #####*# #*#####
15
+ #####*# ####### #*#####
16
+ #####*# ####### #*#####
17
+ #**********#**********#
18
+ #*###*####*#*####*###*#
19
+ #@**#******P******#**@#
20
+ ###*#*#*#######*#*#*###
21
+ #*****#****#****#*****#
22
+ #*########*#*########*#
23
+ #*********************#
24
+ #######################
data/lib/pacman.rb ADDED
@@ -0,0 +1,104 @@
1
+ require 'figure.rb'
2
+ require 'croft.rb'
3
+ require 'area.rb'
4
+
5
+ # Velikost jednoho policka na mape
6
+ CROFTWIDTH = 30
7
+
8
+ # Pozice pro zacatek vykreslovani na ose Y
9
+ Y_START = 0
10
+
11
+ # Sirka okna hry
12
+ WIDTH = 23 * CROFTWIDTH
13
+
14
+ # Vyska okna hry
15
+ HEIGHT = 24 * CROFTWIDTH + Y_START
16
+
17
+ ##
18
+ # Trida Pacman je hlavni trida ktera reprezentuje celou hru.
19
+ class Pacman
20
+ ##
21
+ # Vytvori novou instanci hry
22
+ def initialize
23
+ @pc_speed = 10
24
+ @gh_sp = 15
25
+ @time_mark = 0
26
+ @area = Area.new
27
+ @ghosts = []
28
+ make_ghosts
29
+ @player = Player.new(@area.pc_pos[:x], @area.pc_pos[:y], @pc_speed, 1)
30
+ end
31
+
32
+ def make_ghosts
33
+ 4.times do
34
+ @ghosts.push Ghost.new(@area.gh_pos[:x], @area.gh_pos[:y], @gh_sp, 2)
35
+ end
36
+ end
37
+
38
+ ##
39
+ # Zmeni smer pohybu hrace na +direction+
40
+ # * *Args* :
41
+ # - +direction+ -> novy smer pohybu hrace
42
+ def move(direction)
43
+ @player.direction = direction
44
+ end
45
+
46
+ ##
47
+ # Aktualizuje stav hry
48
+ # * *Returns* :
49
+ # - GAME_OVER -> Konec hry
50
+ def update
51
+ try_time
52
+ @player.move if @player.can_move(@area)
53
+ if @player.collect_bonus(@area) == 10
54
+ make_ghosts_blue
55
+ @time_mark = Time.now
56
+ end
57
+ update_ghosts
58
+ end
59
+
60
+ def update_ghosts
61
+ @ghosts.each do |ghost|
62
+ ghost.try_set_direction(@area)
63
+ ghost.move
64
+ if decide(ghost.eat_pacman(@player), @ghosts.index(ghost)) == true
65
+ return 'GAME_OVER'
66
+ end
67
+ end
68
+ end
69
+
70
+ def try_time
71
+ return unless @time_mark != 0 && Time.now - @time_mark >= 5
72
+ @ghosts.each do |ghost|
73
+ ghost.am_i_blue = false
74
+ ghost.speed = @gh_sp
75
+ end
76
+ @time_mark = 0
77
+ end
78
+
79
+ def decide(value, ghost)
80
+ return true if value == 'GAME_OVER'
81
+ return unless value == 'I GO HOME'
82
+ @ghosts[ghost] = Ghost.new(@area.gh_pos[:x], @area.gh_pos[:y], @gh_sp, 2)
83
+ @player.you_eat_them
84
+ false
85
+ end
86
+
87
+ def make_ghosts_blue
88
+ @ghosts.each do |ghost|
89
+ ghost.am_i_blue = true
90
+ ghost.speed = 5
91
+ end
92
+ end
93
+
94
+ private :make_ghosts, :make_ghosts_blue, :update_ghosts, :decide, :try_time
95
+
96
+ # pole duchu ve hre
97
+ attr_reader :ghosts
98
+
99
+ # mapa hry
100
+ attr_reader :area
101
+
102
+ # postava ovladana hracem
103
+ attr_reader :player
104
+ end
@@ -0,0 +1,131 @@
1
+ require 'pacman_semestral/version'
2
+
3
+ require 'green_shoes'
4
+ require 'figure.rb'
5
+
6
+ Shoes.app height: HEIGHT, width: WIDTH do
7
+ @game = Pacman.new
8
+ @ghs = @game.ghosts
9
+ @plr = @game.player
10
+ background black
11
+ keypress do |k|
12
+ case k
13
+ when 'Left' then @game.move(:left)
14
+ when 'Right' then @game.move(:right)
15
+ when 'Down' then @game.move(:down)
16
+ when 'Up' then @game.move(:up)
17
+ end
18
+ end
19
+ @c = CROFTWIDTH
20
+ cm = @c - 10
21
+ @player = oval radius: @c / 2, center: false, fill: yellow
22
+ @player.move(@plr.left, @plr.top)
23
+ @red = rect @ghs[0].left + 5, @ghs[0].top + 5, cm, cm, fill: red
24
+ @pink = rect @ghs[1].left + 5, @ghs[1].top + 5, cm, cm, fill: deeppink
25
+ @green = rect @ghs[2].left + 5, @ghs[2].top + 5, cm, cm, fill: green
26
+ @orange = rect @ghs[3].left + 5, @ghs[3].top + 5, cm, cm, fill: orange
27
+ @bonuses = []
28
+
29
+ ##
30
+ # vykresli mapu
31
+ def area_build
32
+ @game.area.crofts.each do |line_crofts|
33
+ line_crofts.each do |croft|
34
+ rect croft.left, croft.top, @c, @c, fill: blue if croft.type == '#'
35
+ end
36
+ end
37
+ draw_bonuses
38
+ end
39
+
40
+ ##
41
+ # vykresli vsechny postavy
42
+ def draw_figures
43
+ move(@player, @plr.left, @plr.top)
44
+ move(@red, @ghs[0].left + 5, @ghs[0].top + 5)
45
+ move(@pink, @ghs[1].left + 5, @ghs[1].top + 5)
46
+ move(@green, @ghs[2].left + 5, @ghs[2].top + 5)
47
+ move(@orange, @ghs[3].left + 5, @ghs[3].top + 5)
48
+ draw_colors
49
+ end
50
+
51
+ ##
52
+ # Posune obraz postavy
53
+ # * *Args* :
54
+ # - +itm+ -> postava
55
+ # - +left+ -> poloha na ose X
56
+ # - +top+ -> poloha na ose Y
57
+ def move(itm, left, top)
58
+ itm.left = left
59
+ itm.top = top
60
+ end
61
+
62
+ ##
63
+ # vykresli barvy pro vsechny duchy
64
+ def draw_colors
65
+ draw(@red, 0, red)
66
+ draw(@pink, 1, deeppink)
67
+ draw(@green, 2, green)
68
+ draw(@orange, 3, orange)
69
+ end
70
+
71
+ ##
72
+ # vykresli barvu ducha
73
+ # * *Args* :
74
+ # - +ghost+ -> duch
75
+ # - +ind+ -> index ducha
76
+ # - +color+ -> barva ducha
77
+ def draw(ghost, ind, color)
78
+ ghost.style(fill: blue) if @ghs[ind].am_i_blue
79
+ ghost.style(fill: color) unless @ghs[ind].am_i_blue
80
+ end
81
+
82
+ ##
83
+ # vykresli bonusy
84
+ def draw_bonuses
85
+ @game.area.crofts.each do |line_crofts|
86
+ line_crofts.each do |cr|
87
+ t = cr.top + @c / 3
88
+ push_small_bonus(cr, t)
89
+ push_big_bonus(cr, t)
90
+ end
91
+ end
92
+ end
93
+
94
+ ##
95
+ # prida do pole bonusu maly bonus
96
+ def push_small_bonus(cr, t)
97
+ return unless cr.type == '*'
98
+ o = oval left: cr.left + @c / 3, top: t, radius: @c / 6, fill: white
99
+ @bonuses.push(o)
100
+ cr.index = @bonuses.length - 1
101
+ end
102
+
103
+ ##
104
+ # prida do pole bonusu velky bonus
105
+ def push_big_bonus(cr, t)
106
+ return unless cr.type == '@'
107
+ o = oval left: cr.left + @c / 3.8, top: t, radius: @c / 4, fill: red
108
+ @bonuses.push(o)
109
+ cr.index = @bonuses.length - 1
110
+ end
111
+
112
+ ##
113
+ # prekresli bonusy
114
+ def update_bonuses
115
+ @game.area.crofts.each do |line_crofts|
116
+ line_crofts.each do |croft|
117
+ @bonuses[croft.index].clear if croft.visited
118
+ end
119
+ end
120
+ end
121
+
122
+ area_build
123
+ animate 8 do
124
+ if @game.update == 'GAME_OVER'
125
+ alert('GAME OVER SCORE=' + @plr.score.to_s)
126
+ break
127
+ end
128
+ update_bonuses
129
+ draw_figures
130
+ end
131
+ end
@@ -0,0 +1,6 @@
1
+ ##
2
+ # modul s verzi hry
3
+ module PacmanSemestral
4
+ # verze hry
5
+ VERSION = "0.1.1"
6
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pacman_semestral/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pacman_semestral"
8
+ spec.version = PacmanSemestral::VERSION
9
+ spec.authors = ["David Vondras"]
10
+ spec.email = ["vondrda9@fit.cvut.cz"]
11
+
12
+ spec.summary = %q{Pacman game}
13
+ spec.description = %q{Semestral work mi-rub}
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ #spec.metadata['allowed_push_host'] = "allowed_push_host"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files`.split($/)
26
+ spec.bindir = "bin"
27
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency "green_shoes"
31
+ spec.add_development_dependency "bundler", "~> 1.13"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pacman_semestral
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - David Vondras
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: green_shoes
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Semestral work mi-rub
56
+ email:
57
+ - vondrda9@fit.cvut.cz
58
+ executables:
59
+ - console
60
+ - mapa.txt
61
+ - pacman_semestral
62
+ - setup
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - ".gitignore"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/mapa.txt
74
+ - bin/pacman_semestral
75
+ - bin/setup
76
+ - lib/area.rb
77
+ - lib/croft.rb
78
+ - lib/figure.rb
79
+ - lib/mapa.txt
80
+ - lib/pacman.rb
81
+ - lib/pacman_semestral.rb
82
+ - lib/pacman_semestral/version.rb
83
+ - pacman_semestral.gemspec
84
+ homepage:
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Pacman game
108
+ test_files: []