본문 바로가기

엑셀 & VBA

2-6. Snake 나머지 이동하는 코드 만들기

 나머지 왼쫀, 위, 아래 이동 하는 코드는 기존의 오른쪽으로 이동하는 코드에서 조금만 바꾸면 나머지 이동하는 코드를 완성할 수 있습니다.

 


Sub left()

'변수 불러오기
hx = Cells(4, 27)
hy = Cells(5, 27)
score = Cells(2, 26)

Cells(hx, hy) = "L"

If Cells(hx, hy - 1).Interior.Color = RGB(255, 0, 0) Then '사과 먹음
    ' 오른쪽을 뱀 색으로 바꾸기
    Cells(hx, hy - 1).Interior.Color = RGB(84, 130, 53)
    ' 점수 1점 올리기
    score = score + 1
    ' 점수 기록
    Cells(2, 26) = score
    ' apple 함수 불러오기
    apple
    
ElseIf Cells(hx, hy - 1).Interior.Color = RGB(146, 208, 80) Then ' 오른쪽이 게임판 색일때
    Cells(hx, hy - 1).Interior.Color = RGB(84, 130, 53)
    ' 꼬리 이동하는 함수 불러오기
    tail
Else
    gameover
End If

Cells(5, 27) = hy - 1

End Sub

Sub up()

'변수 불러오기
hx = Cells(4, 27)
hy = Cells(5, 27)
score = Cells(2, 26)

Cells(hx, hy) = "U"

If Cells(hx - 1, hy).Interior.Color = RGB(255, 0, 0) Then '사과 먹음
    ' 오른쪽을 뱀 색으로 바꾸기
    Cells(hx - 1, hy).Interior.Color = RGB(84, 130, 53)
    ' 점수 1점 올리기
    score = score + 1
    ' 점수 기록
    Cells(2, 26) = score
    ' apple 함수 불러오기
    apple
    
ElseIf Cells(hx - 1, hy).Interior.Color = RGB(146, 208, 80) Then ' 오른쪽이 게임판 색일때
    Cells(hx - 1, hy).Interior.Color = RGB(84, 130, 53)
    ' 꼬리 이동하는 함수 불러오기
    tail
Else
    gameover
End If

Cells(4, 27) = hx - 1

End Sub

Sub down()

'변수 불러오기
hx = Cells(4, 27)
hy = Cells(5, 27)
score = Cells(2, 26)

Cells(hx, hy) = "D"

If Cells(hx + 1, hy).Interior.Color = RGB(255, 0, 0) Then '사과 먹음
    ' 오른쪽을 뱀 색으로 바꾸기
    Cells(hx + 1, hy).Interior.Color = RGB(84, 130, 53)
    ' 점수 1점 올리기
    score = score + 1
    ' 점수 기록
    Cells(2, 26) = score
    ' apple 함수 불러오기
    apple
    
ElseIf Cells(hx + 1, hy).Interior.Color = RGB(146, 208, 80) Then ' 오른쪽이 게임판 색일때
    Cells(hx + 1, hy).Interior.Color = RGB(84, 130, 53)
    ' 꼬리 이동하는 함수 불러오기
    tail
Else
    gameover
End If

Cells(4, 27) = hx + 1

End Sub