如何使球减速,然后“反弹”,然后再减速呢?

时间:2019-06-22 19:26:02

标签: python-3.x pygame collision-detection game-physics

我正在用pygame创建网球游戏。我通过StackOverflow修复了碰撞,但是我试图使球在被击中后逐渐减速(就像在现实生活中那样),但是Google和其他帖子并没有帮助。我还希望球停下来,然后放慢速度,模拟现实生活中的“反弹”。

关于此的其他堆栈溢出帖子没有帮助。

import pygame
import time

pygame.init()

# Define some colors
BLACK = (0, 0, 0)
OUT = (193, 58, 34)
COURT = (69, 150, 81)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
SKIN = (232, 214, 162)

ballspeed = 2

# Create the screen
windowsize = (700, 500)
screen = pygame.display.set_mode(windowsize)
pygame.display.set_caption('Tennis')

# Player Sprites
class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("Robert_tennis.png")
        self.rect = self.image.get_rect()
        self.rect.center = (360, 480)
        self.speedx = 0
        self.speedy = 0

    def update(self):
        self.speedx = 0
        self.speedy = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speedx = -4
        if keystate[pygame.K_RIGHT]:
            self.speedx = 4
        self.rect.x += self.speedx
        if self.rect.right > 700:
            self.rect.right = 700
        if self.rect.right < 0:
            self.rect.left = 0
        if keystate[pygame.K_UP]:
            self.speedy = -5
        if keystate[pygame.K_DOWN]:
            self.speedy = 3
        self.rect.y += self.speedy
        if self.rect.y < 235:
            self.rect.y = 235
        if self.rect.y < 0:
            self.rect.y = 0

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("tennisball.png")
        self.rect = self.image.get_rect()
        self.rect.center = (360, 325)
        self.speedx = 0
        self.speedy = 0

    def update(self):
        if tennisball.rect.colliderect(robert):
            robert.image = pygame.image.load("Robert_tennis.png")
            self.speedy = -2
        self.rect = self.rect.move(self.speedx, self.speedy)


#Add myself
all_sprites = pygame.sprite.Group()
robert = Player()
tennisball = Ball()
all_sprites.add(robert)
all_sprites.add(tennisball)

carryOn = True
clock = pygame.time.Clock()

while carryOn:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            carryOn = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                carryOn = False

    all_sprites.update()

    # Fill the screen with a sky color
    screen.fill(OUT)

    # Draw the court
    pygame.draw.rect(screen, COURT, [175, 0, 350, 500])
    pygame.draw.line(screen, WHITE, (170,500), (170,0), 10)
    pygame.draw.line(screen, WHITE, (520,500), (520,0), 10)
    pygame.draw.line(screen, WHITE, (170,130), (520,130), 10)
    pygame.draw.line(screen, WHITE, (170,370), (520,370), 10)
    pygame.draw.line(screen, WHITE, (340,130), (340,370), 10)
    pygame.draw.line(screen, BLACK, (170,250), (520,250), 10)

    # Update
    all_sprites.draw(screen)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

我需要球逐渐减速。这是主要目标。

如果可能的话,一旦它到达另一个法院中间的某个地方,请帮助我实施一次“停止”操作,然后它的速度可能会降低到现在的1.5倍。

0 个答案:

没有答案