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
任何帮助都会很棒,谢谢。
答案 0 :(得分:0)
看起来你打电话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)
...
答案 1 :(得分:0)
方法.convert()需要有关显示/窗口颜色深度(8位,16位,24位或32位)的信息
首先,您必须使用set_display()
创建此显示/窗口编辑:完整代码 -
在执行set_mode()
甚至background = pygame.image.load(...).convert()
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()