티스토리 뷰
* 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()
'Test > Python(20220101~)' 카테고리의 다른 글
Days23. Turtle crossing 게임 2 (0) | 2022.03.05 |
---|---|
Days23. Turtle crossing 게임 (0) | 2022.03.05 |
Days21. 뱀게임 최종 (0) | 2022.03.02 |
Days21. 뱀게임7 - Slicing (0) | 2022.03.02 |
Days21. 뱀게임6 - 꼬리를 물면 GAME OVER (0) | 2022.03.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 부동없이
- dp-1
- 차집합
- fromkeys
- dezoomify
- 대소문자
- 배열
- 중복제거
- set()
- 스토리지
- cloud-init
- vmware
- virt-sysprep
- powershell
- sysprep
- EXA
- exadata
- vmware.powercli
- oracle
- 3par
- 정렬
- powercli
- dp-2
- artandculture
- insert
- LIST
- storage
- Join
- 변수화
- 읽어오기
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함