用pygame打开一个新窗口 [英] Open a new window with pygame

查看:146
本文介绍了用pygame打开一个新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个街机风格的游戏,如果玩家精灵与敌人发生碰撞,它应该关闭游戏屏幕并执行 Game over screen.py 文件,顾名思义,该文件是 Game Over Screen 的脚本.我曾经尝试执行此操作的代码如下:

I am making an arcade style game where if a player sprite collides with an enemy it should close the game screen and execute the Game over screen.py file which is the script for the Game Over Screen as the name suggests. My code which I used to try and do this is as follows:

def sprite_collide():
    global p_rect
    global e_rect
    if p_rect.colliderect(e_rect):
        execfile('Game Over Screen.py')

我刚刚开始制作过屏游戏,所以代码如下:

I have only just started making the game over screen so the code is as follows:

import pygame, sys, time, random
from pygame.locals import *

pygame.init() #initializes pygame window
pygame.display.set_caption('KeyCast') #titlebar caption

GOSURF=pygame.display.set_mode((900,600),0,32) #sets main surface

gobackground = pygame.image.load('Game Over.png') #background image for game

"""--------------------------------------------------------------------------"""
while True:

    def quitgame():
        """exits programme without any errors"""
        for event in pygame.event.get(): #quitting process
            if event.type==QUIT: #if player selects 'exit button' on window
                pygame.quit() #pygame quit
                sys.exit() #system quit
    quitgame()

    def Surface():
        GOSURF.blit(gobackground,(0,0)) #background image

    Surface()

    pygame.display.update()

然而,每当精灵与敌人碰撞时,我都会收到错误消息NameError:全局名称‘GOSURF’未定义"".不知道在这里做什么.

However, whenever the sprite collides with the enemy I get the error message "NameError: global name 'GOSURF' is not defined""". Not sure what to do here.

注意:我在游戏结束屏幕中为 pygame 表面使用了一个不同的名称,即 GOSURF,而游戏脚本中的原始表面只是 SURF.

Note: I have used a different name for the pygame surface in the game over screen which is GOSURF, whereas the original surface in the game script is just SURF.

推荐答案

如果您的意思是同时制作多个窗口.你不能那样做.这是 SDL(底层 C 库)的限制.

If you mean making multiple windows at the same time. You can't do that. It's a limitation of SDL (the underlying C library).

在您的情况下,您必须将 GOSURF 声明为 global 才能从您的函数中访问它.

In your case you have to declare GOSURF global to reach it from your function.

def Surface():
    global GOSURF
    GOSURF.blit(gobackground,(0,0)) #background image

这篇关于用pygame打开一个新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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