gpt4 book ai didi

python - 在 pygame 中执行 for 循环后,图像不会保留在屏幕上

转载 作者:行者123 更新时间:2023-12-01 00:04:34 26 4
gpt4 key购买 nike

我一直在尝试修改此Tutorial中的代码这样子弹击中敌人后 player.png图像显示在位置 x = 60y = 48 。但图像不会保持固定,它只是出现然后消失。我不知道代码中错误或缺失的元素到底在哪里,但我怀疑原因之一是误用了我的 for 部分。 draw_reaction 内循环功能。

我的player.png图片

我的 MWE 代码:

import pygame
import random

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# --- Classes


class Block(pygame.sprite.Sprite):
""" This class represents the block. """
def __init__(self, color):
# Call the parent class (Sprite) constructor
super().__init__()

self.image = pygame.Surface([20, 15])
self.image.fill(color)

self.rect = self.image.get_rect()


class Player(pygame.sprite.Sprite):
""" This class represents the Player. """

def __init__(self):
""" Set up the player on creation. """
# Call the parent class (Sprite) constructor
super().__init__()

self.image = pygame.Surface([20, 20])
self.image.fill(RED)

self.rect = self.image.get_rect()

def update(self):
""" Update the player's position. """
# Get the current mouse position. This returns the position
# as a list of two numbers.
pos = pygame.mouse.get_pos()

# Set the player x position to the mouse x position
self.rect.x = pos[0]


class Bullet(pygame.sprite.Sprite):
""" This class represents the bullet . """
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()

self.image = pygame.Surface([4, 10])
self.image.fill(BLACK)

self.rect = self.image.get_rect()

def update(self):
""" Move the bullet. """
self.rect.y -= 3


# --- Create the window

# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])

# --- Sprite lists

# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()

# List of each block in the game
block_list = pygame.sprite.Group()

# List of each bullet
bullet_list = pygame.sprite.Group()

# --- Create the sprites

for i in range(0,1,2):
# This represents a block
block = Block(BLUE)

# Set a random location for the block
block.rect.x = random.randrange(screen_width)
block.rect.y = random.randrange(350)

# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)

# Create a red player block
player = Player()
all_sprites_list.add(player)
player_image = pygame.image.load("player.png").convert()
# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

score = 0
player.rect.y = 370

# -------- Main Program Loop -----------
while not done:
# --- Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

elif event.type == pygame.MOUSEBUTTONDOWN:
# Fire a bullet if the user clicks the mouse button
bullet = Bullet()
# Set the bullet so it is where the player is
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
# Add the bullet to the lists
all_sprites_list.add(bullet)
bullet_list.add(bullet)

# --- Game logic

# Call the update() method on all the sprites


# Calculate mechanics for each bullet
for bullet in bullet_list:

# See if it hit a block
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)

# For each block hit, remove the bullet and add to the score
for block in block_hit_list:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
score += 1
print(score)

# Remove the bullet if it flies up off the screen
if bullet.rect.y < -10:
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
def draw_reaction():
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
screen.blit(player_image, [60, 48])
all_sprites_list.update()
# --- Draw a frame

# Clear the screen
screen.fill(WHITE)
draw_reaction()
# Draw all the spites
all_sprites_list.draw(screen)

# Go ahead and update the screen with what we've drawn.
pygame.display.flip()

# --- Limit to 20 frames per second
clock.tick(60)

pygame.quit()

编辑更新:游戏只有一个敌人,每次游戏再次开始时,该敌人都会出现在随机位置。

编辑更新2:我知道在大多数游戏中,碰撞后敌人就会从屏幕上消失。这也发生在我的代码中。但出于我的特殊需求,我需要这样的结果:在子弹与敌人碰撞后,图像 (player.png) 始终保持固定。

最佳答案

该问题是由 draw_reaction() 函数仅在子弹与方 block 碰撞的绝对时间期间绘制 player_image 引起的。因此它显示了单帧的图像(六十分之一秒),但随后在下一个循环中不再发生碰撞(子弹被移除),因此它永远不会被绘制。

有很多方法可以解决这个问题,但我不确定显示此位图的目的,所以也许它们没有应有的帮助。

最简单的解决方法可能是将玩家图像变成 Sprite ,然后在子弹击中触发时将其添加到现有的 all_sprites_list 中,然后在一段时间后将其删除(或者如何它应该可以工作)。

class PlayerShip( pygame.sprite.Sprite ):
def __init__(self):
super().__init__()
self.image = pygame.image.load( "player.png" ) #.convert()
self.rect = self.image.get_rect()
self.rect.topleft = ( 60, 48 )

然后:

player_sprite = PlayerShip()

def draw_reaction():
for bullet in bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
for block in block_hit_list:
# TODO: ensure it's not showing already
all_sprites_list.add( player_sprite ) # add the player sprite
break # only add once

编辑:根据评论更新代码片段

关于python - 在 pygame 中执行 for 循环后,图像不会保留在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60067478/

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