首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame key.set_repeat()不工作

Pygame key.set_repeat()不工作
EN

Stack Overflow用户
提问于 2014-05-04 20:12:35
回答 1查看 1.8K关注 0票数 0

我正在尝试编写一个简单的地图编辑器,我在pygame.set_repeat()中遇到了一个问题,我在while循环之前设置了它,但是在while循环中也尝试过它,它似乎没有做任何事情。

我检查过其他线程,但没有发现任何有帮助的东西。有人能提供任何意见吗?

代码语言:javascript
复制
import pygame
import math
import sys
import os
import os.path

from pygame.locals import *
from classes.tile_sprites import sprites
from classes.tile_sprites import buttons

class MainCode:

    def main(self):
        size = width, height = 800, 600
        display = pygame.display.set_mode(size)
        fps_clock = pygame.time.Clock()

        button_location = "../resources/images/buttons"
        icon_location = "../resources/images/icons"

        surface_type = {"track":os.path.join(icon_location, "track.png"),
                         "dirt":os.path.join(icon_location, "dirt.png"),
                         "grass":os.path.join(icon_location, "grass")}

        incline_type = {"steep_incline":os.path.join(icon_location, "steep_incline.png"),
                        "moderate_incline":os.path.join(icon_location,"moderate_incline.png"),
                        "flat":os.path.join(icon_location, "flat.png"),
                        "moderate_decline":os.path.join(icon_location, "moderate_decline.png"),
                        "steep_decline":os.path.join(icon_location, "steep_decline.png")}

        surface_selections = ["track", "dirt", "grass"]
        incline_selections = ["steep_incline", "moderate_incline", "flat", "moderate_decline", "steed_decline"]

        tile_group = pygame.sprite.Group()
        button_group = pygame.sprite.Group()
        selected_group = pygame.sprite.GroupSingle()

        sbl = buttons((25, 25), (0, 255, 0), "left")
        sbl.rect.x, sbl.rect.y = 0, 0
        sbr = buttons((25, 25), (0, 255, 0), "right")
        sbr.rect.x, sbl.rect.y = 100, 0
        ibl = buttons((25, 25), (0, 255, 0), "left")
        ibl.rect.x, ibl.rect.y = 0, 100
        ibr = buttons((25, 25), (0, 255, 0), "right")
        ibr.rect.x, ibr.rect.y = 100, 100
        to_add = [sbl, sbr, ibl, ibr]
        button_group = self.pygame_group_add_many(to_add, button_group)

        pygame.key.set_repeat(1, 1000000000)

        while 1:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
            mouse = pygame.mouse.get_pos()
            key = pygame.key.get_pressed()
            if key[K_DOWN]:
                print "down"

            display.fill((0, 0, 0))
            tile_group.draw(display)
            button_group.draw(display)
            pygame.display.flip()
            fps_clock.tick(30)

    def pygame_group_add_many(self, items_to_add, group):
        for each in items_to_add:
            group.add(each)
        return group

if __name__ == "__main__":
    MainCode().main()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-05 02:33:10

get_pressed()if key[K_DOWN]:意味着您保持按下K_DOWN,而不是“解除”它。get_pressed()set_repeat()无关。

set_repeat()处理事件。

当您保持按下K_DOWN时,此代码只打印一次“事件向下”。

代码语言:javascript
复制
if event.type == KEYDOWN:
    if event.key == K_DOWN:
        print "event down"

如果您添加set_repeat(1,1000),当您保持按下K_DOWN时,它将每1000毫秒打印一次“事件向下”。

完整的例子:

代码语言:javascript
复制
import pygame
from pygame.locals import *

pygame.init()

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

pygame.key.set_repeat(1,1000) # add/remove this line

running = True        
while running:

    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            elif event.key == K_DOWN:
                print "EVENT DOWN"

pygame.quit()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23461077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档