如何让我的球反弹?

时间:2014-01-06 22:36:51

标签: python pygame

当我移动到屏幕大小的10%时,我想让我的“球”(player1)“反弹”。现在,它只是停止加速。

我很确定我必须对self.vel["x_vel"]["x_dir"]self.vel["y_vel"]["y_dir"]做些什么。我认为通过将self.vel["x_vel"]["x_mag"]self.vel["y_vel"]["y_mag"]设置为零,它会完全停止移动,但它会停止加速。我还认为通过将self.vel["x_vel"]["x_dir"]self.vel["y_vel"]["y_dir"]乘以-1,我会“翻转”它移动的方向,但这样做似乎并没有影响任何事情。

当我说我想让它“反弹”时,我的意思是我希望它立即停止,反转方向,并按照玩家按下的任何方向前进。例如,如果你从一开始就持有s,我希望它看起来就像你丢了一个球。

import sys
import pygame

pygame.init()

screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
screen_rect = screen.get_rect()

clock = pygame.time.Clock()

fps = 30


class Character(object):
    def __init__(self, surface):
        self.surface = surface
        self.x_mag = 0
        self.y_mag = 0
        self.x_dir = 0
        self.y_dir = 0
        self.vel = {"x_vel":
                        {"x_mag": self.x_mag, "x_dir": self.x_dir},
                    "y_vel":
                        {"y_mag": self.y_mag, "y_dir": self.y_dir}}
        self.x_pos = (screen_width / 2)
        self.y_pos = (screen_height / 2)
        self.pos = {"x_pos": self.x_pos, "y_pos": self.y_pos}
        self.size = (10, 10)

    def move_right(self):
        self.vel["x_vel"]["x_dir"] = 1
        self.vel["y_vel"]["y_dir"] = 0

        self.vel["x_vel"]["x_mag"] += 5
        self.pos["x_pos"] += (self.vel["x_vel"]["x_mag"] * self.vel["x_vel"]["x_dir"])

    def move_left(self):
        self.vel["x_vel"]["x_dir"] = 1
        self.vel["y_vel"]["y_dir"] = 0

        self.vel["x_vel"]["x_mag"] -= 5
        self.pos["x_pos"] += (self.vel["x_vel"]["x_mag"] * self.vel["x_vel"]["x_dir"])

    def move_up(self):
        self.vel["x_vel"]["x_dir"] = 0
        self.vel["y_vel"]["y_dir"] = 1

        self.vel["y_vel"]["y_mag"] -= 5
        self.pos["y_pos"] += (self.vel["y_vel"]["y_mag"] * self.vel["y_vel"]["y_dir"])

    def move_down(self):
        self.vel["x_vel"]["x_dir"] = 0
        self.vel["y_vel"]["y_dir"] = 1

        self.vel["y_vel"]["y_mag"] += 5
        self.pos["y_pos"] += (self.vel["y_vel"]["y_mag"] * self.vel["y_vel"]["y_dir"])

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            self.move_up()
        if keys[pygame.K_a]:
            self.move_left()
        if keys[pygame.K_s]:
            self.move_down()
        if keys[pygame.K_d]:
            self.move_right()

        if self.pos["x_pos"] <= (screen_width * .1) or self.pos["x_pos"] >= (screen_width * .9):
            self.vel["x_vel"]["x_mag"] = 0
            self.vel["x_vel"]["x_dir"] *= -1

        if self.pos["y_pos"] <= (screen_height * .1) or self.pos["y_pos"] >= (screen_height * .9):
            self.vel["y_vel"]["y_mag"] = 0
            self.vel["y_vel"]["y_dir"] *= -1

        self.character = pygame.Rect((self.pos["x_pos"], self.pos["y_pos"]), self.size)

    def display(self):
        pygame.draw.rect(self.surface, (255, 255, 255), self.character)


def main():
    player1 = Character(screen)
    while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

        player1.move()

        screen.fill((0, 0, 0))

        player1.display()

        pygame.display.update(screen_rect)
        clock.tick(fps)

if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:3)

你有这个权利before!您应该反转velocity的方向,但保持相同的幅度。

同样,我只使用2元组,其中每个值是适当轴的大小和方向的乘积。然后倒车很简单:

# bounce off top
self.velocity = (self.velocity[0], -1 * self.velocity[1])

答案 1 :(得分:1)

您实际上不需要单独存储x_magx_dir - 它们可以存储在单个变量x_vel中,其中符号代表方向。您还有两个单独的x_mag - self.x_magself.vel['x_vel']['x_mag']副本。这是不好的做法,因为改变一个不会改变另一个。正如jonrsharpe所示,我只会使用tuplelist作为速度和位置:

class Character(object):
    def __init__(self, surface):
        self.surface = surface
        self.velocity = [0, 0]
        self.position = [screen_width / 2, screen_height / 2]

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            self.velocity[1] += 5 # move up
        if keys[pygame.K_a]:
            self.velocity[0] -= 5 # move left
        if keys[pygame.K_s]:
            self.velocity[1] -= 5 # move down
        if keys[pygame.K_d]:
            self.velocity[0] += 5 # move right

        self.position[0] += self.velocity[0]
        self.position[1] += self.velocity[1]

        if self.position[1] <= (screen_height * .1) or self.position[1] >= (screen_width * .9):
            self.velocity[0] *= -1