Skip to main content
 首页 » 编程设计

python之在pygame中发生碰撞后如何摆脱物体

2024年11月24日12lexus

我创建了一个程序,可以让方 block 在屏幕上四处弹跳。每次他们与玩家接触时,他们都会消失(我改变他们的位置,使他们在显示器之外),因此用户看不到。是否有可能不仅改变它们的位置,而且完全摆脱它们,这样 CPU 就不必处理它们。

import pygame 
import random 
import sys 
import time 
 
Height = 800 
Width = 800 
Steps = 0 
running = True 
Number = 2000 
x = 0 
 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 
 
pygame.init() 
display = pygame.display.set_mode((Height, Width)) 
clock = pygame.time.Clock() 
pygame.display.set_caption("Game") 
 
 
class Ball(object): 
 
    def __init__(self, ball_pos_x, ball_pos_y, ball_height, ball_width, delta_x, delta_y): 
        self.ball_pos_x = ball_pos_x 
        self.ball_pos_y = ball_pos_y 
        self.ball_height = ball_height 
        self.ball_width = ball_width 
        self.delta_x = delta_x 
        self.delta_y = delta_y 
 
    def draw(self): 
 
        pygame.draw.rect(display, WHITE, (self.ball_pos_x, self.ball_pos_y, 
                                          self.ball_height, self.ball_height)) 
 
    def update(self): 
        self.ball_pos_x += self.delta_x 
        self.ball_pos_y += self.delta_y 
 
        if self.ball_pos_x < 0: 
            self.delta_x = self.delta_x * -1 
        if self.ball_pos_x > Width - self.ball_width: 
            self.delta_x = self.delta_x * -1 
 
        if self.ball_pos_y < 0: 
            self.delta_y = self.delta_y * -1 
        if self.ball_pos_y > Height - self.ball_height: 
            self.delta_y = self.delta_y * -1 
 
 
class Player: 
    def __init__(self, player_height, player_width, pos_x, pos_y, player_vel): 
        self.player_height = player_height 
        self.player_width = player_width 
        self.player_vel = player_vel 
        self.pos_x = pos_x 
        self.pos_y = pos_y 
        self.rect = self.pos_x 
 
    def draw(self): 
        pygame.draw.rect(display, BLUE, (self.pos_x, self.pos_y, 
                                         self.player_height, self.player_width)) 
 
    def update(self): 
        keys = pygame.key.get_pressed() 
        if keys[pygame.K_LEFT] and self.pos_x > 0: 
            self.pos_x -= self.player_vel 
        if keys[pygame.K_RIGHT] and self.pos_x < Width - player.player_width: 
            self.pos_x += self.player_vel 
        if keys[pygame.K_UP] and self.pos_y > 0: 
            self.pos_y -= self.player_vel 
        if keys[pygame.K_DOWN] and self.pos_y < Height - player.player_height: 
            self.pos_y += self.player_vel 
 
    def collision(self): 
 
        pass 
 
 
player = Player(10, 10, Width/2, Height/2, 5) 
""" 
keys = pygame.key.get_pressed() 
if keys[pygame.K_LEFT] and x > 0: 
    x -= vel 
if keys[pygame.K_RIGHT] and x < 800 - 30: 
    x += vel 
""" 
 
list = [] 
for i in range(Number): 
    ball = Ball(random.randrange(0, Width - 10), random.randrange(0, Height - 10), 
                10, 10, random.randint(-10, 10), random.randint(-10, 10)) 
    list.append(ball) 
 
 
while running: 
    display.fill(BLACK) 
    clock.tick(60) 
 
    for event in pygame.event.get(): 
 
        if event.type == pygame.QUIT: 
            pygame.quit() 
 
    # Update 
 
    # Draw 
    for ball in list: 
        ball.draw() 
        ball.update() 
 
        if (ball.ball_pos_x >= player.pos_x - ball.ball_width and ball.ball_pos_x <= player.pos_x + player.player_width and 
                ball.ball_pos_y >= player.pos_y - ball.ball_height and ball.ball_pos_y <= player.pos_y + player.player_height): 
            ball.ball_pos_x = -20 
            ball.delta_x = -1 
 
            x += 1 
            print(x) 
 
    player.draw() 
    player.update() 
 
    pygame.display.flip() 
 
pygame.quit() 

请您参考如下方法:

您可以在 Sprite 类中使用self.kill()。这应该将它们从 Sprite 组中移除。尽管据我所知您需要继承 pygame.sprite.Sprite 并将 Sprite 添加到组中。

那么让我们说:

examplegroup = pygame.sprite.Group() 
 
class Ball(pygame.sprite.Sprite): 
    def __init__(self): 
        super().__init__(examplegroup): 

并且在 update() 函数中...

if collided: 
    self.kill() 

此外,与列表相比,使用 Sprite 组的优点之一是它通常速度更快,并且可以让您更轻松地执行与 pygame.sprite.spritecollide() 的碰撞。