본문 바로가기
파이썬

[두근두근파이썬] 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()

asteroids = []

for i in range(10):
    a = turtle.Turtle()
    a.color("red")
    a.shape("circle")
    a.up()
    a.speed(0)
    a.goto(random.randint(-300, 300), random.randint(-300, 300))
    asteroids.append(a)

distance = [0]*10

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:
    size = len(asteroids)
    ai_hide = asteroids
    
    for i in range(size):
        asteroids[i].fd(random.randint(3, 10))
        asteroids[i].lt(random.randint(0, 359))
        if asteroids[i].xcor() >= 300 or asteroids[i].ycor() >= 300: # 특정 영역 벗어나면 위치 변경
            asteroids[i].goto(-300, random.randint(-300, 300))
        elif asteroids[i].xcor() <= -300 or asteroids[i].ycor() <= -300:
            asteroids[i].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)
    # 플레이어 캐릭터 범위 벗어나면 반대쪽으로 이동
    player.fd(player_speed)

    for i in range(size):
        distance[i] = math.sqrt(math.pow(player.xcor() - asteroids[i].xcor() , 2) + math.pow(player.ycor() - asteroids[i].ycor(), 2))
        # 충돌 처리 _ 좌표 값 비교
        print("ai", i, "와의 거리 :", distance[i])
        if distance[i] <= 20: # 플레이어 캐릭터와 점이 가까워지면 점을 삭제
            asteroids[i].hideturtle()
            score.clear()
            score_num += 1
            asteroids[i].goto(0, 0)
            score.write("스코어 : " + str(score_num))
            del asteroids[i]
            del distance[i]
            break

 

 

 

728x90
반응형