试图找出如何跟踪Pygame事件并组织游戏的功能 [英] Trying to figure out how to track Pygame events and organize the game's functions

查看:117
本文介绍了试图找出如何跟踪Pygame事件并组织游戏的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Pygame的新手,所以我仍然在努力与整个事件的概念。



基本上,我目前的挑战是: >


  1. 获取pygame.event.get()在主循环之外工作,以便我可以让播放器从一部分游戏到下一个(通过按空格键)


  2. 找出一种在主线程中组织游戏的不同功能的方法,以便他们不仅仅是一遍又一遍的循环,而是相互挤压。


我了解主循环是如何重要的在许多游戏中,但是当游戏涉及从一个事件移动到下一个事件(这是一个相对简单的基于文本的游戏,您可以通过不同的菜单并选择进度选择)时,我无法掌握如何使用它在这个游戏中。 。由于主循环是一个while循环,所以它内部的一切都一直在反复重复,所以我可以从一个屏幕到另一个屏幕,而屏幕无限制地相互冲突?



例如,我有一个介绍性序列(一个Intro()函数),应该在其他任何东西之前运行,然后允许你通过击中空格键继续进行实际的游戏。我把Intro()函数放在实际的主循环之前,以防止它循环。但是,pygame.event.get()在它内部不起作用(请记住,我已经在主循环中已经有了for循环的事件),而且我希望能够通过点击进入游戏本身空格键。



如果有人可以启发我并给我一个逻辑和线程的教训,那将是很棒的。



谢谢。

解决方案

坚持使用单个主循环,如果您不需要它们,请不要使用线程



你已经知道你的游戏包含不同的屏幕(让我们称之为场景,所以我们不将其与实际的屏幕混淆,我们绘制东西),所以下一个逻辑步骤是将它们从主循环中分离出来。




$假设你有以下场景:




  • 介绍

  • 主菜单

  • 游戏



那么你应该为每一个创建一个类,将会然后负责图纸/事件处理/本部分所需的任何内容。



一个简单的例子:

  import pygame 
pygame

类场景(对象):
屏幕=无

类简介(场景):
def __init __(self):
self.c =(32,32,100)

def draw(self):
Scene.screen.fill(self.c)

def update(self):
#由于场景是类,他们有一个状态,我们可以修改
r,g,b = self.c
r + = 1
g + = 1
b + = 2
如果r> 255:r = 0
如果g> 255:g = 0
如果b> 255:b = 0
self.c = r,g,b

def句柄(self,event):
#按空格键移动到菜单场景
如果event.type == pygame.KEYDOWN:
如果event.key == pygame.K_SPACE:
#从句柄返回一个场景或更新更改当前场景
return Menu()

class Menu(Scene):
def draw(self):
#draw menu
Scene.screen.fill((200,200,100))

def update(self):
pass
#do something

def handle(self,event):
#handle menu input
如果event.type == pygame.KEYDOWN:
如果event.key == pygame.K_a:
返回Game()
如果event.key == pygame.K_b:
return简介()

类游戏(场景):
pass#实现绘制更新句柄

Scene.screen = pygame.display.set_mode(( 800,600))

scene = I ntro()
while True:
如果pygame.event.get(pygame.QUIT):break
for py in pygame.event.get():
scene = scene。 handle(e)或场景
scene = scene.update()或场景
scene.draw()
pygame.display.flip()

这样,你有一个单独的主循环,但游戏的不同部分是由单独的类来管理的。在上面的简单示例中,每个场景都知道在某个事件上激活的其他场景,但这并不是必需的。您可以创建一些管理场景之间转换的 SceneHandler 。此外,您可能想要有某种游戏状态对象,您想要通过场景(例如,在游戏场景中显示游戏场景中的点数)。


I'm new to Pygame, so I'm still struggling with the whole "events" concept.

Basically, my current challenge is to:

  1. Get pygame.event.get() working outside of the main loop so that I can allow for the player to proceed from one part of the game to the next (by pressing spacebar, for instance).

  2. Figure out a way to organize the different functions of the game in the main thread so that they don't just keep looping over and over and overriding each other.

I understand how the main loop is crucial in many games, but I can't grasp how I could use it in this game when the game involves moving from one event to the next (it's a relatively simple text-based game where you go through different menus and select choices to progress). Since the main loop is a while loop, everything within it keeps repeating over and over, so how is it possible for me to go from one screen to the next without the screens infinitely clashing with each other?

For instance, I have an introductory sequence (an Intro() function) that is supposed to run first before anything else and then allow you to proceed to the actual game by hitting the spacebar. I have placed the Intro() function before the actual main loop to prevent it from looping. However, pygame.event.get() doesn't work inside it (bear in mind that I already have such an event "for" loop in the main loop), and I want to be able to progress to the game itself by hitting the spacebar.

It would be great if someone can enlighten me and give me a lesson in logic and threading.

Thanks.

解决方案

Stick with a single main loop, and don't use threads if you don't really need them.

You already figured out that your game consists of different screens (let's call them Scene so we don't confuse it with the actual screen where we draw stuff), so the next logical step is to seperate them out from the main loop.


Say you have the following scenes:

  • intro
  • main menu
  • game play

then you should create a class for each of those, which will then be responsible for drawing/event handling/whatever-needed-in-this-part.

A simple example:

import pygame
pygame.init()

class Scene(object):
    screen = None

class Intro(Scene):
    def __init__(self):
        self.c = (32, 32, 100)

    def draw(self):
        Scene.screen.fill(self.c)

    def update(self):
        # since scenes are classes, they have a state that we can modify
        r,g,b = self.c
        r += 1
        g += 1
        b += 2
        if r > 255: r = 0
        if g > 255: g = 0
        if b > 255: b = 0
        self.c = r, g, b

    def handle(self, event):
        # move to Menu-scene when space is pressed
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                # returning a scene from handle or update changes the current scene
                return Menu()

class Menu(Scene):
    def draw(self):
        # draw menu
        Scene.screen.fill((200, 200, 100))

    def update(self):
        pass
        # do something

    def handle(self, event):
        # handle menu input
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                return Game()
            if event.key == pygame.K_b:
                return Intro()

class Game(Scene):
    pass # implement draw update handle

Scene.screen = pygame.display.set_mode((800, 600))

scene = Intro()
while True:
    if pygame.event.get(pygame.QUIT): break
    for e in pygame.event.get(): 
        scene = scene.handle(e) or scene
    scene = scene.update() or scene
    scene.draw()
    pygame.display.flip()

This way, you have a single mainloop, but different parts of your game are managed by seperate classes. In the simple example above, each scene knows what other scene to activate on a certain event, but this is not necessary. You could create some kind of SceneHandler that manages the transitions between the scenes. Also, you may want to have some kind of game state object that you want to pass around scenes (e.g. show your points from the game scene in your game-over scene).

这篇关于试图找出如何跟踪Pygame事件并组织游戏的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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