Pygame 中的闪烁精灵
Flickering Sprite in Pygame
我知道很多人都遇到过此处 pygame 中的图像闪烁问题,但 none 的回复对我有所帮助。我试图让 Space 入侵者,但是,子弹在屏幕上移动时会闪烁。请尝试帮助我,谢谢! 我目前不关心子弹的大小、位置或比例,我知道它看起来不太好,但我只是想让它正常显示! 下面是代码:
import pygame
#import sys- might use later
import random
#Sets the starting values for screen, etc. of the playing space
pygame.init()
size = (800, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Space Invaders")
play = True
clock = pygame.time.Clock()
blkColor = (0, 0, 0)
#Loads and sizes the alien and player ship(s)
playerShip = pygame.image.load('ship.png')
playerShip = pygame.transform.scale(playerShip, (50, 50))
playerX = 370
playerY = 520
alien = pygame.image.load('alien.png')
alien = pygame.transform.scale(alien, (35, 35))
alienX = random.randint(0, 750)
alienY = 0
move = 5
alienMove = 5
bullet = pygame.image.load('bullet.png')
bullet = pygame.transform.scale(bullet, (5, 100))
bulletX = 0
bulletY = 600
hit = False
fire = False
hitRangeMin = -35
hitRangeMax = 35
score = 0
def player():
screen.blit(playerShip, (playerX, playerY))
def enemy():
screen.blit(alien, (alienX, alienY))
def alienMovement():
global alienX
global alienY
global alienMove
#Moves the alien across the screen; when it hits the edge, it shifts down one spot and goes the other direction
alienX += alienMove
if alienX > 750:
alienMove = -5
alienY += 35
if alienX < 0:
alienMove = 5
alienY += 35
def shoot(x, y):
global fire
global bulletY
fire = True
screen.blit(bullet, (x, y))
pygame.display.flip()
if bulletY < 0:
fire = False
bulletY = 550
elif bulletY >= 0:
fire = True
def gameOver(score):
print('Will add score and display and stuff- does noo matter.')
# Keeps the game window open until exited
while not hit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
player()
enemy()
key_input = pygame.key.get_pressed()
if key_input[pygame.K_LEFT]:
playerX -= move
elif key_input[pygame.K_RIGHT]:
playerX += move
if playerX > 800:
playerX = 0
if playerX < 0:
playerX = 800
#For shooting the bullet
if key_input[pygame.K_SPACE]:
bulletX = playerX + 23
shoot(bulletX, bulletY)
##Loops with function "shoot" to move bullet up the screen
if fire:
shoot(bulletX, bulletY)
bulletY -= 5
screen.fill(blkColor)
alienMovement()
player()
enemy()
pygame.display.flip()
clock.tick(60)
问题是由多次调用 pygame.display.update()
引起的。在应用程序循环结束时更新显示就足够了。多次调用 pygame.display.update()
或 pygame.display.flip()
会导致闪烁。
从 shoot
中删除 pygame.display.flip()
:
def shoot(x, y):
global fire
global bulletY
fire = True
screen.blit(bullet, (x, y))
# pygame.display.flip() <--- DELETE
if bulletY < 0:
fire = False
bulletY = 550
elif bulletY >= 0:
fire = True
并且在绘制子弹之前必须清空屏幕:
while not hit:
# [...]
screen.fill(blkColor) # <--- INSERT
if key_input[pygame.K_SPACE]:
bulletX = playerX + 23
shoot(bulletX, bulletY)
##Loops with function "shoot" to move bullet up the screen
if fire:
shoot(bulletX, bulletY)
bulletY -= 5
# screen.fill(blkColor) <--- DELETE
alienMovement()
player()
enemy()
pygame.display.flip()
clock.tick(60)
我知道很多人都遇到过此处 pygame 中的图像闪烁问题,但 none 的回复对我有所帮助。我试图让 Space 入侵者,但是,子弹在屏幕上移动时会闪烁。请尝试帮助我,谢谢! 我目前不关心子弹的大小、位置或比例,我知道它看起来不太好,但我只是想让它正常显示! 下面是代码:
import pygame
#import sys- might use later
import random
#Sets the starting values for screen, etc. of the playing space
pygame.init()
size = (800, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Space Invaders")
play = True
clock = pygame.time.Clock()
blkColor = (0, 0, 0)
#Loads and sizes the alien and player ship(s)
playerShip = pygame.image.load('ship.png')
playerShip = pygame.transform.scale(playerShip, (50, 50))
playerX = 370
playerY = 520
alien = pygame.image.load('alien.png')
alien = pygame.transform.scale(alien, (35, 35))
alienX = random.randint(0, 750)
alienY = 0
move = 5
alienMove = 5
bullet = pygame.image.load('bullet.png')
bullet = pygame.transform.scale(bullet, (5, 100))
bulletX = 0
bulletY = 600
hit = False
fire = False
hitRangeMin = -35
hitRangeMax = 35
score = 0
def player():
screen.blit(playerShip, (playerX, playerY))
def enemy():
screen.blit(alien, (alienX, alienY))
def alienMovement():
global alienX
global alienY
global alienMove
#Moves the alien across the screen; when it hits the edge, it shifts down one spot and goes the other direction
alienX += alienMove
if alienX > 750:
alienMove = -5
alienY += 35
if alienX < 0:
alienMove = 5
alienY += 35
def shoot(x, y):
global fire
global bulletY
fire = True
screen.blit(bullet, (x, y))
pygame.display.flip()
if bulletY < 0:
fire = False
bulletY = 550
elif bulletY >= 0:
fire = True
def gameOver(score):
print('Will add score and display and stuff- does noo matter.')
# Keeps the game window open until exited
while not hit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
player()
enemy()
key_input = pygame.key.get_pressed()
if key_input[pygame.K_LEFT]:
playerX -= move
elif key_input[pygame.K_RIGHT]:
playerX += move
if playerX > 800:
playerX = 0
if playerX < 0:
playerX = 800
#For shooting the bullet
if key_input[pygame.K_SPACE]:
bulletX = playerX + 23
shoot(bulletX, bulletY)
##Loops with function "shoot" to move bullet up the screen
if fire:
shoot(bulletX, bulletY)
bulletY -= 5
screen.fill(blkColor)
alienMovement()
player()
enemy()
pygame.display.flip()
clock.tick(60)
问题是由多次调用 pygame.display.update()
引起的。在应用程序循环结束时更新显示就足够了。多次调用 pygame.display.update()
或 pygame.display.flip()
会导致闪烁。
从 shoot
中删除 pygame.display.flip()
:
def shoot(x, y):
global fire
global bulletY
fire = True
screen.blit(bullet, (x, y))
# pygame.display.flip() <--- DELETE
if bulletY < 0:
fire = False
bulletY = 550
elif bulletY >= 0:
fire = True
并且在绘制子弹之前必须清空屏幕:
while not hit:
# [...]
screen.fill(blkColor) # <--- INSERT
if key_input[pygame.K_SPACE]:
bulletX = playerX + 23
shoot(bulletX, bulletY)
##Loops with function "shoot" to move bullet up the screen
if fire:
shoot(bulletX, bulletY)
bulletY -= 5
# screen.fill(blkColor) <--- DELETE
alienMovement()
player()
enemy()
pygame.display.flip()
clock.tick(60)