Test/Python(20220101~)
Days22. Pong 게임
kiostory
2022. 3. 2. 19:52
* Breakdown the program
- Create the screen
- Create and move a paddle
- Create another paddle
- Create the ball and make it move
- Detect collision with wall and bounce
- Detect collision with paddle
- Detect when paddle misses
- Keep score
* main.py
from turtle import Screen
from paddle import Paddle
from ball import Ball
from scoreboard import Scoreboard
import time
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("kio's PONG Game - ver.202203 (Key : LeftUser - w,s | RightUser - Up,Down | Quit - q )")
screen.tracer(0)
r_paddle = Paddle((380, 0))
l_paddle = Paddle((-380, 0))
score = Scoreboard()
ball = Ball()
def paddle_reset():
r_paddle.goto(380, 0)
l_paddle.goto(-380, 0)
screen.listen()
screen.onkeypress(r_paddle.up, "Up")
screen.onkeypress(r_paddle.down, "Down")
screen.onkeypress(l_paddle.up, "w")
screen.onkeypress(l_paddle.down, "s")
screen.onkey(screen.bye, "q")
game_is_on = True
while game_is_on:
time.sleep(ball.move_speed)
screen.update()
# Detect collision with up/down wall
if ball.ycor() >= 290 or ball.ycor() <= -290:
ball.bounce_y()
# Detect connision with r/l paddles
if (ball.distance(r_paddle) < 50 and ball.xcor() > 350) or (ball.distance(l_paddle) < 50 and ball.xcor() < -350):
ball.bounce_x()
if ball.xcor() >= 390:
ball.reset_position()
paddle_reset()
score.clear()
score.lpoint()
if ball.xcor() < -390:
ball.reset_position()
paddle_reset()
score.clear()
score.rpoint()
ball.move()
screen.exitonclick()
* ball.py
from turtle import Turtle
import time
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.color("yellow")
self.resizemode("user")
self.pu()
self.setpos(0, 0)
self.x_move = 10
self.y_move = 10
self.move_speed = 0.1
def bounce_y(self):
self.y_move *= -1
def bounce_x(self):
self.x_move *= -1
self.move_speed *= 0.95
def move(self):
new_x = self.xcor() + self.x_move
new_y = self.ycor() + self.y_move
self.goto(new_x, new_y)
def reset_position(self):
self.goto(0, 0)
self.move_speed = 0.1
time.sleep(1)
self.bounce_x()
* paddle.py
from turtle import Turtle
MOVING_DISTANCE = 40
UP = 90
DOWN = 270
class Paddle(Turtle):
def __init__(self, position):
super().__init__()
self.shape("square")
self.color("white")
self.resizemode("user")
self.shapesize(stretch_wid=5, stretch_len=1)
self.pu()
self.setpos(position)
def up(self):
new_y = self.ycor() + MOVING_DISTANCE
self.goto(self.xcor(), new_y)
def down(self):
new_y = self.ycor() - MOVING_DISTANCE
self.goto(self.xcor(), new_y)
* scoreboard.py
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial", 14, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.goto(0, 265)
self.color("white")
self.pu()
self.hideturtle()
self.left_user_score = 0
self.right_user_score = 0
self.score()
# def game_over(self):
# self.goto(0, 0)
# self.write("GAME OVER", align=ALIGNMENT, font=FONT)
def score(self):
self.write(f"{self.left_user_score} : {self.right_user_score}", align=ALIGNMENT, font=FONT)
def lpoint(self):
self.left_user_score += 1
self.score()
def rpoint(self):
self.right_user_score += 1
self.score()