python - pygame错误,没有设置视频模式

标签 python pygame

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

#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))

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

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

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

最佳答案

我认为您需要调用:

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

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

pygame.display.set_mode()

Initialize a window or screen for display

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.

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.

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

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.

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.

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.

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

For example:

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

关于python - pygame错误,没有设置视频模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905989/

相关文章:

python - 安装 Python 脚本,维护对 Python 2.6 的引用

python - 我们如何在 python 中解析包含带有 xml 命名空间标签的节点的 xml 数据?

c++ - ctypes python std::字符串

python-3.x - 虽然true循环给我递归错误

python - 内置容器的自定义比较

python - 递归生成器

python - 在pygame中将矩形转换为曲面

python - 如何减慢pygame中的动画速度?

python - Pygame - 创建 "Enemy"类,然后将其导入游戏

python - 模块无法加载,仅标题发生变化(pygame)