为什么退出窗口按钮有效,但游戏中的退出按钮无效? [英] why is the exit window button work but the exit button in the game does not work?

查看:64
本文介绍了为什么退出窗口按钮有效,但游戏中的退出按钮无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的游戏制作一个开始菜单,但是当我点击我在开始菜单中制作的退出按钮时,它没有退出.我的代码有问题吗?

我尝试为退出创建一个函数,将其放入使用窗口退出按钮退出游戏的代码中,但没有任何效果.

导入pygame导入操作系统pygame.mixer.pre_init()pygame.mixer.init(44100, 16, 2, 262144)pygame.init()从 pygame.locals 导入*pygame.mixer.music.load(os.path.join(os.getcwd(), 'Sounds','intro.ogg'))pygame.mixer.music.set_volume(0.3)pygame.mixer.music.play(-1)每秒帧数 = 60白色 = (255,255,255)灰色 = (128,128,128)黑色 = (0,0,0)红色 = (255,0,0)橙色 = (255,128,0)黄色 = (255,255,0)绿色 = (0,255,0)Lgreen = (128,255,0)蓝色 = (0,0,255)Lblue = (0,255,255)紫色 = (255,0,255)粉红色 = (255,0,127)pygame.display.set_caption('Snake Universe')Title = pygame.image.load('Graphics/Title.png')Play = pygame.image.load('Graphics/Play.png')Option = pygame.image.load('Graphics/Option.png')Exit = pygame.image.load('Graphics/Exit.png')LinePX = pygame.image.load('Graphics/LinePX.png')LineO = pygame.image.load('Graphics/LineO.png')时钟 = pygame.time.Clock()电影 = pygame.movi​​e.Movie('视频/bg.mpg')屏幕 = pygame.display.set_mode((1280, 720))bgE = pygame.image.load('Graphics/transparent.png')电影屏幕 = pygame.Surface(movie.get_size()).convert()电影.set_display(电影屏幕)电影播放()y = 235y1 = 3000cnt = 0玩 = 真玩的时候:cnt+=1如果 cnt>=1870:cnt=0电影.倒带()电影播放()对于 pygame.event.get() 中的事件:如果 event.type == pygame.KEYDOWN:如果 event.key==pygame.K_RETURN:如果 y == 426:电影.停止()播放 = 错误pygame.quit()放弃()如果 event.key == pygame.K_UP:y += 1如果 y == 3236:y = 235y1 = 3000如果 y == 236:y = 425y1 = 3000如果 y == 426:y1 = 335y = 3235如果 event.key == pygame.K_DOWN:y += 1如果 y == 236:y = 3235y1 = 335如果 y == 3236:y1 = 3000y = 425如果 y == 426:y1 = 3000y = 235如果 event.type == pygame.QUIT:电影.停止()播放 = 错误pygame.quit()放弃()screen.blit(movie_screen,(0, 0))screen.blit(标题, (360, 0))screen.blit(播放, (460, 250))screen.blit(退出,(460, 450))screen.blit(LinePX, (482.5, y))screen.blit(LineO, (482.5, y1))screen.blit(选项, (460, 350))screen.blit(bgE, (-100, 0))pygame.display.update()时钟滴答(FPS)

我希望它退出窗口,但它什么也不做.

解决方案

只要 playingTrue,主循环就会运行.

<块引用>

playing = True玩的时候:# [...]

pygame.QUIT 事件被处理时,playing 被设置 False 主循环条件失败:

<块引用>

if event.type == pygame.QUIT:播放 = 错误# [...]

注意,pygame.quit() 不会终止循环,但会取消初始化所有 pygame 模块,如果在应用程序的中间完成,这将导致以下异常.

如果您想通过键盘退出应用程序,请输入密钥 pygame.K_KP_ENTER,当 pygame.KEYDOWN 事件被处理时,你必须做同样的事情:

if event.type == pygame.KEYDOWN:如果 event.key==pygame.K_KP_ENTER:播放 = 错误

或者您必须通过 pygame.event.post():

if event.type == pygame.KEYDOWN:如果 event.key==pygame.K_KP_ENTER:pygame.event.post(pygame.event.Event(pygame.QUIT))

i'm making a start menu for my game but when i hit the exit button i made in the start menu, it doesn't exit. Is there anything wrong with my code?

I tried making a function for the exit, put it in the code that exit the game with the window exit button, but nothing worked.

import pygame
import os
pygame.mixer.pre_init()
pygame.mixer.init(44100, 16, 2, 262144)
pygame.init()
from pygame.locals import*


pygame.mixer.music.load(os.path.join(os.getcwd(), 'Sounds', 
'intro.ogg'))
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)  

FPS = 60
white = (255,255,255)
grey = (128,128,128)
black = (0,0,0)
red = (255,0,0)
orange = (255,128,0)
yellow = (255,255,0)
green = (0,255,0)
Lgreen = (128,255,0)
blue = (0,0,255)
Lblue = (0,255,255)
purple = (255,0,255)
pink = (255,0,127)

pygame.display.set_caption('Snake Universe')
Title = pygame.image.load('Graphics/Title.png')
Play = pygame.image.load('Graphics/Play.png')
Option = pygame.image.load('Graphics/Option.png')
Exit = pygame.image.load('Graphics/Exit.png')
LinePX = pygame.image.load('Graphics/LinePX.png')
LineO = pygame.image.load('Graphics/LineO.png')
clock = pygame.time.Clock()
movie = pygame.movie.Movie('Video/bg.mpg')
screen = pygame.display.set_mode((1280, 720))
bgE = pygame.image.load('Graphics/transparent.png')
movie_screen = pygame.Surface(movie.get_size()).convert()


movie.set_display(movie_screen)
movie.play()


y = 235
y1 = 3000



cnt = 0
playing = True
while playing:
    cnt+=1
    if cnt>=1870:
        cnt=0
        movie.rewind()
        movie.play()
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key==pygame.K_RETURN:
                if y == 426:
                    movie.stop()
                    playing = False
                    pygame.quit()
                    quit()      
            if event.key == pygame.K_UP:
                y += 1
                if y == 3236:
                    y = 235
                    y1 = 3000
                if y == 236:
                    y = 425
                    y1 = 3000
                if y == 426:
                    y1 =335
                    y = 3235

            if event.key == pygame.K_DOWN:
                y += 1
                if y == 236:
                    y = 3235
                    y1 = 335
                if y == 3236:
                    y1 = 3000
                    y = 425
                if y == 426:
                    y1 = 3000
                    y = 235

            if event.type == pygame.QUIT:
                movie.stop()
                playing = False
                pygame.quit()
                quit()

   screen.blit(movie_screen,(0, 0))
   screen.blit(Title, (360, 0))
   screen.blit(Play, (460, 250))
   screen.blit(Exit, (460, 450))
   screen.blit(LinePX, (482.5, y))
   screen.blit(LineO, (482.5, y1))
   screen.blit(Option, (460, 350))
   screen.blit(bgE, (-100, 0))
   pygame.display.update()
   clock.tick(FPS)

i expected it to exit the window but instead it doesn't do anything.

解决方案

The main loop runs as long as playing is True.

playing = True
while playing:
    # [...]

When the pygame.QUIT event is handled, playing is set False the main loop condition is fails:

if event.type == pygame.QUIT:
    playing = False
    # [...]

Note, pygame.quit() doesn't terminate the loop, but it uninitialize all pygame modules, which will cause an exception in the following, if it is done in the middle of the application.

If you want to quit the application by the keypad enter key pygame.K_KP_ENTER, the you've to do the same when the pygame.KEYDOWN event is handled:

if event.type == pygame.KEYDOWN:
    if event.key==pygame.K_KP_ENTER:
        playing = False

Or you've to send a pygame.QUIT event by pygame.event.post():

if event.type == pygame.KEYDOWN:
    if event.key==pygame.K_KP_ENTER:
        pygame.event.post(pygame.event.Event(pygame.QUIT))

这篇关于为什么退出窗口按钮有效,但游戏中的退出按钮无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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