简体   繁体   English

如何让球像乒乓球一样移动?

[英]How to keep a ball moving like in pong?

I'm creating a tennis game, similar to pong but will be much more similar to real tennis (ie ability to control the direction and type of your shot).我正在创建一个网球游戏,类似于乒乓球,但更类似于真正的网球(即控制击球方向和类型的能力)。 I think I've got the collision detection with the ball and the player figured out, but when the player collides with the ball it moves a few pixels then stops.我想我已经对球进行了碰撞检测并且玩家已经弄清楚了,但是当玩家与球发生碰撞时,它会移动几个像素然后停止。 I need it to keep going like in pong.我需要它像乒乓球一样继续前进。 Not sure if the problem is with the collision detection or just with something else in the code.不确定问题是与碰撞检测有关还是与代码中的其他内容有关。

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()

When you do当你做

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

then the ball moves only once.那么球只移动一次。 In that "moment" when the player hits the ball and the condition is fulfilled.在玩家击球并满足条件的那个“时刻”。 After the ball has moved, the condition is not fulfilled any more and the ball stops.球移动后,条件不再满足,球停止。

When the racket hits the ball then you've to change the speed of the ball rather than its position.当球拍击球时,你必须改变球的速度而不是它的位置。
Add attributes which store the speed of the ball ( .speedx , .speedy ).添加存储球速度的属性( .speedx.speedy )。 Initialize the attributes by 0. Continuously change the position of the ball by it speed in the method .update() .将属性初始化为 0。在.update()方法中通过速度不断改变球的位置。 When the racket hits the ball then change the speed:当球拍击球时,改变速度:

eg例如

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)

Note, if there is a player at the other side of the court, which hits the ball, then the speed is changed again.注意,如果球场另一侧有球员击球,则速度再次改变。 The speed can also have an sideward component for a diagonal movement.速度还可以具有用于对角运动的侧向分量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM