Hello world! I write a simple platformer where mouse eat cheeses from platforms. My code for mouse
1. Player class:
#class Player
class Player
def initialize window, x, y, left, right
@x, @y = x, y
@left = Gosu::Image.new(window, left, false)
@right = Gosu::Image.new(window, right, false)
@face_left = true
end
attr_reader :x, :y
#move left
def move_left
@face_left = true
@x -= 5.0
@x = 1.0 if @x <= 1.0
end
#move right
def move_right
@face_left = false
@x += 5.0 if @x <= 595.0
end
#up
def up
@y -= 7.0 if @y >= 17.0
end
#move down
def down
@y += 2.5 if @y <= 372.5
end
#move
def move
@x %= 640
@y %= 480
end
#draw
def draw
if @face_left
@left.draw(@x, @y, 1)
else
@right.draw(@x, @y, 1)
end
end
end
2.Mouse class:
#class Mouse - main player
class Mouse < Player
def initialize window, x, y
super window, x, y, "images/mouse.png", "images/mouse-r.png"
@score, @lives = 0, 3
@heart = Gosu::Image.new(window, "images/heart.png", false)
@ui = Gosu::Font.new(window, 'Monaco', 25)
puts "Create mouse..."
@on_ground = false
end
attr_reader :lives, :score
attr_accessor :on_ground
#draw mouse
def draw
super
@ui.draw("Score: #{@score}", 10, 450, 2)
@heart_x = 0
@lives.times do
@heart.draw(600-@heart_x, 450, 2)
@heart_x += 24
end
end
#move down
def down
@y += 2.5 if @y <= 372.5
end
#dead
def dead
@lives -= 1
@lives = 0 if @lives <= 0
end
def dead?
return @lives == 0
end
#add score
def add_score
@score += 100
end
end
3.Level class:
#class Level
class Level
def initialize window
@window, @num = window, 0
@cheeses = []
@bricks = []
@ui = Gosu::Font.new(@window, 'Monaco', 25)
puts "Initialize levels..."
end
attr_reader :ui, :num
attr_accessor :bricks, :cheeses
#starting level
def start
@num += 1
(2+@num).times do
@x_cord = rand(10..550)
@y_cord = rand(50..325)
@cheeses << Cheese.new(@window, @x_cord, @y_cord)
@bricks << Brick.new(@window, @x_cord - 10, @y_cord + 36)
end
puts "Starting level #{@num}."
puts "Go!"
end
#draw
def draw
@cheeses.each do |e| e.draw end
@bricks.each do |e| e.draw end
@ui.draw("Level:#{@num}", 10, 425, 2)
end
#max scores in level
def total_scores
(2*@num + 0.5 * @num * (@num+1))*100
end
#end level
def end_level
@cheeses = []
@bricks = []
end
end
4.Main window:
#main window
class MyWindow < Gosu::Window
def initialize
puts "Start environment..."
super 640, 480, false
self.caption = 'Mouse Game'
@bg = Gosu::Image.new(self, 'images/bg.png', true);
@music = Gosu::Song.new(self, 'sounds/music.ogg')
puts "Create game music..."
@music.play(looping = true)
@mouse = Mouse.new(self, 520, 375)
@level = Level.new(self)
@level.start
end
#game logic
def update
if @mouse.score == @level.total_scores
@level.end_level
@level.start
end
@mouse.move_left if button_down? Gosu::KbLeft
@mouse.move_right if button_down? Gosu::KbRight
@mouse.up if button_down? Gosu::KbUp or button_down? Gosu::KbSpace
@mouse.down if @mouse.on_ground == false
@mouse.move
@level.cheeses.each do |e|
if (e.x - @mouse.x).abs <= 15.0 && (e.y - @mouse.y).abs <= 15.0
e.drawing = false
e.x = e.y = -1
@mouse.add_score
end
end
end
def draw
@level.draw
@mouse.draw
@bg.draw(0, 0, 0)
end
def button_down(key)
case key
when Gosu::KbEscape
@music.stop
close
puts "Exit."
end
end
end
How can I create logic:
Stop falling down mouse if he standing in platform and fall down if he jump from platform?
I try when mouse intersects by x and y with platform but he fall down always
Thanks for all, and sorry for my English! (I'm novice in game development)
Hey, looks really good. If you can upload your project to github, I will take a closer look at it and let you know my thoughts (for what they're worth)...
By arrow
Date 2013-11-05 11:35
Edited 2013-11-05 13:01
A basic way to make a player standing on a platform is to do a box against point hit test. You have a point which is the coordinate of the players feet, and you have a box which could be your bricks.
If the point is within the box then the player is moved on top of the box! I hope this is enough to get you started.
class Brick
## Box attributes
attr_reader :x, :y, :width, :height
end
class Player
def feet_on? box
## Find the coordinate of the playes feet here
feet_x = @x + 0 #replace zero so the feet_x is at the players feet
feet_y = @y + 0 #replace zero so the feet_y is at the players feet
## Check if the point of the feet is within the boundary box of the brick.
## Box should be drawn from top left corner.
feet_x >= box.x &&
feet_x <= box.x + box.width &&
feet_y > box.y &&
feet_y < box.y + box.height
end
def feet_y= val
dy = 0 #replace delta y so @y + dy is at the players feet
@y = val - dy
end
end
## I would recommend you move your game logic for your level into the level
class Level
def initialize window, player=nil
@window = window
## player from argument or create new player
@player = player || Player.new(window, 100, 100)
@bricks = []
end
## Game Logic
def update
## Update your game physics here, move everything before hit testing
## Hit test all of the bricks against the player:
@bricks.each do |brick|
move_player_on_top_of brick if @player.feet_is_on? brick
end
end
def move_player_on_top_of box
@player.feet_y = box.y
end
end
class MyWindow < Gosu::Window
def initialize
@level = Level.new self
end
def update
@level.update
end
def draw
@level.draw
end
end
Thank you very much. It's really easy
Loading...