Ping-pong game logic analysis:
1. Create the screen
2. Create and move paddle
3. Create another paddle
4. Create the ball and make it move
5. Detect collision with wall and bounce
6. Detect collision with paddle
7. Detect when paddle misses
8. Keep score
main.py
from turtle import Turtle, Screen
from paddle import Paddle
from ball import Ball
import time
from scoreboard import Scoreboard
screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.title("Ping-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()
#Detect collision with wall
if ball.ycor() > 280 or ball.ycor() < -280:
ball.wall_bounce()
#Detect collision with r_paddle
if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
ball.paddle_bounce()
#Detect R paddle misses
if ball.xcor() > 380:
ball.reset_position()
scoreboard.l_point()
#Detect L paddle misses
if ball.xcor() < -380:
ball.reset_position()
scoreboard.r_point()
#Game over
if scoreboard.l_score == 5:
scoreboard.game_over(winner="Left")
game_is_on = False
if scoreboard.r_score == 5:
scoreboard.game_over(winner="Right")
game_is_on = False
screen.exitonclick()
ball.py
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.color("white")
self.penup()
self.x_move = 10
self.y_move = 10
self.move_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 wall_bounce(self):
self.y_move *= -1
def paddle_bounce(self):
self.x_move *= -1
self.move_speed *= 0.9
def reset_position(self):
self.goto(0,0)
self.move_speed = 0.1
self.paddle_bounce()
padding.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() + 40
self.goto(self.xcor(), new_y)
def go_down(self):
new_y = self.ycor() - 40
self.goto(self.xcor(), new_y)
scoreboard.py
from turtle import Turtle
FONT = ("Courier", 80, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.penup()
self.hideturtle()
self.l_score = 0
self.r_score = 0
self.update_socrebaord()
def update_socrebaord(self):
self.clear()
self.goto(-100, 200)
self.write(self.l_score, align="center", font=(FONT))
self.goto(100, 200)
self.write(self.r_score, align="center", font=(FONT))
def l_point(self):
self.l_score += 1
self.update_socrebaord()
def r_point(self):
self.r_score += 1
self.update_socrebaord()
def game_over(self, winner):
self.goto(0,0)
self.write(f"Game Over! {winner} wins!", align="center", font=("Courier", 16, "normal"))
Realization:
It is important to thoroughly analyze the logic before starting to code a project.
'Python' 카테고리의 다른 글
Seventh Python Project: Turtle Crossing Game (1) | 2024.08.17 |
---|---|
Fifth Python Project: Snake Game - 2 (0) | 2024.08.10 |
Fifth Python Project: Snake Game - 1 (0) | 2024.08.05 |
Fourth Python Project: Turtle Racing Game (0) | 2024.08.04 |
Third Python Project: Quiz game (1) | 2024.08.03 |