在pygame中双击键盘,而不是双击 [英] Double-click the keyboard in pygame, not double-click

查看:116
本文介绍了在pygame中双击键盘,而不是双击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用pygame做一个双击功能,检测键盘右箭头.比如双击右箭头慢跑变快跑,不过我是新手.不知道这个函数怎么写.找了很多文档,应该怎么写.

I want to use pygame to make a double-click function that detects the right arrow of the keyboard. For example, double-clicking the right arrow jogging becomes fast running, but I am a novice. I do n’t know how to write this function. I have found a lot of documentation and how should I write it.

推荐答案

我在上次无聊的会议上编写了这个示例.代码中的注释解释了发生了什么.

I hacked together this example during my last boring meeting. The comments in the code explain what's going on.

import pygame

class Actor(pygame.sprite.Sprite):
    def __init__(self, image, pos):
        super().__init__()
        self.image = image
        self.pos = pygame.Vector2(pos)
        self.rect = self.image.get_rect(center=self.pos)

    def update(self, events, dt):
        pass

# just for the cheap motion effect....
class Shadow(Actor):
    def __init__(self, image, pos):
        # new surface to allow surface level alpha value
        # with per-pixel alpha surface
        tmp = pygame.Surface(image.get_rect().size)
        tmp.set_colorkey((1,2,3))
        tmp.fill((1,2,3))
        tmp.blit(image, (0,0))
        super().__init__(tmp, pos)
        self.time = 0
        self.alpha = 255
        self.image.set_alpha(self.alpha)

    def update(self, events, dt):
        self.time += dt
        if self.time > 50:
            self.time = 0
            self.alpha -= 50
            if self.alpha < 50:
                self.kill()
            else:
                self.image.set_alpha(self.alpha)

class Player(Actor):
    def __init__(self, image, pos):
        super().__init__(image, pos)

        # since we want to know if there's a double key press
        # we need to keep track of the last button pressed
        self.last_move_button = None

        # a flag that indicates that we're running
        self.running = False

        self.run_counter = 0

    def update(self, events, dt):

        # this is the part that checks for the double key press
        for e in events:
            if e.type == pygame.KEYDOWN:
                ticks = pygame.time.get_ticks()

                #we check if we pressed the same key in the last 500ms before
                self.running = self.last_move_button and self.last_move_button[0] == e.key and ticks - self.last_move_button[1] < 500

                # keep track of the last button pressed and the time of the key press
                self.last_move_button = (e.key, ticks)

        # this is the "regular" movement code
        pressed = pygame.key.get_pressed()
        move = pygame.Vector2((0, 0))
        if pressed[pygame.K_w]: move += (0, -1)
        if pressed[pygame.K_a]: move += (-1, 0)
        if pressed[pygame.K_s]: move += (0, 1)
        if pressed[pygame.K_d]: move += (1, 0)
        if move.length() > 0: 
            move.normalize_ip()
        else:
            # if we're not moving we're not running
            self.running = False

        # if the running flag is set, we move at double speed
        speed = 2 if self.running else 1

        self.pos += move * (dt/5) * speed
        self.rect.center = self.pos

        # just for the cheap motion effect....
        self.run_counter = (self.run_counter + dt) if self.running else 0
        if self.running and self.run_counter > 25:
            self.run_counter = 0
            self.groups()[0].add(Shadow(self.image, self.rect.center))


def main():
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    clock = pygame.time.Clock()
    dt = 0

    sprites = pygame.sprite.Group(Player(pygame.image.load('guy.png').convert_alpha(), (100, 200)))

    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        sprites.update(events, dt)
        screen.fill((30, 30, 30))
        sprites.draw(screen)

        pygame.display.update()
        dt = clock.tick(60)

if __name__ == '__main__':
    main()

这是我使用的图像:

这是它的样子:

这篇关于在pygame中双击键盘,而不是双击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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