Как начать перемещать мяч по экрану в игре в пинбол. Я пытался увеличить время до -1, когда он достиг границы, но я не думаю, что код был написан правильно. Мне нужно, чтобы мяч двигался, как мяч в обычной игре в пинбол. Я не слишком знаком со столкновениями и тому подобным, но я пытаюсь выучить это.
import pygame
pygame.init()
win = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("First Game")
clock = pygame.time.Clock()
balls = []
run = True
class player():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 10
def redrawGameWindow():
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (player1.x, player1.y, player1.width, player1.height))
pygame.draw.rect(win, (255, 0, 0), (player2.x, player2.y, player2.width, player2.height))
pygame.draw.circle(win, ball.colour, (ball.x, ball.y), ball.radius)
pygame.display.update()
class projectile(object):
def __init__(self, x, y, radius, colour):
self.x = x
self.y = y
self.radius = radius
self.colour = colour
self.vel = 7
ball = projectile(500, 350, 15, (225,225,225))
player1 = player(30, 275, 30, 150)
player2 = player(940, 275, 30, 150)
while run:
clock.tick(30)
neg = -1
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if keys[pygame.K_w] and player1.y > player1.vel:
player1.y -= player1.vel
if keys[pygame.K_s] and player1.y < 700 - player1.height - player1.vel:
player1.y += player1.vel
if keys[pygame.K_UP] and player2.y > player2.vel:
player2.y -= player2.vel
if keys[pygame.K_DOWN] and player2.y < 700 - player2.height - player2.vel:
player2.y += player2.vel
if keys[pygame.K_SPACE]:
if ball.x > 0 and ball.y > 0:
ball.y -= ball.vel
ball.x -= ball.vel
if ball.y == 1:
ball.y *= neg
redrawGameWindow()
pygame.quit()