• python으로 pong game을 만들어 보기

    https://github.com/yeschan119/mini-projects/tree/main/pong-game

  • result

    pong.png

  • code

    • main.py

      from turtle import Turtle, Screen
      from paddle import Paddle
      from ball import Ball
      from scoreboard import Scoreboard
      import time
      
      #Break the problem
      ##Create the screen
      ##create and move a paddle
      ##create another paddle
      ##create the ball and make it move
      ##detect collision with all and bounce
      ##detect when paddle misses
      ##keep score
      
      ## create the screen
      screen = Screen()
      screen.bgcolor("black")
      screen.setup(width=800, height=600)
      screen.title("Pong")
      #tracer를 끄지 않으면 paddle은 (0,0)에서 만들어져 원하는 위치로 이동하는 모습이 보인다.
      #이를 꺼야 화면을 켰을 때 원하는 위치로 옮겨지는 모습이 안보임
      screen.tracer(0)
      
      ## create and move a paddle
      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")
      #tracer를 껐기 때문에 매번 screen을 직접 update을 해주지 않으면 화면에 아무 것도 나타나지 않음
      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.bounce_y()
      
          #detect collision with paddle
          if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
              ball.bounce_x()
              
          #r_paddle misses the ball
          if ball.xcor() > 380:
              ball.reset_position()
              scoreboard.l_point()
          
          #l_paddle misses the ball
          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("white")
              self.shape("circle")
              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.goto(0,0)
              self.move_speed = 0.1
              self.bounce_x()
      
    • paddle.py

      from turtle import Turtle, Screen
      
      class Paddle(Turtle):
          def __init__(self, x, y):
              #turtle class를 상속 받아서 초기화 시켜주는 작업
              super(). __init__()
              self.shape("square")
              self.color("white")
              # 100 x 20 사이즈의 패들 생성. 기본이 20 x 20이므로 비율을 아래와 같이 하면 됨
              self.shapesize(stretch_wid=5, stretch_len=1)
              self.penup()
              self.goto(x, y)
              
          def go_up(self):
              new_y = self.ycor() + 22
              self.goto(self.xcor(), new_y)
          
          def go_down(self):
              new_y = self.ycor() - 22
              self.goto(self.xcor(), new_y)
      
    • scoreboard.py

      from turtle import Turtle
      
      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, 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_scoreboard()
          
          def r_point(self):
              self.r_score += 1
              self.update_scoreboard()