Python 如何检测一次按键并使用'=';象征?

Python 如何检测一次按键并使用'=';象征?,python,pygame,Python,Pygame,我试图找出如何在按住“向上”箭头键时增加形状移动的强度。当我拿着空格键的时候,我希望我的形状能根据我所做的强度发出隆隆声。我还想做的是能够通过按一次“向上”箭头键来增加强度(注意:一次。目前,当我按一次箭头键时,它的循环速度太快,将强度乘以5000x。我希望每次只添加一个数字。)这是我不喜欢的代码 import pygame import random import time BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255

我试图找出如何在按住“向上”箭头键时增加形状移动的强度。当我拿着空格键的时候,我希望我的形状能根据我所做的强度发出隆隆声。我还想做的是能够通过按一次“向上”箭头键来增加强度(注意:一次。目前,当我按一次箭头键时,它的循环速度太快,将强度乘以5000x。我希望每次只添加一个数字。)这是我不喜欢的代码

import pygame
import random
import time



BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Edit the intensity of the shake (Must be one number apart)
# Ex: a = -100, b = 101. A is negative, B is positive
a = -4
b = 5
up = 10
intensity = (a, b)

pygame.init()

size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

done = False

clock = pygame.time.Clock()


class Rectangle():
    def __init__(self):
        self.x = random.randrange(0, 700)
        self.y = random.randrange(0, 500)
        self.height = random.randrange(20, 70)
        self.width = random.randrange(20, 70)
        self.x_change = random.randrange(-3, 3)
        self.y_change = random.randrange(-3, 3)
        self.color = random.sample(range(250), 4)

    def draw(self):
        pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])

    def move(self):
        self.x += self.x_change
        self.y += self.y_change


class Ellipse(Rectangle):
    pass

    def draw(self):
        pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])

    def move(self):
        self.x += self.x_change
        self.y += self.y_change

my_list = []
for number in range(600):
    my_object = Rectangle()
    my_list.append(my_object)
for number in range(600):
    my_object = Ellipse()
    my_list.append(my_object)

# -------- Main Program Loop -----------
while not done:
    keys = pygame.key.get_pressed()
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(BLACK)
    for rect in my_list:
        rect.draw()
        rect.move()
    for rectElli in my_list:
        rectElli.draw()
        if keys[pygame.K_SPACE]:
            rectElli.y_change = random.randrange(a, b)
            rectElli.x_change = random.randrange(a, b)
            rectElli.move()
            print(a)
            print(b)
        if keys[pygame.K_UP] and up / 10 == 1:
            up += 1
            a -= 1
            b -= -1
            print(a)
            print(b)
            print(up)
        if up / 10 != 1:
            up += 1

    pygame.display.flip()
    clock.tick(60)
pygame.quit()
这是在我想要增加强度之前已经运行过的代码

import pygame
import random
import time



BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Edit the intensity of the shake (Must be one number apart)
# Ex: a = -100, b = 101. A is negative, B is positive
a = -4
b = 5
up = 10
intensity = (a, b)

pygame.init()

size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

done = False

clock = pygame.time.Clock()


class Rectangle():
    def __init__(self):
        self.x = random.randrange(0, 700)
        self.y = random.randrange(0, 500)
        self.height = random.randrange(20, 70)
        self.width = random.randrange(20, 70)
        self.x_change = random.randrange(-3, 3)
        self.y_change = random.randrange(-3, 3)
        self.color = random.sample(range(250), 4)

    def draw(self):
        pygame.draw.rect(screen, self.color, [self.x, self.y, self.width, self.height])

    def move(self):
        self.x += self.x_change
        self.y += self.y_change


class Ellipse(Rectangle):
    pass

    def draw(self):
        pygame.draw.ellipse(screen, self.color, [self.x, self.y, self.width, self.height])

    def move(self):
        self.x += self.x_change
        self.y += self.y_change

my_list = []
for number in range(600):
    my_object = Rectangle()
    my_list.append(my_object)
for number in range(600):
    my_object = Ellipse()
    my_list.append(my_object)

# -------- Main Program Loop -----------
while not done:
    keys = pygame.key.get_pressed()
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(BLACK)
    for rect in my_list:
        rect.draw()
        rect.move()
    for rectElli in my_list:
        rectElli.draw()
        if keys[pygame.K_SPACE]:
            rectElli.y_change = random.randrange(a, b)
            rectElli.x_change = random.randrange(a, b)
            rectElli.move()

    pygame.display.flip()
    clock.tick(60)
pygame.quit()

第一个问题是,对于穿过椭圆的-循环,要增加
中的强度。所以强度不是增加一次,而是每个椭圆增加一次

如果不希望每10帧增加一次强度,则必须验证是否每帧增加一次帧计数器
up
,但必须验证它是否可被10整除。验证整除10的余数是否为0。使用模运算符(
%
)计算余数:

如果向上%10==0:
请参见示例,当按下“向上”时,强度增加,释放时,强度恢复正常:

未完成时:
keys=pygame.key.get_pressed()
#---主事件循环
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
屏幕填充(黑色)
对于my_列表中的rect:
rect.draw()
rect.move()
对于my_列表中的rectElli:
rectElli.draw()
如果键[pygame.K_SPACE]:
rectElli.y_change=random.randrange(a,b)
rectElli.x_change=random.randrange(a,b)
rectElli.move()
如果键[pygame.K_UP]:
向上+=1
如果向上%10==0:
a-=1
b-=-1
其他:
a、 b=-4,5
pygame.display.flip()
时钟滴答(60)