Class: GamesAndRpgParadise::Player

Inherits:
Chingu::GameObject
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb,
lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb,
lib/games_and_rpg_paradise/gui/gosu/final_fantasy/player.rb,
lib/games_and_rpg_paradise/games/tic_tac_toe/tic_tac_toe_with_AI/player.rb

Overview

GamesAndRpgParadise::Player

Instance Attribute Summary collapse

Instance Method Summary collapse

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_and_rpg_paradise/games/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.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def bricks
  @bricks
end

#head_eastObject (readonly)

Returns the value of attribute head_east.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def head_east
  @head_east
end

#head_northObject (readonly)

Returns the value of attribute head_north.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def head_north
  @head_north
end

#head_southObject (readonly)

Returns the value of attribute head_south.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def head_south
  @head_south
end

#head_westObject (readonly)

Returns the value of attribute head_west.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def head_west
  @head_west
end

#healthObject (readonly)

Returns the value of attribute health.



14
15
16
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 14

def health
  @health
end

#livesObject (readonly)

Returns the value of attribute lives.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def lives
  @lives
end

#markObject

Returns the value of attribute mark.



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

def mark
  @mark
end

#scoreObject (readonly)

Returns the value of attribute score.



14
15
16
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 14

def score
  @score
end

#time_hitObject (readonly)

Returns the value of attribute time_hit.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def time_hit
  @time_hit
end

#winObject (readonly)

Returns the value of attribute win.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def win
  @win
end

#xObject (readonly)

Returns the value of attribute x.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



10
11
12
# File 'lib/games_and_rpg_paradise/gui/gosu/battle_city/player.rb', line 10

def y
  @y
end

Instance Method Details

#accelerateObject

pressing up arrow causes player to accelerate



56
57
58
59
60
61
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 56

def accelerate  # pressing up arrow causes player to accelerate
   if self.velocity_x <= @max_speed && self.velocity_y <= @max_speed
 		self.velocity_x += Gosu::offset_x(self.angle, @speed)
  	self.velocity_y += Gosu::offset_y(self.angle, @speed)
   end
end

#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_and_rpg_paradise/games/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

blink on and off every 7 ticks



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 44

def blink    # blink on and off every 7 ticks
  if @blink == 14
    @image = @picture2
    @blink = 0
  elsif @blink == 7
    @image = @picture1
    @blink +=1
  else
    @blink +=1
  end
end

#bomb(enemytanks) ⇒ Object



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

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



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

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



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

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



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

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

#brakeObject

pressing down arrow invokes brakes



63
64
65
66
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 63

def brake       # pressing down arrow invokes brakes
	self.velocity_x *= 0.88
	self.velocity_y *= 0.88
end

#cool_downObject

player cannot be damaged when blinking



32
33
34
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 32

def cool_down   # player cannot be damaged when blinking
  @cooling_down = $cooling_down
end

#damageObject



36
37
38
39
40
41
42
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 36

def damage
  if @cooling_down == 0  # only causes damage if player is not blinking
    @cooling_down = $cooling_down
    $health -= 1
    Sound["media/audio/exploded.ogg"].play(0.3)
  end
end

#downObject



64
65
66
67
68
69
70
71
72
# File 'lib/games_and_rpg_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



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

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

#fireObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 76

def fire
@shoot.play(rand(0.05..0.1))  # randomize laser sound effect volume
  if $weapon == 1   # number of Bullets fired depends on weapons upgrade status
		Bullet.create(:x => @x, :y => @y, :angle => @angle, :zorder => Zorder::Projectile)

  elsif $weapon == 2   # use Gosu::offset for upgraded weapons (thanks to PeterT)
    Bullet.create(:x => @x + Gosu::offset_x(@angle+90, 8), :y => @y + Gosu::offset_y(@angle+90, 8), :angle => @angle)
    Bullet.create(:x => @x + Gosu::offset_x(@angle+90, -8), :y => @y + Gosu::offset_y(@angle+90, -8), :angle => @angle)

  elsif $weapon >= 3   # fires three Bullets when weapons are fully upgraded
    Bullet.create(:x => @x, :y => @y, :angle => @angle, :zorder => Zorder::Projectile)
    Bullet.create(:x => @x + Gosu::offset_x(@angle+90, 14), :y => @y + Gosu::offset_y(@angle+90, 14), :angle => @angle)
    Bullet.create(:x => @x + Gosu::offset_x(@angle+90, -14), :y => @y + Gosu::offset_y(@angle+90, -14), :angle => @angle)
#      Bullet.create(:x => @x + 20 * Math.cos(@angle*Math::PI/180) , :y => @y + 20 * Math.sin(@angle*Math::PI/180), :angle => @angle)
#      alternate way to do the weapons offsets with sin and cos
  end
end

#idleObject



36
37
38
# File 'lib/games_and_rpg_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_and_rpg_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_and_rpg_paradise/games/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_and_rpg_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



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

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

#sense_enemyObject



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

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

#speedifyObject

speedify



95
96
97
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 95

def speedify  # method called in Win gamestate, in ending.rb
  @max_speed = 50
end

#turn_leftObject



68
69
70
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 68

def turn_left
	self.angle -= @rotate_speed
end

#turn_rightObject



72
73
74
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 72

def turn_right
	self.angle += @rotate_speed
end

#upObject



54
55
56
57
58
59
60
61
62
# File 'lib/games_and_rpg_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

update



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/games_and_rpg_paradise/gui/gosu/chinguroids/objects.rb', line 100

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_and_rpg_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



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

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