12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- from turtle import Turtle
- from config import SCREEN_HEIGHT, SCREEN_WIDTH, PADDLE_LENGTH
- import time
- import os
- from random import randint
- STEP = 10
- INITIAL_SPEED = 10
- class Ball(Turtle):
- def __init__(self):
- super().__init__()
- self.shape("circle")
- self.setheading(randint(15, 45))
- self.color("white")
- self.speed = INITIAL_SPEED
- self.penup()
- self.goto(0, 0)
-
- def move(self):
- if self.ycor() >= SCREEN_HEIGHT / 2 - STEP - 20 or self.ycor() <= STEP + 20 - SCREEN_HEIGHT / 2:
- # os.system("mpg123 " + "sound/ping.mp3")
- self.hit('ver')
- time.sleep(1 / self.speed)
- self.forward(STEP)
-
- def hit(self, direction):
- if direction == "ver":
- self.setheading(-self.heading())
- elif direction == "hor":
- self.setheading(-self.heading() + 180)
-
- def is_paddle_hit(self, pl, pr):
- if (self.xcor() < 0):
- x = abs(self.xcor() - pl.xcor()) - 20
- y1 = pl.ycor() + PADDLE_LENGTH * 10
- y2 = pl.ycor() - PADDLE_LENGTH * 10
- else:
- x = pr.xcor() - self.xcor() - 20
- y1 = pr.ycor() + PADDLE_LENGTH * 10
- y2 = pr.ycor() - PADDLE_LENGTH * 10
-
- if x < 10 and self.ycor() < y1 and self.ycor() > y2: return True
-
- return False
- def speed_up(self):
- self.speed += 5
-
- def reset(self):
- angle = randint(15, 45)
- if self.heading() < 181:
- angle += 180
- self.setheading(angle)
- self.speed = INITIAL_SPEED
- self.goto(0, 0)
- time.sleep(0.5)
|