Я делаю классический Понг в Python, но мне нужна помощь.
Для / в этой игре я хочу рассчитать траекторию мяча, поэтому для заданного beginPoint (отскок отлевый весло) и угол (зеленая вещь) Я хочу рассчитать конечную точку (синий X).Это все для того, чтобы переместить правую ракетку к мячу, чтобы вы могли играть в понг самостоятельно.
Вот изображение проблемы, чтобы прояснить ее: https://imgur.com/OySberQ
IЯ пытался придумать какой-то алгоритм с использованием тригонометрии, но смог вычислить только первую точку, а затем застрял:
def calculateTragectory(beginY, angle):
distanceX = 0
direction = ball.directionY
while True:
if direction == -1:
distanceX += (height - beginY) * math.tan(angle)
direction == 1
elif direction == 1:
distanceX += beginY * math.tan(angle)
direction == -1
Вот остаток кода:
import pygame
import random
import math
pygame.init()
width = 1000
height = 600
leftScore = 0
rightScore = 0
delay = 2
paddleSpeed = 2
ballSpeed = 1
paddleWidth = 15
paddleHeight = 70
ballRadius = 10
spacingX = 3*paddleWidth
window = pygame.display.set_mode((width, height))
font = pygame.font.SysFont('Arial', 128)
class Paddle:
def __init__(self, side):
# left = true, right = false
if side:
self.x = spacingX
else: self.x = width-spacingX
self.y = 300
def move(self, UpOrDown):
if UpOrDown:
self.y -= paddleSpeed
else: self.y += paddleSpeed
def draw(self):
pygame.draw.rect(window, (255, 255, 255), (self.x, self.y, paddleWidth, paddleHeight))
class Ball():
def __init__(self):
self.x = width/2
self.y = height/2
self.directionX = -1
self.directionY = 1
def move(self):
if self.y > height or self.y < 0:
self.directionY *=-1
self.x += ballSpeed*self.directionX
self.y += ballSpeed*self.directionY
def draw(self):
pygame.draw.circle(window, (255, 255, 255), (int(self.x), int(self.y)), ballRadius)
def reset(self):
self.x = width/2
self.y = random.uniform(50, 550)
self.directionX *= -1
def keyInput():
# Key inputs True = Up, down is False
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and leftPaddle.y > 0:
leftPaddle.move(True)
if keys[pygame.K_s] and leftPaddle.y < height - paddleHeight:
leftPaddle.move(False)
if keys[pygame.K_SPACE]:
ball.x = width/2
ball.y = height/2
def collisionDetection():
if ball.x == leftPaddle.x + paddleWidth + ballRadius and leftPaddle.y <= ball.y <= leftPaddle.y + paddleHeight:
ball.directionX *= -1
if ball.x == computerPaddle.x - ballRadius and computerPaddle.y <= ball.y <= computerPaddle.y + paddleHeight:
ball.directionX *= -1
def scoreUpdate():
global rightScore, leftScore
if ball.x <= 0:
rightScore += 1
ball.reset()
elif ball.x >= width:
leftScore +=1
ball.reset()
def calculateTragectory(beginY, angle):
distanceX = 0
direction = ball.directionY
while True:
if direction == -1:
distanceX += (height - beginY) * math.tan(angle)
direction == 1
elif direction == 1:
distanceX += beginY * math.tan(angle)
direction == -1
a = beginPoint * math.tan(angle)
#Init paddles
leftPaddle = Paddle(True)
computerPaddle = Paddle(False)
ball = Ball()
#Game loop:
while True:
pygame.time.delay(delay)
ball.move()
collisionDetection()
scoreUpdate()
scoreText_left = font.render(str(leftScore), True, (255, 255, 255))
scoreText_right = font.render(str(rightScore), True, (255, 255, 255))
keyInput()
window.fill((0, 0, 0))
window.blit(scoreText_left, (350-64, 50))
window.blit(scoreText_right, (650, 50))
leftPaddle.draw()
computerPaddle.y = ball.y
computerPaddle.draw()
ball.draw()
pygame.display.update()
#Exit
for event in pygame.event.get():
if event.type == pygame.QUIT:
if event.key == pygame.K_ESCAPE:
pygame.quit()