본문 바로가기
파이썬

파이썬(Turtle_Game)-2024-06-12

by 앵보몬 2024. 6. 13.
728x90
반응형

Turtle_Game

 

요구사항

1) 이번 게임에서는 blue_turtle (악당거북이) , white_turtle (주인공거북이), food(먹이), supplement(보충제)로 설정한다.

2) 변수명 = t.Turtle() 함수를 통해 원하는 만큼 거북이를 늘릴 수 있습니다.

3) turn_으로 시작하는 함수작성하여 사용자가 키보드의 방향키를 눌렀을 때 원하는 방향으로 돌립니다.

4) play() 함수를 작성합니다. 주인공 거북이가 앞으로 이동하며, 악당 거북이가 주인공을 쫒아가고, 주인공 거북이가 악당거북이 먹이에 닿았을 때의 결과값을 모두 play()함수에서 처리한다.

5) playing변수, start() 함수. playing변수를 통해 게임이 실행 중인지, 대기중인지 구분하며 (True / False) 처리한다.

6) start() 함수를 통해 사용자가 space 를 누르면 게임이 시작되도록 합니다.

 

예외 처리

1) 설명 : 파일 입출력 및 잘못된 입력에 대한 예외 처리

2) 사례 : 파일이 없을 경우, 잘못된 수량 입력

3) 핵심 : 안정적인 프로그램 동작 보장

 

import turtle as t
import random
import time

score = 0
playing = False
speed = 0  # 속도 변수 초기화
tspeed = 0

# 파란 거북이 만들기
blue_turtle = t.Turtle()
blue_turtle.shape("turtle")
blue_turtle.color("blue")
blue_turtle.speed(0)
blue_turtle.up()
blue_turtle.goto(0, 200)

# 흰 거북이 만들기(음식)
white_turtle = t.Turtle()
white_turtle.shape("circle")
white_turtle.color("green")
white_turtle.speed(0)
white_turtle.up()
white_turtle.goto(0, -200)

# 흰 거북이 속도 증강(보충제)
supplement = t.Turtle()
supplement.shape("triangle")
supplement.color("red")
supplement.speed(0)
supplement.up()
supplement.goto(0, 50)

# 거북이 이동을 위한 함수 정의
def turn_right():
    t.setheading(0)

def turn_up():
    t.setheading(90)

def turn_left():
    t.setheading(180)

def turn_down():
    t.setheading(270)

# 게임 시작
def start():
    global playing
    if not playing:  # 조건을 간단히 합니다.
        playing = True
        t.clear()
        play()


# 메인 게임 로직
def play():
    global score, playing, speed, tspeed
    if playing:
        t.forward(10)
   
        if random.randint(1, 5) == 3:
            ang = blue_turtle.towards(t.pos())
            blue_turtle.setheading(ang)
            speed = score + 2

        if speed > 20:
            speed = 20
        blue_turtle.forward(speed)

        if t.distance(blue_turtle) < 10:
            message("Game Over", "Score: " + str(score))
            playing = False
            score = 0

        if t.distance(supplement) < 30:
            tspeed = tspeed + 2
            t.write("주인공의 속도 : " + str(tspeed))
       
        if t.distance(supplement) < 30:
            tspeed = tspeed + 2
            t.write("주인공의 속도 : " + str(tspeed))
            star2_x = random.randint(-400, 400)
            star2_y = random.randint(-230, 230)
            supplement.goto(star2_x, star2_y)  
           

        if t.distance(white_turtle) < 15:
            score += 1
            t.clear()
            t.write(score, align="center", font=("Courier", 24, "normal"))
            star_x = random.randint(-230, 230)
            star_y = random.randint(-230, 230)
            white_turtle.goto(star_x, star_y)
       
        if score == 10:
            message("Victory", "Score: " + str(score))
            playing = False
            score = 0    
       
        t.ontimer(play, 100)

# 화면에 메시지 표시
def message(m1, m2):
    t.clear()
    t.goto(0, 100)
    t.write(m1, align="center", font=("Courier", 24, "normal"))
    t.goto(0, -100)
    t.write(m2, align="center", font=("Courier", 18, "normal"))
    t.home()

# 게임 창 설정
t.title("Turtle Run")
t.setup(600, 600)
t.bgcolor("black")
t.shape("turtle")
t.speed(0)
t.up()
t.color("white")

# keypress 이벤트 등록
t.onkeypress(turn_right, "Right")
t.onkeypress(turn_up, "Up")
t.onkeypress(turn_left, "Left")
t.onkeypress(turn_down, "Down")
t.onkeypress(start, "space")
t.listen()

# 초기 메시지로 게임 시작
message("Turtle Run", "[Space] to start")
t.mainloop()

 

728x90
반응형