Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame中双击键盘,而不是双击_Python_Pygame_Double Click - Fatal编程技术网

Python 在pygame中双击键盘,而不是双击

Python 在pygame中双击键盘,而不是双击,python,pygame,double-click,Python,Pygame,Double Click,我想使用pygame制作一个双击功能,用于检测键盘的右箭头。例如,双击右箭头慢跑变为快速跑步,但我是新手。我不知道如何写这个函数。我已经找到了很多文档,我应该如何编写它。我在上次无聊的会议上拼凑了这个例子。 代码中的注释解释了发生了什么 import pygame class Actor(pygame.sprite.Sprite): def __init__(self, image, pos): super().__init__() self.image

我想使用pygame制作一个双击功能,用于检测键盘的右箭头。例如,双击右箭头慢跑变为快速跑步,但我是新手。我不知道如何写这个函数。我已经找到了很多文档,我应该如何编写它。

我在上次无聊的会议上拼凑了这个例子。 代码中的注释解释了发生了什么

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
班级演员(pygame.sprite.sprite):
定义初始化(自我、图像、位置):
super()。\uuuu init\uuuuu()
self.image=image
self.pos=pygame.Vector2(pos)
self.rect=self.image.get_rect(中心=self.pos)
def更新(自身、事件、dt):
通过
#只是为了廉价的运动效果。。。。
班影(演员):
定义初始化(自我、图像、位置):
#新曲面以允许曲面级别alpha值
#具有每像素alpha曲面
tmp=pygame.Surface(image.get_rect().size)
tmp.设置颜色键((1,2,3))
tmp.填料((1,2,3))
tmp.blit(图像,(0,0))
超级()。\uuuuu初始化(tmp,pos)
self.time=0
self.alpha=255
self.image.set_alpha(self.alpha)
def更新(自身、事件、dt):
自身时间+=dt
如果self.time>50:
self.time=0
self.alpha-=50
如果self.alpha<50:
self.kill()
其他:
self.image.set_alpha(self.alpha)
班级运动员(演员):
定义初始化(自我、图像、位置):
super()。\uuuu init\uuuu(图像,位置)
#因为我们想知道是否有双按键
#我们需要跟踪最后一次按下的按钮
self.last\u move\u按钮=无
#指示我们正在运行的标志
self.running=False
self.run\u计数器=0
def更新(自身、事件、dt):
#这是检查双按键的部分
对于事件中的e:
如果e.type==pygame.KEYDOWN:
ticks=pygame.time.get_ticks()
#我们检查是否在之前的最后500毫秒内按了相同的键
self.running=self.last\u move\u按钮和self.last\u move\u按钮[0]==e.key和ticks-self.last\u move\u按钮[1]<500
#记录上次按下的按钮和按键时间
self.last_move_按钮=(e.key,滴答声)
#这是“常规”运动代码
pressed=pygame.key.get_pressed()
move=pygame.Vector2((0,0))
如果按下[pygame.K_w]:移动+=(0,-1)
如果按下[pygame.K_a]:移动+=(-1,0)
如果按下[pygame.K_s]:移动+=(0,1)
如果按下[pygame.K_d]:移动+=(1,0)
如果move.length()大于0:
move.normalize_ip()
其他:
#如果我们不走,我们就不会跑
self.running=False
#如果设置了running标志,我们将以双倍速度移动
如果自运行,则速度=2,否则为1
self.pos+=移动*(dt/5)*速度
self.rect.center=self.pos
#只是为了廉价的运动效果。。。。
self.run\u计数器=(self.run\u计数器+dt)如果self.running否则为0
如果自运行和自运行计数器>25:
self.run\u计数器=0
self.groups()[0]。添加(阴影(self.image、self.rect.center))
def main():
pygame.init()
screen=pygame.display.set_模式((500500))
clock=pygame.time.clock()
dt=0
sprites=pygame.sprite.Group(Player(pygame.image.load('guy.png')。convert_alpha(),(100200)))
尽管如此:
events=pygame.event.get()
对于事件中的e:
如果e.type==pygame.QUIT:
返回
sprites.update(事件,dt)
屏幕填充((30,30,30))
精灵。绘制(屏幕)
pygame.display.update()
dt=时钟滴答声(60)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
以下是我使用的图像:

下面是它的样子: