如何保持球像乒乓球一样运动?

时间:2019-06-22 06:20:36

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

我正在创建一个类似于乒乓球的网球游戏,但它将与真实的网球更加相似(即能够控制击球的方向和类型)。我认为我已经对球进行了碰撞检测,并且球员想出了办法,但是当球员与球碰撞时,它会移动几个像素然后停止。我需要它来继续前进。不确定是冲突检测还是代码中的其他问题。

import pygame

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)

    def update(self):
        if tennisball.rect.colliderect(robert):
            self.rect.y -= 2

#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 个答案:

答案 0 :(得分:1)

这样做的时候

def update(self):
   if tennisball.rect.colliderect(robert):
       self.rect.y -= 2

然后球只移动一次。在那一刻,当运动员击球并满足条件时。球移动后,条件不再满足,球停止。

当球拍击中球时,您必须更改球的速度而不是其位置。
添加存储球速度的属性(.speedx.speedy)。用0初始化属性。在方法.update()中,通过球的速度不断改变球的位置。当球拍击球时,请更改速度:

例如

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):
            self.speedy = -2
        self.rect = self.rect.move(self.speedx, self.speedy)

请注意,如果在球场另一侧有一名球员击中了球,那么速度会再次改变。该速度还可以具有用于对角运动的侧向分量。