拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Pygame中闪烁的精灵

Pygame中闪烁的精灵

白鹭 - 2022-01-23 2154 0 0

我知道很多人都在这里的 pygame 中遇到闪烁影像的问题,但没有一个回应对我有帮助。我正在尝试制作 Space Invaders,但是,子弹在荧屏上移动时会闪烁。请尽量帮助我,谢谢!我目前不关心子弹的大小、位置或比例,我知道它看起来不太好,但我只想让它正确显示!下面是代码:

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)

uj5u.com热心网友回复:

该问题是由多次呼叫引起的pygame.display.update()在应用程序回圈结束时更新显示就足够了。多次呼叫pygame.display.update()pygame.display.flip()导致闪烁。

洗掉pygame.display.flip()来自shoot

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)
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *