파이썬/파이썬(python) 중급

[22-2 파이썬] 터틀 핑퐁 게임 만들기(클래스 상속, 터틀 명령어 실습)

Olivia-BlackCherry 2022. 8. 31. 17:55

오늘의  첫 번째 미션이다.

1.기본화면 세팅하기
- Screen클래스: setup(width, height), bgccolor(), title(), tracer(), update(), exitonclick
- Turtle 클래스에서 paddle 객체 생성하기
2. paddle.py 파일 만들기
- 클래스 상속
- shape(), color(), penup(), shape_size(), goto()
-패들이 위 아래로 움직임

main.py

from turtle import Screen
from paddle import Paddle

screen = Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Pingpong")
screen.tracer(0)

r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))

screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")

keep_going = True
while keep_going:
    screen.update()

screen.exitonclick()

paddle.py

from turtle import Turtle

class Paddle(Turtle):
    def __init__(self, position):
        super().__init__()
        self.shape("square")
        self.color("white")
        self.penup()
        self.shapesize(stretch_len =1, stretch_wid=5)
        self.goto(position)

    def go_up(self):
        x = self.xcor()
        new_y = self.ycor() + 10
        self.goto(x, new_y)

    def go_down(self):
        x = self.xcor()
        new_y = self.ycor() - 10
        self.goto(x, new_y)

 

 

 

두 번째 미션이다.

3. ball.py 파일 만들기
4. 공 만들기. 시작은 정 중앙
5. 공이 오른쪽 대각선 방향으로 움직이다가, 위나 아래 벽에 부딪히면 튕겨지게 만들기

main.py

from turtle import Screen
from paddle import Paddle
from ball import Ball
import time

screen = Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Pingpong")
screen.tracer(0)

r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball((0,0))

screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")

keep_going = True
while keep_going:
    screen.update()
    ball.move()
    time.sleep(0.1)
    if ball.ycor() > 280 or ball.ycor() < - 280:
        ball.bounce()

screen.exitonclick()

ball.py

from turtle import Turtle

class Ball(Turtle):
    def __init__(self, position):
        super().__init__()
        self.shape("square")
        self.color("white")
        self.penup()
        self.goto(position)
        self.x_move = 10
        self.y_move = 10


    def move(self):
        new_x = self.xcor() + self.x_move
        new_y = self.ycor() + self.y_move
        self.goto(new_x, new_y)

    def bounce(self):
        self.y_move *= -1

 

 

 

세 번째 미션이다.

6. 공이 패들과 맞았다면 공을 튕겨내기
7. 공이 패들과 맞지 않았다면 원점에서 다시 시작하기

main.py

from turtle import Screen
from paddle import Paddle
from ball import Ball
import time

screen = Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Pingpong")
screen.tracer(0)

r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball((0,0))

screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")

keep_going = True
while keep_going:
    screen.update()
    ball.move()
    time.sleep(0.1)
    if ball.ycor() > 280 or ball.ycor() < - 280:
        ball.bounce_y()
    if ball.distance(r_paddle) <50 and ball.xcor() > 380 or ball.distance(l_paddle) <50 and ball.xcor() > -380:
        ball.bounce_x()
    if ball.xcor() >380:
        ball.reset()
    if ball.xcor() <-380:
        ball.reset()
screen.exitonclick()

ball.py

from turtle import Turtle

class Ball(Turtle):
    def __init__(self, position):
        super().__init__()
        self.shape("square")
        self.color("white")
        self.penup()
        self.goto(position)
        self.x_move = 10
        self.y_move = 10


    def move(self):
        new_x = self.xcor() + self.x_move
        new_y = self.ycor() + self.y_move
        self.goto(new_x, new_y)

    def bounce_y(self):
        self.y_move *= -1

    def bounce_x(self):
        self.x_move *= -1

    def reset(self):
        self.goto(0,0)
        self.bounce_x()

 

 

 

네 번째 미션이다.

8. 점수판 만들기 (0:0)에서부터 시작하기
9. 득점하면 점수 바꾸기
10. 패들에 공 맞히면 공의 속도가 빨라지도록 만들기

main.py

from turtle import Screen
from paddle import Paddle
from ball import Ball
from score import Score
import time

screen = Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("Pingpong")
screen.tracer(0)

r_paddle = Paddle((350, 0))
l_paddle = Paddle((-350, 0))
ball = Ball((0,0))
scoreboard = Score()

screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")


keep_going = True
while keep_going:
    screen.update()
    ball.move()
    time.sleep(ball.ball_speed)
    if ball.ycor() > 280 or ball.ycor() < - 280:
        ball.bounce_y()
    if ball.distance(r_paddle) <50 and ball.xcor() > 340 or ball.distance(l_paddle) <50 and ball.xcor() > -380:
        ball.bounce_x()
    if ball.xcor() >380:
        ball.reset()
        scoreboard.l_getpoint()
    if ball.xcor() <-380:
        ball.reset()
        scoreboard.r_getpoint()
screen.exitonclick()

ball.py

from turtle import Turtle

class Ball(Turtle):
    def __init__(self, position):
        super().__init__()
        self.shape("square")
        self.color("white")
        self.penup()
        self.goto(position)
        self.x_move = 10
        self.y_move = 10
        self.ball_speed = 0.1


    def move(self):
        new_x = self.xcor() + self.x_move
        new_y = self.ycor() + self.y_move
        self.goto(new_x, new_y)

    def bounce_y(self):
        self.y_move *= -1

    def bounce_x(self):
        self.x_move *= -1
        self.ball_speed *= 0.5

    def reset(self):
        self.goto(0,0)
        self.bounce_x()
        self.ball_speed = 0.1

score.py

from turtle import Turtle
class Score(Turtle):
    def __init__(self):
        super().__init__()
        self.color("white")
        self.penup()
        self.hideturtle()
        self.l_score = 0
        self.r_score= 0
        self.update_scoreboard()

    def update_scoreboard(self):
        self.clear()
        self.goto(-100, 260)
        self.write(arg=self.l_score, align="center", font=("Courier", 50, "normal"))
        self.goto(100, 260)
        self.write(arg=self.r_score, align="center", font=("Courier", 50, "normal"))

    def l_getpoint(self):
        self.l_score +=1
        self.update_scoreboard()

    def r_getpoint(self):
        self.r_score +=1
        self.update_scoreboard()