Python 显示精灵

Python Display Sprites

在学校的计算中,我们必须做一个项目,地雷是制作一个 Space Invaders 类型的游戏。我仍然是 python 的新手,并且不断收到这两个错误!我不知道如何修复它们,任何帮助将不胜感激。

C:\Python27\python.exe "C:/Users/Iain/Desktop/Computing Project/Galaxian.py"
Traceback (most recent call last):
  File "C:/Users/Iain/Desktop/Computing Project/Galaxian.py", line 32, in <module>
    player = Player()
  File "C:/Users/Iain/Desktop/Computing Project/Galaxian.py", line 23, in __init__
    self.image = pygame.image.load('images\user1.gif').convert()
pygame.error: No video mode has been set

Process finished with exit code 1

这是我的代码(我知道它很乱,我很抱歉:p)

import random
import pygame

# Colours

White = (255, 255, 255)
Black = (0, 0, 0)
Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)
Orange = (255, 255, 0)
Yellow = (0, 255, 255)

# Initialize the game and display
pygame.init()
pygame.mixer.init()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        # type: () -> object

        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('images\user1.gif').convert()
        self.rect = self.image.get_rect()
        self.speedx = 0

        def update(self):
            self.rectx += self.speedx

all_sprites = pygame.sprite.Group()

player = Player()

all_sprites.add(player)


pygame.mixer.music.load("C:\Users\Iain\Desktop\Computing Project\Audio\music\soundtrack.mp3")
pygame.mixer.music.play(-1)

display_width = 1280
display_height = 720

galaxianDisplay = pygame.display.set_mode((display_width, display_heightth))

clock = pygame.time.Clock()
Background = pygame.image.load("C:\Users\Iain\Desktop\Computing Project\images\Background.jpg").convert()
Background = pygame.transform.scale(Background, (1280, 720))

galaxianDisplay.blit(Background, [0, 0])
x = display_width / 2
y = display_height / 2

# Loop

crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        print(event)
    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit

如有帮助将非常感谢。

看来你打电话给 pygame.display.set_mode 太晚了。在创建 Player() 的新实例之前,您需要移动这些行,因为您定义的构造函数正在尝试在设置显示模式之前加载图像。

display_width = 1280
display_height = 720

galaxianDisplay = pygame.display.set_mode((display_width, display_height))

换句话说,您的代码应该如下所示

...

all_sprites = pygame.sprite.Group()

display_width = 1280
display_height = 720

galaxianDisplay = pygame.display.set_mode((display_width, display_height))   

player = Player()

all_sprites.add(player)

pygame.mixer.music.load("C:\Users\Iain\Desktop\Computing Project\Audio\music\soundtrack.mp3")
pygame.mixer.music.play(-1)

...

方法 .convert() 需要有关 display/window 颜色深度(8 位、16 位、24 位或 32 位)的信息

所以首先你必须使用 set_display()

创建这个 display/window

编辑: 完整代码。

它在执行background = pygame.image.load(...).convert()之前执行set_mode()甚至self.image = pygame.image.load(...).convert()

import pygame
import random

# --- constants --- (UPPER_CASE names)

WHITE  = (255, 255, 255)
BLACK  = (  0,   0,   0)
RED    = (255,   0,   0)
GREEN  = (  0, 255,   0)
BLUE   = (  0,   0, 255)
ORANGE = (255, 255,   0)
YELLOW = (  0, 255, 255)

DISPLAY_WIDTH = 1280
DISPLAY_HEIGHT = 720

FPS = 60

# --- classes --- (CamelCase names)

class Player(pygame.sprite.Sprite):
                                     # <-- empty line for readabelity
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load("images\user1.gif").convert()
        self.rect = self.image.get_rect()
        self.speed_x = 0

    def update(self):
        self.rect.x += self.speed_x

# --- functions --- (lower_case names)

# empty

# --- main --- (lower_case names)

# - init -

pygame.init()
pygame.mixer.init()

display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
display_rect = display.get_rect()

# - objects -

all_sprites = pygame.sprite.Group()

player = Player()
player.rect.center = display_rect.center
player.speed_x = 1

all_sprites.add(player)

background = pygame.image.load("C:\Users\Iain\Desktop\Computing Project\images\Background.jpg").convert()
background = pygame.transform.scale(background, (DISPLAY_WIDTH, DISPLAY_HEIGHT))

# - other -

pygame.mixer.music.load("C:\Users\Iain\Desktop\Computing Project\Audio\music\soundtrack.mp3")
pygame.mixer.music.play(-1)

# - mainloop -

crashed = False
clock = pygame.time.Clock()

while not crashed:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        print(event)

    # - updates (without draws) -

    all_sprites.update()

    # - draws (without updates) -

    display.blit(background, (0, 0))

    all_sprites.draw(display)

    pygame.display.update()

    # - FPS -

    clock.tick(FPS)

# - end -
pygame.quit()