Skip to content

Instantly share code, notes, and snippets.

@aoitaku
Created October 13, 2014 17:26
Show Gist options
  • Save aoitaku/40edc2d3098822f012ba to your computer and use it in GitHub Desktop.
Save aoitaku/40edc2d3098822f012ba to your computer and use it in GitHub Desktop.
dxruby_platformer_8th
require "dxruby"
Player = Struct.new(:x, :y, :vx, :vy, :image)
player = Player.new(32, 416, 0, 0, Image.load("image/player_image.png"))
enemy_image = Image.load("image/enemy_image.png")
enemy_image2 = Image.load("image/enemy_image2.png")
Enemy = Struct.new(:x, :y, :image)
enemies = [
Enemy.new(576, 416, enemy_image),
Enemy.new(542, 416, enemy_image),
Enemy.new(510, 416, enemy_image2)
]
map = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]
map_images = Image.load_tiles("image/map_image.png", 2, 1)
Window.loop do
player.vx = Input.x
player.vy = Input.y
if player.vx > 0
x = player.x + player.vx
y = player.y
map_pos_x = x / 32 + 1
map_pos_y = y / 32
if map[map_pos_y][map_pos_x] == 1
player.vx -= x - (map_pos_x - 1) * 32
end
elsif player.vx < 0
x = player.x + player.vx
y = player.y
map_pos_x = x / 32
map_pos_y = y / 32
if map[map_pos_y][map_pos_x] == 1
player.vx += (map_pos_x + 1) * 32 - x
end
end
if player.vy > 0
x = player.x
y = player.y + player.vy
map_pos_x = x / 32
map_pos_y = y / 32 + 1
if map[map_pos_y][map_pos_x] == 1
player.vy -= y - (map_pos_y - 1) * 32
end
elsif player.vy < 0
x = player.x
y = player.y + player.vy
map_pos_x = x / 32
map_pos_y = y / 32
if map[map_pos_y][map_pos_x] == 1
player.vy += (map_pos_y + 1) * 32 - y
end
end
player.x += player.vx
player.y += player.vy
Window.draw_tile(0, 0, map, map_images, 0, 0, nil, nil, 0)
Window.draw(player.x, player.y, player.image)
enemies.each do |enemy|
Window.draw(enemy.x, enemy.y, enemy.image)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment