这是我现在实现简单的 pygames 的方式:

import pygame, sys
from pygame.locals import *

def run_game():
    pygame.init()

    SIZE = (640, 400)
    BG_COLOUR = (0, 0, 0)
    LINE_COLOUR = (255, 255, 255)

    screen = pygame.display.set_mode(SIZE)
    clock = pygame.time.Clock()

    while True:
        time_passed = clock.tick(30)
        for event in pygame.event.get():
                if event.type == QUIT:
                        exit_game()

        screen.fill(BG_COLOUR)
        pygame.draw.aaline(screen, LINE_COLOUR, (1, 1), (639, 399))
        pygame.display.flip()

def exit_game():
    sys.exit()

if __name__ == "__main__"
    run_game()

我还看过一个 keeprunning 标志被用来退出主事件循环,以及使用 pygame.event.poll() 而不是循环遍历 pygame.event.get(). 。有什么建议,例如变量的大小写/命名,有什么建议可以使其更有效或更具可读性吗?

有帮助吗?

解决方案

无论 pygame 作者推荐什么,我建议避免 from ... import * :它只会让你的代码更难阅读(因为读者必须检查每个和每个裸名检查它是否实际在本地分配或来自deuced * )。更改为导入pygame.locals为pygl ,比如说,并且每次使用它的名称都是合格的(我认为在你的代码中这只是意味着改变 QUIT pygl.QUIT )。您选择使用什么简称来代替 pygl 并不重要,但这是我强烈推荐的一般结构。记住:命名空间是一个很棒的主意 - 让我们做更多的事情! - )

在任何地方使用4个空格的缩进,正如PEP 8所说:你似乎在混合一些4空格的缩进与其他8个空格或制表符 -

不要分配你永远不会使用的变量,就像你为 time_passed 做的那样。

代码如下:                 if event.type == QUIT:                         exit_game() 只要您测试一种或几种可能性(使用 if / elif ),但没有“向上扩展”,这是很好的。很多,无论是可读性还是效率。当您需要涵盖几种情况时,请使用可在循环之前设置的字典:

dispatch = {pygl.QUIT: exit_game, # whatever else
           }

而不是 if / elif / else ,请使用:

f = dispatch.get(event.type)
if f is None:  # the "else" case"
   ...
else: f()

其他提示

你的例子很好。但是如果你看到“pygame.event.poll()”是如何工作的,请看这个简单的例子:

event = pygame.event.poll()
    while "ok":
        event = pygame.event.wait()
        cursor = pygame.mouse.get_pos()
        if event.type == QUIT:  raise SystemExit
            if event.type == MOUSEBUTTONUP and pygame.mouse.get_pressed()[0]:
               print "up"
        if event.type == MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0]:
               print "down"
        if event.type == KEYDOWN and event.key == K_SPACE: print "space key"
        if event.type == KEYDOWN and event.key == K_BACKSPACE:
               print "backspace key"

您可以使用 pygame 中的所有事件:

  • pygame.event.get-从队列获取事件从队列获得事件
  • pygame.event.poll-从队列中获取单个事件,从队列中获取一个事件
  • pygame.event.Wait-等待队列中的单个事件

或者

  • pygame.event.clear([[Pygame.Quit,pygame.activeEvent,pygame.Keydown,pygame.keyup,pygame.meyup,pygame.mousemotion,pygame.joybuttonup,pygame.useere.useervent

基本上,您可以为此事件创建函数并使用它。像这样 :

def click_down():
    #print "Event queue:", pygame.event.peek()
    return pygame.event.peek(pygame.MOUSEBUTTONDOWN)

def is_click():
    if not click_down():
        return None

    clicks = pygame.event.get(pygame.MOUSEBUTTONDOWN)
    pygame.event.get(pygame.MOUSEBUTTONUP)
    return clicks[0].pos
def quiting():
    return pygame.event.peek(pygame.QUIT)
def keyboard():
    pygame.event.pump()
    pygame.event.get(pygame.KEYDOWN)

或阻止某些事件,例如:

pygame.event.set_blocked((pygame.JOYAXISMOTION))

pygame.Color()类很好用:

from pygame import Color

# example: text is 20% gray. gray100 = (255,255,255)
text_fg = Color('gray20')
text_bg = Color('gray80')

# many names are defined
Color("red")

# rgb, rgba
Color(100, 100, 100)
Color(100, 100, 100, 20)

#hex
Color("fefefe")
Color("fefefe00")

这看起来基本上是我见过的所有其他例子。也许你可能想找一些用pygame编写的开源游戏。

我不确定这是否是您正在寻找的那种答案,但之后 点击 pygame.draw.aaline(screen,LINE_COLOUR,(1,1),(639,399)) 点击 你应该把 点击 pygame.display.flip() 点击 这样,玩家实际看到的显示每帧都会刷新。如果您运行此代码,那么您将看到的只是一个黑屏,没有线,因为显示器永远不会更新。

非常简单地从经验测试中你似乎有这条线:

pygame.quit()

在sys.exit()

之前丢失
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top