Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- GitHub
- IntelliJ
- Android
- rubymine
- CPP
- RaspberryPi
- error
- boj
- OTLanguage
- kotlin
- ruby
- Vane
- ruby2d
- C
- maven
- Python
- gnuplot
- 루비
- gradle
- react
- JS
- OAuth
- Java
- jetbrains
- 개발노트
- plugin
- Godot
- Spring
- Baekjoon
- Shell
Archives
- Today
- Total
PersesTitan(페르) 기술블로그
[Ruby] 루비로 게임 제작해보기 6-1: 물체 통과 막기 본문
[Ruby] 루비로 게임 제작해보기 6: 물체 통과 막기
(현재 입력된 방향키를 확인하기 쉽게 좌측 상단에 입력된 방향키가 보이도록 임시로 넣었습니다.)
아래와 같이 벽에 완전히 붙어있을때 움직일 수 없는데 벽에 붙어 있을때도 닿아 있는 것으로 인식되므로 움직일 수 없기 때문에 해당 부분을 해결해볼려고 합니다.
코드
이전에 사용했던 Direction모듈은 같이 사용했지만 너무 길어서 저는 따로 다른 파일로 빼서 사용하기로 했습니다.
direction.rb
# 방향을 정하는 모듈
module Direction
LEFT = "left"
RIGHT = "right"
TOP = "up"
UNDER = "down"
def Direction.move(e, o, speed = 1)
case e.key
when LEFT ; o.x -= speed
when RIGHT ; o.x += speed
when TOP ; o.y -= speed
when UNDER ; o.y += speed
else return end
end
# @o1 : 기준이 될 객체
# @o2 : 위치가 변경될 객체
# @option : 위치 (왼쪽, 오른쪽, 위쪽, 아래쪽) 기준
# @distance_x : 추가 x 값
# @distance_y : 추가 y 값
def Direction.rel_move(o1, o2, option, distance_x = 0, distance_y = 0)
case option
when LEFT
o2.x, o2.y = o1.x - o2.width + distance_x, o1.y + distance_y
when RIGHT
o2.x, o2.y = o1.x + o1.width + distance_x, o1.y + distance_y
when TOP
o2.x, o2.y = o1.x + distance_x, o1.y - o2.height + distance_y
when UNDER
o2.x, o2.y = o1.x + distance_x, o1.y + o1.height + distance_y
else return end
end
# (p_x1, p_y1) ┌─────┐ (p_x2, p_y1)
# │ │
# (p_x1, p_y2) └─────┘ (p_x2, p_y2)
#
# y
# │
# │
# └───── x
def Direction.check(o1, o2)
p_x1, p_y1 = o1.x, o1.y
p_x2, p_y2 = p_x1 + o1.width, p_y1 + o1.height
p_x1 += 1
p_y1 += 1
p_x2 -= 1
p_y2 -= 1
return o2.contains?(p_x1, p_y1) || o2.contains?(p_x2, p_y2) || o2.contains?(p_x1, p_y2) || o2.contains?(p_x2, p_y1)
end
end
start.rb
require 'ruby2d'
require_relative 'direction'
check_in = lambda do |o1, os, x, y|
os.each do |o|
if Direction.check o1, o
o1.x, o1.y = x, y
end
end
end
set title: "Game"
w = get :width
h = get :height
# 장애물
block = []
text = Rectangle.new(x: w/2, y: h/2, width: 20, height: 20, color: "red")
text.x -= text.width
text.y -= text.height
rect = Rectangle.new(x: 0, y: 0, width: 50, height: 50)
Direction::rel_move(text, rect, Direction::UNDER)
block << rect
Window.on :key_held do |e|
x, y = text.x, text.y
Direction::move(e, text, speed=3)
if text.x < 0 || text.x + text.width > w; text.x = x end
if text.y < 0 || text.y + text.height > h; text.y = y end
check_in.call(text, block, x, y)
end
show
결과
코드 풀이
direction.rb
p_x1 += 1, p_y1 += 1 ...
나머지 기능은 이전 코드와 비슷하므로 생략하고 각 좌표마다 특정 값을 조절하여 벽에 붙어도 움직일 수 있도록 하였습니다. 해당 값은 원하는 값 많큼 더하고 빼주시면 됩니다.
start.rb
require_relative 'direction'
따로 파일로 분리시킨 direction.rb를 분리시킨 파일을 불러오는 로직 입니다.
'Language > Ruby' 카테고리의 다른 글
[Ruby] 루비에서 그래프 구현하기 (gnuplot 설치) (0) | 2023.04.12 |
---|---|
[Ruby] 루비로 게임 제작해보기 7: 도착 로직 구현 (0) | 2023.01.06 |
[Ruby] 루비로 게임 제작해보기 6: 물체 통과 막기 (0) | 2022.12.28 |
[Ruby] 루비로 게임 제작해보기 5: 상대 위치로 이동 (0) | 2022.12.27 |
[Ruby] 루비로 게임 제작해보기 4: 키보드로 이동 구현 (0) | 2022.12.27 |