TIL: Python Basics Day 22 - Pong Game

이다연·2021년 1월 2일
0

Udemy Python Course

목록 보기
22/64
post-thumbnail

Project: Pong Game

Learning 1: When you add a function under innit function, it initiates it when you create an object. If not, you need to write the "object.function" to execute it.
Learning 2: .goto() , coordinate, is the function that makes the ball moves.
Learning 3: ball.move_speed *= 0.9 increases the speed (0.1 * 0.9)
Learning 4: onkey(function, "key_name") -> function doesn't need () parenthesis

main.py

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

screen = Screen()
screen.setup(width= 800, height=600)
screen.bgcolor("black")
screen.title("Pong Game")
screen.tracer(0) #turn off the animation


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:
    scoreboard.front_page()
    scoreboard.name()
    screen.update()
    time.sleep(ball.move_speed)
    ball.move()


    #Detect collision with wall
    if ball.ycor() > 280 or ball.ycor() < -280:
        ball.bounce_y()
    #Detect collision with both paddle
    if ball.distance(r_paddle) < 40 and ball.xcor() > 330 or ball.distance(l_paddle) < 40 and ball.xcor() < -330:
        time.sleep(0.05)
        ball.bounce_x()

    #Detect when paddle misses the ball
    if ball.xcor() > 370:
        ball.reset_position()
        scoreboard.l_point()

    if ball.xcor() < -370:
        ball.reset_position()
        scoreboard.r_point()

    #rule: 3 point lead wins the game 
    if scoreboard.r_score == 3:
        game_is_on = False
        scoreboard.finish("B")

    elif scoreboard.l_score == 3:
        game_is_on = False
        scoreboard.finish("A")

screen.exitonclick()

scoreboard.py

from turtle import Turtle

FONT =("Courier", 80, "normal")
WIN_FONT =("Courier", 25, "normal")
RULE_FONT =("Courier", 13, "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_scoreboard()

    def update_scoreboard(self):
        self.clear()
        self.goto(-100, 180)
        self.write(self.l_score, align= "center", font=FONT)
        self.goto(100, 180)
        self.write(self.r_score, align= "center", font=FONT)

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

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

    def finish(self, player):
        self.goto(0, 0)
        self.write(f"Player {player} has won the game.", align= "center", font=WIN_FONT)

    def front_page(self):
        self.goto(0, -280)
        self.write(f"The first player to get a 3 point lead wins the game.", align= "center", font=RULE_FONT)
        self.goto(0, -250)
        self.color("white")
        self.pensize(5)
        self.setheading(90)

        for i in range(18):
            self.pendown()
            self.forward(10)
            self.penup()
            self.forward(20)

    def name(self):
        self.color("white")
        self.goto(-150, 250)
        self.write("A", align= "center", font=WIN_FONT)
        self.goto(150, 250)
        self.write("B", align= "center", font=WIN_FONT)

paddle.py

from turtle import Turtle

class Paddle(Turtle):
    def __init__(self, position):
        super().__init__()
        self.shape("square")
        self.color("white")
        self.penup()
        self.turtlesize(stretch_wid=5, stretch_len= 1)
        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)

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 bounce_y(self):
        self.y_move *= -1

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

    def reset_position(self):
        self.home()
        self.move_speed = 0.1
        self.bounce_x()

profile
Dayeon Lee | Django & Python Web Developer

1개의 댓글

comment-user-thumbnail
2022년 11월 26일

Playing games can be not only for fun, but also to make money. For example, if you have the opportunity to play then I advise you to play directly on free bingo . I have been playing there for a few months and I like it very much. So if you want to try something new then I advise you to play there for beginners the site fits just perfectly.

답글 달기