상세 컨텐츠

본문 제목

PING PONG GAME

python

by bumychoi 2024. 6. 14. 16:57

본문

main.py

from turtle import Screen,Turtle
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
import time

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

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


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")


game_is_on = True
while game_is_on:
    time.sleep(ball.move_speed)
    screen.update()
    ball.move()
    # 벽튕기기
    if ball.ycor()>290 or ball.ycor()<-290:
        ball.bounce_y()

    # 팬들 감지
    if ball.distance(r_paddle) < 60 and ball.xcor() > 320 or ball.distance(l_paddle)<60 and ball.xcor()<-320:
        ball.bounce_x()
    # 공아웃 오른쪽
    if ball.xcor() > 380:
        ball.reset_position()
        Scoreboard.l_point()

    # 공아웃 왼쪽
    if ball.xcor() < -380:
        ball.reset_position()
        Scoreboard.r_point()

screen.exitonclick()


ball.py

from turtle import Turtle

class Ball(Turtle):

    def __init__(self):
        super().__init__()
        self.color("green")
        self.shape("circle")
        self.penup()
        self.x_move = 1
        self.y_move = 1
        self.move_speed = 0.005

    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_position(self):
        self.goto(0,0)
        self.move_speed = 0.1
        self.bounce_x()

paddle.py

from turtle import Turtle

class Paddle(Turtle):

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

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

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

 

scoreboard.py

from turtle import Turtle


class Scoreboard(Turtle):

    def __init__(self):
        super().__init__()
        self.color("blue")
        self.penup()
        self.hideturtle()
        self.l_score = 0
        self.r_score = 0
        self.update_scireborard()
   
    def update_scireborard(self):
        self.clear()
        self.goto(-100,200)
        self.write(self.l_score,align="center",font=("Courier",80,"normal"))
        self.goto(100,200)
        self.write(self.r_score,align="center",font=("Courier",80,"normal"))



    def l_point(self):
        self.l_score += 1
        self.update_scireborard()
        self.move_speed *= 1.2

    def r_point(self):
        self.r_score += 1
        self.update_scireborard()

'python' 카테고리의 다른 글

미국 주이름 맞추기 게임  (0) 2024.06.17
pandas 배우기  (0) 2024.06.17
python 집중공부 (유데미 뱀게임 만들기)  (0) 2024.06.14
수정작업 중~~  (1) 2024.01.15
mongo db 추가 ~~~ 읽기 까지 완성  (0) 2024.01.13

관련글 더보기