pygame 错误,未设置视频模式 [英] pygame error, No video mode has been set

查看:115
本文介绍了pygame 错误,未设置视频模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是为我的游戏设置了一些功能,但我的脚本无法加载图像

I am just setting up some functions for my game but my script fails to load the image

#used variables
# x, y for alien location
# nPc for the aliens image
#
#
#
#
#
#
#
#
#
#



#set up
import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()


nPc = '/home/claude/Dropbox/Bowtie/Prisim/Images/Alien_Races/Standered/alien_1.png'


nPc = pygame.image.load(nPc).convert_alpha()

def loc_alien():
        x = random.randint(0, 400)
        y = randaom.randint(0, 400)


def spawn_alien(x, y):
        screen.blit(nPc, (x, y))

当我运行它时,我不会发生任何事情,因为我还没有使用这些函数,但是当我运行它时,我得到了这个错误

when I run this I wont nothing to happen as I am not using the functions yet, but when I do run it I get this error

Traceback (most recent call last):
  File "/home/claude/Dropbox/Bowtie/Prisim/Scripts/aliens.py", line 26, in <module>
    nPc = pygame.image.load(nPc).convert_alpha()
error: No video mode has been set

有人知道我做错了什么吗?

anyone know what I'm doing wrong?

推荐答案

我相信你需要致电:

screen = pygame.display.set_mode((800, 600)) # change to the real resolution

这个调用实际上会返回你想要 blit 的表面.以下是链接资源中的文档.

this call will actually return the surface that you want to blit on. Below is the documentation from the linked resource.

pygame.display.set_mode()

初始化一个窗口或屏幕以供显示

set_mode(resolution=(0,0), flags=0, depth=0) -> Surface

此函数将创建一个显示表面.传入的参数是对显示类型的请求.实际创建的显示将是系统支持的最佳匹配.

This function will create a display Surface. The arguments passed in are requests for a display type. The actual created display will be the best possible match supported by the system.

分辨率参数是一对表示宽度的数字和高度.flags 参数是附加选项的集合.depth 参数表示用于颜色的位数.

The resolution argument is a pair of numbers representing the width and height. The flags argument is a collection of additional options. The depth argument represents the number of bits to use for color.

返回的 Surface 可以像普通 Surface 一样绘制但最终会在显示器上看到变化.

The Surface that gets returned can be drawn to like a regular Surface but changes will eventually be seen on the monitor.

如果没有通过分辨率或者设置为(0, 0)并且pygame使用SDL1.2.10 或以上版本,创建的 Surface 将具有相同的大小作为当前的屏幕分辨率.如果只设置了宽度或高度为 0,Surface 将具有与屏幕相同的宽度或高度解析度.使用 1.2.10 之前的 SDL 版本将引发例外.

If no resolution is passed or is set to (0, 0) and pygame uses SDL version 1.2.10 or above, the created Surface will have the same size as the current screen resolution. If only the width or height are set to 0, the Surface will have the same width or height as the screen resolution. Using a SDL version prior to 1.2.10 will raise an exception.

通常最好不要传递深度参数.它将默认为系统的最佳和最快的颜色深度.如果您的游戏需要您可以使用此参数控制深度的特定颜色格式.Pygame 会模拟一个不可用的颜色深度,这可能会很慢.

It is usually best to not pass the depth argument. It will default to the best and fastest color depth for the system. If your game requires a specific color format you can control the depth with this argument. Pygame will emulate an unavailable color depth which can be slow.

请求全屏显示模式时,有时完全匹配无法做出所要求的决议.在这些情况下 pygame将选择最接近的兼容匹配.返回的表面将仍然始终匹配请求的分辨率.

When requesting fullscreen display modes, sometimes an exact match for the requested resolution cannot be made. In these situations pygame will select the closest compatible match. The returned surface will still always match the requested resolution.

flags 参数控制您想要的显示类型.有有几种可供选择,您甚至可以使用组合多种类型按位或运算符,(管道|"字符).如果您通过 0 或没有flags 参数将默认为软件驱动的窗口.这里有您要从中选择的显示标志:

The flags argument controls which type of display you want. There are several to choose from, and you can even combine multiple types using the bitwise or operator, (the pipe "|" character). If you pass 0 or no flags argument it will default to a software driven window. Here are the display flags you will want to choose from:

pygame.FULLSCREEN    create a fullscreen display
pygame.DOUBLEBUF     recommended for HWSURFACE or OPENGL
pygame.HWSURFACE     hardware accelerated, only in FULLSCREEN
pygame.OPENGL        create an OpenGL renderable display
pygame.RESIZABLE     display window should be sizeable
pygame.NOFRAME       display window will have no border or controls

例如:

# Open a window on the screen
screen_width=700
screen_height=400
screen=pygame.display.set_mode([screen_width,screen_height])

这篇关于pygame 错误,未设置视频模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆