简体   繁体   English

使用Midi事件使玩家在pygame中移动的方法

[英]ways of using midi events to make a player move in pygame

I have made some little games with the pygame module and it had been fun. 我用pygame模块做了一些小游戏,这很有趣。 However, right now i´m trying to move the character with a piano via midi using pygame.midi. 但是,现在我正在尝试使用pygame.midi通过midi用钢琴移动角色。 there´s the problem. 有问题。 I did it but not in the way i wanted to because the character moves sometimes and other it doesn´t. 我这样做了,但并没有按照我想要的方式进行,因为角色有时会移动,而其他时候则不会。 I think i know where is the problem: If i understand correctly, the pygame.event.get() function "catch" the events always, it does not matter when (inside the main loop of the game), so i think that with my code this is not happening, i mean, i believe that the player not always do what is supposed to do because the midi input has to be in a specific "time" not as pygame.event.get(). 我想我知道问题出在哪里:如果我理解正确,那么pygame.event.get()函数总是“捕获”事件,什么时候(在游戏主循环内)都没有关系,所以我认为我的代码没有发生,我的意思是,我相信玩家并非总是按照预期的方式做,因为Midi输入必须在特定的“时间”内而不是pygame.event.get()。 I looked into the pygame documentation and there seems to be a function that does the trick: pygame.midi.midis2events(), but i don´t know how to use it.The question is just: How can i move the character through the piano via midi with pygame in order that the character moves everytime? 我查看了pygame文档,似乎有一个功能可以解决问题:pygame.midi.midis2events(),但我不知道如何使用它。问题是:我如何在角色中移动角色?通过pygame通过midi钢琴,以使角色每次移动?

import pygame as pg, pygame.midi 

WIDTH = 800
HEIGHT = 600
FPS = 60

BLACK = (0,0,0)
BLUE = (0,0,255)


pg.init()
pg.midi.init()
screen = pg.display.set_mode((WIDTH,HEIGHT))
pg.display.set_caption('STACKOVERFLOW_EXAMPLE_MIDI')
clock = pg.time.Clock()

running = True

inp = pg.midi.Input(1)
x = WIDTH//2
y = HEIGHT//2
speedx = 0

midi_list = []

while running:
    clock.tick(FPS)
    if inp.poll():
        midi_value = inp.read(1000)[0][0][1]
        if midi_value==57:
            midi_list.append(midi_value)
            if len(midi_list)==1:
                speedx = -1
            else:
                speedx = 0
                midi_list = []
        if midi_value == 63:
            running = False
    x = x+speedx
    screen.fill(BLUE)
    pg.draw.rect(screen,BLACK,[x,y,50,60])
    pg.display.update()
    pg.display.flip()

pg.quit()

I need that the player moves to the left (or to the right) when the piano´s key A3 (a specific note) is being pressed and that the player stops moving when the key has been released. 我需要在按下钢琴的琴键A3(特定音符)时播放器向左(或向右)移动,并且释放琴键时播放器停止移动。 In this particular fragment of the code, but also to do things like press a piano´s key an shoot one bullet regardless of whether is being pressed or not, in short, to do the same things i can do with the keyboard events. 在这段代码的特定片段中,还要做一些事情,例如按下钢琴的琴键,发射一颗子弹,而不管是否被按下,简而言之,就是要做与键盘事件相同的事情。

ok! 好! i found and answer that works well for the time being: 我发现并回答暂时有效:

if inp.poll():
    s(0.1)
    midi_values = inp.read(1000)

    for midi in midi_values:
        midi_value = midi[0][1]
        #Movement to the left
        if midi_value==57:
            midi_list.append(midi_value)
            if len(midi_list)==1:
                speedx = -50

Modifying the code in order to iterate the list midi_values gives me the correct input-output response without the inestability which i experienced. 修改代码以迭代列表midi_values可以为我提供正确的输入输出响应,而不会产生我经历的不稳定性。 Also i did introduce a sleep(0.1) delay and works well without to much cpu use. 我也确实引入了sleep(0.1)延迟,并且在不使用大量CPU的情况下也能很好地工作。 I found the answer here: https://www.reddit.com/r/learnpython/comments/36dxu9/python_whileloop_cpu_demanding/ 我在这里找到了答案: https : //www.reddit.com/r/learnpython/comments/36dxu9/python_whileloop_cpu_demanding/

The only problem which i find now is that the pygame window stops with the message no responde (doesn´t respond or something similar) and it stops working. 我现在发现的唯一问题是pygame窗口停止,并显示消息no responsee(不响应或类似内容)并且停止工作。 For example the cube which i´m moving with the keys of the piano doesn´t do anything but the key to close the program does work fine!! 例如,我用钢琴的键移动的立方体什么也不做,但是关闭程序的键确实可以正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM