PyGame游戏子弹触左窗口闪烁问题排查求助
2D PyGame游戏子弹左边缘碰撞时画面闪烁问题排查请求
我开发了一款实现角色行走与射击功能的2D PyGame游戏,当前遇到的问题是:子弹触碰到窗口左边缘时画面会闪烁,触碰到右边缘则无此现象。曾猜测是子弹移出数组时屏幕未及时刷新导致,移除循环中的break语句后问题仍未解决,且闪烁现象存在随机性,怀疑可能是代码或其他因素导致,请求帮忙排查。
游戏初始化代码
import pygame pygame.init() win = pygame.display.set_mode((500, 480)) pygame.display.set_caption('Legend of Zorro') walkRight = [pygame.image.load('Game/R1.png'), pygame.image.load('Game/R2.png'), pygame.image.load('Game/R3.png'), pygame.image.load( 'Game/R4.png'), pygame.image.load('Game/R5.png'), pygame.image.load('Game/R6.png'), pygame.image.load('Game/R7.png'), pygame.image.load( 'Game/R8.png'), pygame.image.load('Game/R9.png')] walkLeft = [pygame.image.load('Game/L1.png'), pygame.image.load('Game/L2.png'), pygame.image.load('Game/L3.png'), pygame.image.load( 'Game/L4.png'), pygame.image.load('Game/L5.png'), pygame.image.load('Game/L6.png'), pygame.image.load('Game/L7.png'), pygame.image.load( 'Game/L8.png'), pygame.image.load('Game/L9.png')] bg = pygame.image.load('Game/bg.jpg') char = pygame.image.load('Game/standing.png') clock = pygame.time.Clock() framerate = 45
Player类实现
class Player(object): def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.vel = 10 self.isJump = False self.jumpCount = 10 self.left = True self.right = False self.walkCount = 0 self.standing = True def draw(self, win): if self.walkCount + 1 >= 27: self.walkCount = 0 if not self.standing: if self.left: win.blit(walkLeft[self.walkCount // 3], (self.x, self.y)) self.walkCount += 1 elif self.right: win.blit(walkRight[self.walkCount // 3], (self.x, self.y)) self.walkCount += 1 else: if self.right: win.blit(walkRight[0], (self.x, self.y)) else: win.blit(walkLeft[0], (self.x, self.y))
Projectile类实现(怀疑问题相关)
class projectile(object): def __init__(self, x, y, radius, colour, facing): self.x = x self.y = y self.radius = radius self.colour = colour self.facing = facing self.vel = 8 * facing def draw(self, win): pygame.draw.circle(win, self.colour, (self.x, self.y), self.radius)
画面重绘函数
def redrawGameWindow(): win.blit(bg, (0, 0)) man.draw(win) for bullet in bullets: bullet.draw(win) pygame.display.update() man = Player(300, 410, 64, 64) bullets = []
主循环与核心逻辑
run = True while run: clock.tick(framerate) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False # 子弹更新与移除逻辑 for bullet in bullets: if 500 > bullet.x > 0: bullet.x += bullet.vel else: bullets.remove(bullet) break # 已移除该语句,问题仍存在 # 角色控制逻辑 keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: if man.left: facing = -1 else: facing = 1 if len(bullets) < 1: bullets.append( projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing)) if keys[pygame.K_LEFT] and man.x > man.vel: man.x -= man.vel man.left = True man.right = False man.standing = False elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel: man.x += man.vel man.right = True man.left = False man.standing = False else: man.standing = True man.walkCount = 0 if not man.isJump: if keys[pygame.K_UP]: man.isJump = True man.walkCount = 0 else: if man.jumpCount >= -10: neg = 1 if man.jumpCount < 0: neg = -1 man.y -= (man.jumpCount ** 2) * 0.5 * neg man.jumpCount -= 1 else: man.isJump = False man.jumpCount = 10 redrawGameWindow() pygame.quit()
补充说明(编辑#1)
- 移除break语句后无改善,该语句是之前添加后忘记删除的,移除后闪烁问题依然存在。
- 闪烁似乎仅发生在子弹碰撞窗口左边缘时,但存在随机不出现的情况,因此怀疑问题可能并非完全由代码导致。
内容的提问来源于stack exchange,提问作者Crazy MANE41
相关产品推荐
相关产品推荐

