Skip to content

Instantly share code, notes, and snippets.

@oeloeloel
Last active January 28, 2023 00:10
Show Gist options
  • Save oeloeloel/0eea06a6f3d54b23a400dc4daa90e845 to your computer and use it in GitHub Desktop.
Save oeloeloel/0eea06a6f3d54b23a400dc4daa90e845 to your computer and use it in GitHub Desktop.
1nvader
def tick(args)
args.state.game_state ||= 'playing'
args.state.player ||= {
x: 100, y: 40, w: 80, h: 80, angle: 90,
path: 'sprites/circle/blue.png'
}
args.state.player_bullet ||= {
x: 130, y: 800, w: 20, h: 20,
path: 'sprites/triangle/equilateral/blue.png'
}
args.state.enemy ||= {
x: 100, y: 600, w: 80, h: 80, angle: -90, next_step: 10,
path: 'sprites/square/orange.png'
}
args.state.enemy_bullet ||= {
x: 130, y: -999, w: 20, h: 20, angle: 180,
path: 'sprites/triangle/equilateral/orange.png'
}
args.state.veil ||= {
x: 0, y: 0, w: 1280, h: 720, path: :pixel, r: 255, g: 0, b: 0, a: 0
}
args.state.jumbotron ||= {
x: 640, y: 360, text: '', size_enum: 100, alignment_enum: 1,
vertical_alignment_enum: 1, r: 255, g: 255, b: 255
}
args.state.instructions ||= {
x: 640, y: 35, text: 'use arrow keys to move. press space to fire',
alignment_enum: 1, size_enum: 3
}
args.outputs.primitives << [
args.state.player_bullet, args.state.enemy_bullet, args.state.player, args.state.enemy,
args.state.veil, args.state.jumbotron, args.state.instructions
]
args.state.game_state = 'win' if args.state.player_bullet.intersect_rect? args.state.enemy
args.state.game_state = 'lose' if args.state.enemy_bullet.intersect_rect? args.state.player
if args.inputs.keyboard.key_down.escape
args.gtk.reset
return
end
unless args.state.game_state == 'playing'
args.state.veil.a = 128
args.state.jumbotron.text = "You #{args.state.game_state}!"
args.state.instructions.text = 'press esc to restart'
return
end
args.state.player.x += args.inputs.left_right * 10
args.state.player.x = args.state.player.x.clamp(0, 1200)
args.state.enemy.x += args.state.enemy.next_step
args.state.enemy.next_step *= -1 if args.state.enemy.x < 0 || args.state.enemy.x > 1200 || rand(100).zero?
args.state.enemy.x = args.state.enemy.x.clamp(0, 1200)
if args.inputs.keyboard.key_down.space
args.state.player_bullet.x = args.state.player.x + 30
args.state.player_bullet.y = args.state.player.y + 40
end
if rand(100).zero? && args.state.enemy_bullet.y <= -30
args.state.enemy_bullet.x = args.state.enemy.x + 30
args.state.enemy_bullet.y = args.state.enemy.y + 30
end
args.state.player_bullet.y += 10 if args.state.player_bullet.y < 800
args.state.enemy_bullet.y -= 10 if args.state.enemy_bullet.y > -30
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment