pygame-黑屏显示

时间:2020-03-15 17:20:06

标签: python pygame

我刚刚开始学习python。我正在制作太空侵略者游戏,我不知道为什么,但是每次我运行代码时,直到我关闭它,显示屏都是黑色的。我检查了代码,没有看到任何错误。 也许这是一个简单而普遍的问题,但我无法解决:/ 拜托,告诉我怎么了,我会非常感激。这是我的代码:

import math
import random

import pygame

# Intialaze the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((600, 800))

# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('battleship (1).png')
pygame.display.set_icon(icon)

score = 0

# Player

playerImg = pygame.image.load('battleship (2).png')

playerX = 275
playerY = 700
playerX_change = 0

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    enemyImg.append(pygame.image.load('invader.png'))
    enemyX.append(random.randint(1, 525))
    enemyY.append(random.randint(50, 150))
    enemyX_change.append(0.3)
    enemyY_change.append(115)

# Bullet
bulletImg = pygame.image.load('bullet (3).png')

bulletX = 0
bulletY = 700
bulletY_change = 0.87
bullet_state = "ready"


def enemy(x, y, i):
    screen.blit((enemyImg[i]), (int(x), int(y)))


def player(x, y):
    screen.blit((playerImg), (int(x), int(y)))


def fire_bullet(x, y):
    global bullet_state
    bullet_state = "fire"
    screen.blit((bulletImg), (int(x + 16.0), int(y - 18.5)))


def isCollision(enemyX, enemyY, bulletX, bulletY):
    distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
    if distance < 26.8:
        return True
    else:
        return False


# game loop
running = True
while running:

    # RGB - Red, Green, Blue
    screen.fill((255, 255, 255))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Player Movement
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                playerX_change = 0.3
            if event.key == pygame.K_LEFT:
                playerX_change = -0.3
            if event.key == pygame.K_SPACE:
                if bullet_state == "ready":
                    BulletX = playerX
                fire_bullet(playerX, bulletY)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                PlayerX_change = 0

    # Player boundaries
    playerX += playerX_change
    if playerX <= 0:
        playerX = 0
    elif playerX >= 536:
        playerX = 536

    # Enemy Movement
    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <= 0:
            enemyX_change[i] = 0.3
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 536:
            enemyX_change[i] = -0.3
            enemyY[i] += enemyY_change[i]
        # Collision
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 700
            bullet_state = "ready"
            score += 1
            print(score)
            enemyX[i] = random.randint(1, 525)
            enemyY[i] = random.randint(50, 150)

        enemy(enemyX[i], enemyY[i], i)

# Bullet Movement
if bulletY <= 0:
    bulletY = 700
    bullet_state = "ready"

if bullet_state == "fire":
    fire_bullet(bulletX, bulletY)
    bulletY -= bulletY_change

player(playerX, playerY)
pygame.display.update()

1 个答案:

答案 0 :(得分:0)

我认为您

screen.fill((255, 255, 255))
第78行的

用黑色填充屏幕。 尝试将其替换为:

pygame.display.update()