100일 간의 코드: 2022년을 위한 완전한 Python Pro 부트캠프 - 22일차(유명한 아케이드 게임 Pong 빌드)
from turtle import Screen
from paddle import Paddle
from ball import Ball
from score import Scoreboard
import time
# Screen setup
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Pong")
screen.tracer(0)
# Initiate 2 x paddles(left, right) and ball
l_paddle = Paddle((-350, 0))
r_paddle = Paddle((350, 0))
ball = Ball()
score = Scoreboard()
# Screen settings
screen.listen()
screen.onkeypress(r_paddle.go_up, "Up")
screen.onkeypress(r_paddle.go_down, "Down")
screen.onkeypress(l_paddle.go_up, "a")
screen.onkeypress(l_paddle.go_down, "s")
# Game logic
game_is_on = True
while game_is_on:
screen.update()
time.sleep(ball.mov_speed)
ball.move()
# Detect collision with wall at ycor, bounce the ball off the 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()
# Detect r_paddle misses
if ball.xcor() > 380:
ball.reset_position()
score.l_point()
# Detect l_paddle misses
if ball.xcor() < -380:
ball.reset_position()
score.r_point()
# Game Over
if score.l_score >= 3 or score.r_score >= 3:
game_is_on = False
score.game_over()
screen.exitonclick()
from turtle import Turtle
class Paddle(Turtle):
def __init__(self, position):
super().__init__()
self.shape("square")
self.color("white")
self.shapesize(stretch_wid=3, stretch_len=1)
self.penup()
self.goto(position)
def go_up(self):
new_y = self.ycor() + 20
self.penup()
self.goto(self.xcor(), new_y)
def go_down(self):
new_y = self.ycor() - 20
self.penup()
self.goto(self.xcor(), new_y)
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.color("white")
self.shapesize(stretch_wid=1, stretch_len=1)
self.penup()
self.x_mov = 10
self.y_mov = 10
self.mov_speed = 0.1
def move(self):
new_x = self.xcor() + self.x_mov
new_y = self.ycor() + self.y_mov
self.goto(new_x, new_y)
def bounce_y(self):
self.y_mov *= -1
self.mov_speed *= 0.9
def bounce_x(self):
self.x_mov *= -1
def reset_position(self):
self.goto(0, 0)
self.mov_speed = 0.1
self.bounce_x()
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Courier", 60, "bold")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.hideturtle()
self.l_score = 0
self.r_score = 0
self.penup()
self.color("white")
self.update_scoreboard()
def update_scoreboard(self):
self.clear()
self.goto(-75, 200)
self.write(self.l_score, align=ALIGNMENT, font=FONT)
self.goto(75, 200)
self.write(self.r_score, align=ALIGNMENT, 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 game_over(self):
self.goto(0, 0)
self.write("Game Over", align=ALIGNMENT, font=FONT)
Reference
이 문제에 관하여(100일 간의 코드: 2022년을 위한 완전한 Python Pro 부트캠프 - 22일차(유명한 아케이드 게임 Pong 빌드)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mike_kameta_aed62d48c2d0f/100-days-of-code-the-complete-python-pro-bootcamp-for-2022-day-22-build-pong-the-famous-arcade-game-4me7텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)