Class: GamesParadise::Player

Inherits:
Tank
  • Object
show all
Defined in:
lib/games_paradise/gui/gosu/battle_city/player.rb,
lib/games_paradise/gui/gosu/final_fantasy/player.rb,
lib/games_paradise/tic_tac_toe/tic_tac_toe_with_AI/player.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Tank

#move_east, #move_north, #move_south, #move_west, #nearest_obj, #reset, #sense_collide

Constructor Details

#initialize(i) ⇒ Player

#

initialize

#


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
# File 'lib/games_paradise/tic_tac_toe/tic_tac_toe_with_AI/player.rb', line 16

def initialize(window)
  @window = window
  # tank size (consistently square 56px)
  @tank_west = Gosu::Image.new("media/blue_tank_west.png")
  @tank_east = Gosu::Image.new("media/blue_tank_east.png")
  @tank_north = Gosu::Image.new("media/blue_tank_north.png")
  @tank_south = Gosu::Image.new("media/blue_tank_south.png")
  @x = 260
  @y = 644 
  @head_west, @head_east, @head_north, @head_south = false, false, true, false       
  @cannon = Cannon.new(self)
  @wall = Wall.new
  @bricks = @wall.bricks
  @wall_units = @wall.wall_units
  @enemyteam = EnemyTeam.new(self)
  @enemytanks = @enemyteam.enemy_team
  @explosion = Gosu::Image.load_tiles("media/tank_explode.png", 72, 72)
  @fort = Fort.new
  @bombed_tank = 0
  @win = false
  @hit_brick = Gosu::Sample.new("media/distant_explosion.mp3")
  @hit_tank = Gosu::Sample.new("media/tank_explosion.mp3")
  @lives = 3
  @exploded = false
  @time_hit = nil
  @loc_x = 0
  @loc_y = 0
  @text_display = Gosu::Font.new(@window,"FUTURA", 50)
end

Instance Attribute Details

#bricksObject (readonly)

Returns the value of attribute bricks.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def bricks
  @bricks
end

#head_eastObject (readonly)

Returns the value of attribute head_east.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def head_east
  @head_east
end

#head_northObject (readonly)

Returns the value of attribute head_north.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def head_north
  @head_north
end

#head_southObject (readonly)

Returns the value of attribute head_south.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def head_south
  @head_south
end

#head_westObject (readonly)

Returns the value of attribute head_west.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def head_west
  @head_west
end

#livesObject (readonly)

Returns the value of attribute lives.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def lives
  @lives
end

#markObject

Returns the value of attribute mark.



9
10
11
# File 'lib/games_paradise/tic_tac_toe/tic_tac_toe_with_AI/player.rb', line 9

def mark
  @mark
end

#time_hitObject (readonly)

Returns the value of attribute time_hit.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def time_hit
  @time_hit
end

#winObject (readonly)

Returns the value of attribute win.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def win
  @win
end

#xObject (readonly)

Returns the value of attribute x.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



9
10
11
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 9

def y
  @y
end

Instance Method Details

#best_move(board, engine) ⇒ Object

#

best_move

This simulatest the best move that the robot can take. The robot is represented by the ‘O’, so we can treat the robot as a Player too.

This is the “AI” part.

#


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/games_paradise/tic_tac_toe/tic_tac_toe_with_AI/player.rb', line 40

def best_move(board, engine)
  e
  e "Robot (O player) is taking turn..."
  sleep 0.30 # Simulate "thinking" ...
  position = check_priority(board)
  board.positions_with_values["#{position}"] = 'O'
  winner = engine.check_winner(board)
  if winner != 'No One'
    e
    board.display
    engine.display_winner(self.mark)
  end
  e
  board.display
end

#bomb(enemytanks) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 41

def bomb(enemytanks)    
  enemytanks.each do |tank|
    if Gosu.distance(@cannon.x + 7.5, @cannon.y + 7.5, tank.x + 28, tank.y + 28) < 28
      @enemyteam.time_hit = Time.now # memorising the struck time
      @enemyteam.exploded = true # change the explosion status
      @enemyteam.loc_x = tank.x # memorising the struck location
      @enemyteam.loc_y = tank.y
      tank.alive = false
      @cannon.neutralised = true
      tank.cannon.neutralised = true
      @bombed_tank += 1
      @hit_tank.play
    end
  end
end

#bomb_by(enemytanks) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 57

def bomb_by(enemytanks)
  enemytanks.each do |tank|
    if Gosu.distance(tank.cannon.x + 7.5, tank.cannon.y + 7.5, @x + 28, @y + 28) < 28
      @cannon.neutralised = true
      tank.cannon.neutralised = true
      @hit_tank.play
      @exploded = true
      @time_hit = Time.now
      @loc_x = @x
      @loc_y = @y                
    end
  end
end

#bomb_fortObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 98

def bomb_fort
  if Gosu.distance(@cannon.x + 7.5, @cannon.y + 7.5, @fort.x + 20, @fort.y + 20) < 30
    bomb_fort_events
  end
  @wall_units.each do |unit|
    @enemytanks.each do |tank|
      if Gosu.distance(tank.cannon.x + 7.5, tank.cannon.y + 7.5, @fort.x + 20, @fort.y + 20) < 30
        bomb_fort_events
      end
    end
  end
end

#bomb_wallObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 85

def bomb_wall
  @wall_units.each do |unit|       
    if Gosu.distance(@cannon.x + 7.5, @cannon.y + 7.5, unit[1] + 20, unit[2] + 20) < 23
      events_after(@cannon, unit)
    end
    @enemytanks.each do |tank|
      if Gosu.distance(tank.cannon.x + 7.5, tank.cannon.y + 7.5, unit[1] + 20, unit[2] + 20) < 23
        events_after(tank.cannon, unit)
      end
    end
  end    
end

#downObject



64
65
66
67
68
69
70
71
72
# File 'lib/games_paradise/gui/gosu/final_fantasy/player.rb', line 64

def down
	@player_colliders.bottom_collision_check(@x, @y, @speed)
	@y += @speed unless @player_colliders.bottom_collision
	if @right_flag
		@image = @right_walk_anim[Gosu.milliseconds / 200 % @right_walk_anim.size]
	else
		@image = @left_walk_anim[Gosu.milliseconds / 200 % @left_walk_anim.size]
	end
end

#drawObject



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
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 141

def draw    
  if @exploded
    img = @explosion[Gosu::milliseconds / 100 % @explosion.size]      
    img.draw(@loc_x - 8, @loc_y - 8, 2)
    if @lives > 0 && Time.now - @time_hit > 0.5
      @exploded = false
      @lives -= 1
      @x = 260
      @y = 644
    end      
  elsif @lives > 0 && @exploded == false
    case true
    when @head_north; @tank_north.draw(@x, @y, 2)
    when @head_west; @tank_west.draw(@x, @y, 2)
    when @head_east; @tank_east.draw(@x, @y, 2)
    when @head_south; @tank_south.draw(@x, @y, 2)
    end
  end
  @text_display.draw_text("LIVES: #{@lives}", 10, 670, 3, 0.8, 0.8, Gosu::Color::GREEN)
  @text_display.draw_text("ENEMY: #{@bombed_tank}", 10, 5, 3, 0.8, 0.8, Gosu::Color::GREEN)
  @cannon.draw
  @enemyteam.draw
  @wall.draw
  @fort.draw
end

#idleObject



36
37
38
# File 'lib/games_paradise/gui/gosu/final_fantasy/player.rb', line 36

def idle
	@image = @idle_anim
end

#leftObject



47
48
49
50
51
52
# File 'lib/games_paradise/gui/gosu/final_fantasy/player.rb', line 47

def left
	@right_flag = false
	@player_colliders.left_collision_check(@x, @y, @speed)
	@x-= @speed unless @player_colliders.left_collision
	@image = @left_walk_anim[Gosu.milliseconds / 200 % @left_walk_anim.size]
end

#move(board, position, engine) ⇒ Object

#

move

#


23
24
25
26
27
28
29
30
# File 'lib/games_paradise/tic_tac_toe/tic_tac_toe_with_AI/player.rb', line 23

def move(board, position, engine) # X player move
  board.positions_with_values[position] = self.mark
  board.display
  winner = engine.check_winner(board)
  if winner != 'No One'
    engine.display_winner(self.mark)
  end
end

#rightObject



40
41
42
43
44
45
# File 'lib/games_paradise/gui/gosu/final_fantasy/player.rb', line 40

def right
	@right_flag = true
	@player_colliders.right_collision_check(@x, @y, @speed)
	@x += @speed unless @player_colliders.right_collision
	@image = @right_walk_anim[Gosu.milliseconds / 200 % @right_walk_anim.size]
end

#sense_brickObject



78
79
80
81
82
83
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 78

def sense_brick
  unless @bricks.empty?
    nearest_brick = nearest_obj(@bricks)
  end
  sense_collide(nearest_brick)
end

#sense_enemyObject



71
72
73
74
75
76
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 71

def sense_enemy
  unless @enemytanks.empty?
    nearest_tank = nearest_obj(@enemytanks)
  end
  sense_collide(nearest_tank)
end

#upObject



54
55
56
57
58
59
60
61
62
# File 'lib/games_paradise/gui/gosu/final_fantasy/player.rb', line 54

def up
	@player_colliders.top_collision_check(@x, @y, @speed)
	@y -= @speed unless @player_colliders.top_collision
	if @right_flag
		@image = @right_walk_anim[Gosu.milliseconds / 200 % @right_walk_anim.size]
	else
		@image = @left_walk_anim[Gosu.milliseconds / 200 % @left_walk_anim.size]
	end
end

#updateObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 121

def update
  case true # tank unable to move diagonally.
  when @window.button_down?(Gosu::KB_LEFT); move_west
  when @window.button_down?(Gosu::KB_RIGHT); move_east
  when @window.button_down?(Gosu::KB_UP); move_north
  when @window.button_down?(Gosu::KB_DOWN); move_south
  when @window.button_down?(Gosu::KB_SPACE); @cannon.fire
  end
  @cannon.update
  @enemyteam.update
  bomb(@enemytanks)
  bomb_by(@enemytanks)
  sense_enemy
  sense_brick
  @wall.update
  bomb_wall
  bomb_fort
  win_lose_state
end

#warp(x, y) ⇒ Object



31
32
33
34
# File 'lib/games_paradise/gui/gosu/final_fantasy/player.rb', line 31

def warp(x,y)
	@x = x + @width/2
	@y = y + @height/2
end

#win_lose_stateObject



111
112
113
114
115
116
117
118
119
# File 'lib/games_paradise/gui/gosu/battle_city/player.rb', line 111

def win_lose_state
  if @enemytanks.length == 0 && @bombed_tank == 50
    @win = true
    @window.game_running = false
  end
  if @lives == 0 
    @window.game_running = false
  end 
end