gpt4 book ai didi

python - 如何使用 python 和 pygame 删除 rects/sprites

转载 作者:行者123 更新时间:2023-11-28 22:52:50 28 4
gpt4 key购买 nike

我正在开发一款游戏,我想添加一个随机生成的弹药箱。到目前为止,盒子会按预期生成,如果你越过它,它就会被移除并给你 200+ 弹药。但是,它只能提供 50 发弹药。然后在游戏的后期,当玩家回到那个地方时,他们会继续获得更多弹药。如果我只是坐在那里,我最终会得到 1000+ 弹药。

这是我的弹药等级:

class Ammo(pygame.sprite.Sprite):
def __init__(self, color, x, y, player = None):
pygame.sprite.Sprite.__init__(self)

self.image = pygame.Surface([20, 20])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y

def update(self):
pass

这是游戏循环:

class Game():
def __init__(self):
pygame.init()

screen_width = 850
screen_height = 640

place_ammo = False

self.screen = pygame.display.set_mode( (screen_width,screen_height) )

pygame.mouse.set_visible(False)

#-----

self.all_sprites_list = pygame.sprite.Group()
self.block_list = pygame.sprite.Group()
self.bullet_list = pygame.sprite.Group()

self.blitwave = 1

# --- create sprites ---

self.background = Background()

self.player = Player(self.screen.get_rect(), 0, 370)
self.all_sprites_list.add(self.player)
self.ammo = Ammo(self.screen.get_rect(),random.randrange(10,screen_width),random.randint(10,screen_height - 10))
self.all_sprites_list.add(self.ammo)

self.ammo_amount = 10
self.on_screen = 1
self.score = 0

self.crosshair = Crosshair()

def bullet_create(self, start_pos, mouse_pos):
bullet = Bullet(start_pos, mouse_pos)

self.all_sprites_list.add(bullet)
self.bullet_list.add(bullet)

def bullets_update(self):
for bullet in self.bullet_list:

block_hit_list = pygame.sprite.spritecollide(bullet, self.block_list, True)
screen_width = 850
screen_height = 640

for block in block_hit_list:
self.bullet_list.remove(bullet)
self.all_sprites_list.remove(bullet)
self.score += 1
self.on_screen -= 1
self.ammo_chance = self.ammo_amount * 5
if self.ammo_chance > 0:
self.drop = random.randint(1, self.ammo_chance)
print(self.drop)
if self.drop > 0 and self.drop < 2:

print('ammo drop')
self.ammo = Ammo(self.screen.get_rect(),random.randrange(10,screen_width),random.randint(10,screen_height - 10))
self.all_sprites_list.add(self.ammo)

if bullet.rect.y < -10:
self.bullet_list.remove(bullet)
self.all_sprites_list.remove(bullet)

# -------- Main Program Loop -----------

def run(self):
screen_width = 850
screen_height = 640

#wave
self.wave = 1
self.wave_no = 2
self.wave_running = True
block = Block(BLUE, random.randrange(100, screen_width), random.randrange(10, screen_height-10), self.player)

self.block_list.add(block)
self.all_sprites_list.add(block)

clock = pygame.time.Clock()

self.cash = 1
self.health = 100
self.ammo_amount = 10

RUNNING = True
PAUSED = False

while RUNNING:
# --- events ---
if self.player.health <= 0:
RUNNING = False
for event in pygame.event.get():

if event.type == pygame.QUIT:
RUNNING = PAUSED

elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
PAUSED = True

elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
PAUSED = not PAUSED

elif event.type == pygame.MOUSEBUTTONDOWN and self.ammo_amount > 0:
self.bullet_create(self.player, event.pos)
self.ammo_amount -= 1

self.cash = self.score * 5

if self.on_screen == 0:
for i in range(self.wave_no):
block = Block(BLUE, random.randrange(100, screen_width), random.randrange(10, screen_height-10), self.player)

self.block_list.add(block)
self.all_sprites_list.add(block)
self.on_screen += 1

self.wave_div = int(self.wave_no / 2)
self.wave_no += self.wave_div
self.wave += 1

#wave font
font = pygame.font.SysFont("", 34)
self.text_pause = font.render("WAVE " + str(self.wave) * self.blitwave, -1, RED)
self.text_pause_rect = self.text_pause.get_rect(center=self.screen.get_rect().center) # center text


#health font
self.text_health = font.render("|" * self.player.health, -1, RED)

#score font
self.text_score = font.render("SCORE " + str(self.score), -1, BLACK)

#cash font
self.text_cash = font.render("CASH " + str(self.cash), -1, GREEN)

#ammo font
self.text_ammo = font.render("AMMO " + str(self.ammo_amount), -1, RED)

# send event to player
self.player.event_handler(event)

if not PAUSED:
self.all_sprites_list.update()
self.bullets_update()

player_rect = pygame.Rect(self.player.rect.x, self.player.rect.y, 20, 20)
block_rect = pygame.Rect(block.rect.x, block.rect.y, 20, 20)

if player_rect.colliderect(block_rect):
self.player.health = self.player.health - 3

if player_rect.colliderect(self.ammo.rect):
self.ammo_amount += 50
self.all_sprites_list.remove(self.ammo)

self.crosshair.update()

# --- draws ---
self.background.draw(self.screen)
self.all_sprites_list.draw(self.screen)

#must be last
self.screen.blit(self.text_pause, (10, 610))
self.screen.blit(self.text_score, (700, 585))
self.screen.blit(self.text_cash, (700, 560))
self.screen.blit(self.text_ammo, (700, 610))
self.screen.blit(self.text_health, (10, 10))
self.crosshair.draw(self.screen)

pygame.display.update() # use flip() OR update()

# --- FPS ---

clock.tick(70)

# --- quit ---

pygame.quit()

#----------------------------------------------------------------------

Game().run()

最佳答案

Sprite 确实被移除了,但是被调用来检查碰撞的 self.ammo 即使在 Sprite 被移除后仍然有效。这是我要做的:

if self.ammo and player_rect.colliderect(self.ammo.rect):
self.ammo_amount += 50
self.all_sprites_list.remove(self.ammo)
self.ammo = None

关于python - 如何使用 python 和 pygame 删除 rects/sprites,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065430/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com