본문 바로가기
파이썬

[두근두근파이썬] 8장 프로젝트 3. 애스터로이드 게임

by 아스키의 공부방 2020. 11. 21.
728x90
반응형
import turtle
import random
import math

player_speed = 2
score_num = 0
player = turtle.Turtle()
player.color("blue")
player.shape("turtle")
player.up()
player.speed(0)
screen = player.getscreen()

ai1_hide = False
ai2_hide = False

ai1 = turtle.Turtle()
ai1.color("blue")
ai1.shape("circle")
ai1.up()
ai1.speed(0)
ai1.goto(random.randint(-300, 300), random.randint(-300, 300))

ai2 = turtle.Turtle()
ai2.color("red")
ai2.shape("circle")
ai2.up()
ai2.speed(0)
ai2.goto(random.randint(-300, 300), random.randint(-300, 300))

score = turtle.Turtle()
score.speed(0)
score.up()
score.hideturtle()
score.goto(-300, 300)
score.write("스코어 : ")

area = turtle.Turtle()
area.speed(0)
area.hideturtle()
area.up()
area.goto(-300, 300)
area.down()
area.goto(300, 300)
area.goto(300, -300)
area.goto(-300, -300)
area.goto(-300, 300)

speed_display = turtle.Turtle()
speed_display.speed(0)
speed_display.up()
speed_display.hideturtle()
speed_display.goto(300, 300)
speed_display.write("현재 속도 :" + str(player_speed))

def left():
    player.lt(30) # left

def right():
    player.rt(30) # right

def up():
    global player_speed
    player_speed += 1
    speed_display.clear()
    speed_display.goto(300, 300)
    speed_display.write("현재 속도 :" + str(player_speed))

def down():
    global player_speed
    player_speed -= 1
    speed_display.clear()
    speed_display.goto(300, 300)
    speed_display.write("현재 속도 :" + str(player_speed))

screen.onkeypress(left, "Left")
screen.onkeypress(right, "Right")
screen.onkeypress(up, "Up")
screen.onkeypress(down, "Down")
screen.listen()
# 키 입력

while True:
    player.fd(player_speed) # forward
    if ai1_hide == False:
        ai1.fd(random.randint(3, 10))
        ai1.lt(random.randint(0, 359)) # 회전 방향 변경
    if ai2_hide == False:
        ai2.fd(random.randint(3, 10))
        ai2.lt(random.randint(0, 359))

    if ai1.xcor() >= 300 or ai1.ycor() >= 300: # 특정 영역 벗어나면 위치 변경
        ai1.goto(-300, random.randint(-300, 300))
    elif ai1.xcor() <= -300 or ai1.ycor() <= -300:
        ai1.goto(-300, random.randint(-300, 300))

    if ai2.xcor() >= 300 or ai2.ycor() >= 300:
        ai2.goto(-300, random.randint(-300, 300))
    elif ai2.xcor() <= -300 or ai2.ycor() <= -300:
        ai2.goto(-300, random.randint(-300, 300))

    if player.xcor() >= 300:
        player.goto(player.xcor() - 600, player.ycor())
    elif player.xcor() <= -300:
        player.goto(player.xcor() + 600, player.ycor())
    if player.ycor() >= 300:
        player.goto(player.xcor(), player.ycor() - 600)
    elif player.ycor() <= -300:
        player.goto(player.xcor(), player.ycor() + 600)
    # 플레이어 캐릭터 범위 벗어나면 반대쪽으로 이동

    distance1 = math.sqrt(math.pow(player.xcor() - ai1.xcor() , 2) + math.pow(player.ycor() - ai1.ycor(), 2))
    distance2 = math.sqrt(math.pow(player.xcor() - ai2.xcor() , 2) + math.pow(player.ycor() - ai2.ycor(), 2))
    # 충돌 처리 _ 좌표 값 비교

    print("ai1과의 거리 :", distance1)
    print("ai2과의 거리 :", distance2)

    if distance1 <= 20: # 플레이어 캐릭터와 점이 가까워지면 점을 삭제
        ai1.hideturtle()
        score.clear()
        if ai1_hide == False:
            score_num += 1
            ai1_hide = True
            ai1.goto(0, 0)
        score.write("스코어 : " + str(score_num))
    if distance2 <= 20:
        ai2.hideturtle()
        score.clear()
        if ai2_hide == False:
            score_num += 1
            ai2_hide = True
            ai2.goto(0, 0)
        score_num += 1
        score.write("스코어 : " + str(score_num))

    if ai1.isvisible() != True and ai2.isvisible() != True:
        break

 

 

 

728x90
반응형