Keyboard Pygame鼠标/键盘控制条件

Keyboard Pygame鼠标/键盘控制条件,keyboard,pygame,mouse,conditional-statements,Keyboard,Pygame,Mouse,Conditional Statements,我有两个矩形(条纹),我想点击条纹用鼠标上下移动,然后用K_向上和K_向下键移动相同的条纹。使用我编写的代码,我可以用鼠标移动条纹,但按键同时移动两条条纹,而不是单独移动。我只是一个星期的程序员,请帮助 import pygame import pygame.gfxdraw pygame.init() width, height = 800, 600 gameWindow = pygame.display.set_mode((width, height)) pygame.display.set

我有两个矩形(条纹),我想点击条纹用鼠标上下移动,然后用K_向上和K_向下键移动相同的条纹。使用我编写的代码,我可以用鼠标移动条纹,但按键同时移动两条条纹,而不是单独移动。我只是一个星期的程序员,请帮助

import pygame
import pygame.gfxdraw

pygame.init()
width, height = 800, 600
gameWindow = pygame.display.set_mode((width, height))
pygame.display.set_caption("Koordynacja LoL")
clock = pygame.time.Clock()
# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 102, 0)
yellow = (255, 204, 0)

grid_color = (224, 224, 224)
k = 5


class Stripe():
    def __init__(self, color, size, pos_x, pos_y):

        self.size = size
        self.image = pygame.Surface(size)
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.rect.x = pos_x
        self.rect.y = pos_y

        self.speed = 5

    def move(self, xdir, ydir):
        self.rect.x += xdir * self.speed
        self.rect.y += ydir * self.speed

P1_len = 250
stripe1 = Stripe(green, (15, P1_len), 100, 200)

stripe_1a = Stripe(0, (15, 1000), 100, 0)
stripe_1aa = gameWindow.blit(stripe_1a.image, stripe_1a.rect)

P2_len = 110
stripe2 = Stripe(red, (15, P2_len), 200, 100)
stripe_2a = Stripe(0, (15, 1000), 200, 0)
stripe_2aa = gameWindow.blit(stripe_2a.image, stripe_2a.rect)

zielone2 = gameWindow.blit(stripe2.image, stripe2.rect)

pygame.display.update()

FPS = 15
Running = True

stripe_move = None
while Running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Running = False
        (mouseX, mouseY) = pygame.mouse.get_pos()
        (p1, p2, p3) = pygame.mouse.get_pressed()
        if  stripe_1aa.collidepoint(mouseX, mouseY) and p1 == 1:
            stripe1 = Stripe(green, (15, 250), 100, mouseY-P1_len)

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                stripe1.move(0, 1)
                print("lol")
            if event.key == pygame.K_UP:
                stripe1.move(0, -1)


        elif stripe_2aa.collidepoint(mouseX, mouseY) and p1 == 1:
            stripe2 = Stripe(red, (15, 110), 200,  mouseY - P2_len)

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                stripe2.move(0, 1)
            if event.key == pygame.K_UP:
                stripe2.move(0, -1)
                print("wut?")
        elif event.type == pygame.MOUSEBUTTONUP:
            stripe_move = None

    gameWindow.fill((255, 255, 255))

    # Draw stuff here
    """ Greedy Grid """
    for i in range(width):
        grid_x = k * i
        grid_y = k * i

        pygame.draw.line(gameWindow, grid_color, (grid_x, 0), (grid_x, height), 1)
        pygame.draw.line(gameWindow, grid_color, (0, grid_y), (width, grid_y), 1)
        pygame.draw.line(gameWindow, black, (2 * k, height - 2 * k), (width - 2 * k, height - 2 * k), 3)
        pygame.draw.line(gameWindow, black, (2 * k, height - 2 * k), (2 * k, 2 * k), 3)
        pygame.draw.aaline(gameWindow, black, (2 * k, 2 * k), (width - 2 * k, 2 * k), 3)

    pygame.draw.aaline(gameWindow, blue, (200, 115), (500, 70), True)
    gameWindow.blit(stripe1.image, stripe1.rect)
    gameWindow.blit(stripe2.image, stripe2.rect)


    # End draw stuff here
    pygame.display.update()
    clock.tick(FPS)


pygame.quit()
quit()
enter code here

我将按如下方式构造代码:如果单击鼠标,并且
事件.pos
与条带冲突,则将该条带分配给变量(
selected\u stripe
),如果用户单击其他位置,则将其设置为
None
。只要鼠标按钮按下,还可以将
滚动
变量设置为
True
。在事件循环之外,您可以将所选条带的y位置设置为鼠标位置。对于向上键和向下键,您只需使用所选条带的
move
方法即可

selected_stripe = None
scrolling = True

while Running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                scrolling = True
                if stripe_1aa.collidepoint(event.pos):
                    selected_stripe = stripe1
                elif stripe_2aa.collidepoint(event.pos):
                    selected_stripe = stripe2
                else:
                    selected_stripe = None
        elif event.type == pygame.MOUSEBUTTONUP:
            scrolling = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_DOWN:
                if selected_stripe:
                    selected_stripe.move(0, 1)
            if event.key == pygame.K_UP:
                if selected_stripe:
                    selected_stripe.move(0, -1)

    if selected_stripe and scrolling:
        mouse_x, mouse_y = pygame.mouse.get_pos()
        selected_stripe.rect.y = mouse_y

欢迎来到SO!最好发布一个,因为这会让我们更容易帮助你。我发布了整个代码谢谢你的编辑。你的解决方案非常有效!非常感谢你教我这个方法,你真是棒极了!关于这个项目,我有很多问题(比如如何将条纹移动捕捉到网格),但我当然会尝试使用这个论坛作为我的最后手段。嗨,有没有可能在变量selected_stripe中添加另一个rect对象作为stripe1,但具有不同的y_位置和颜色,可以随stripe1移动(仅限)。我试过了,但没用我不知道你到底想做什么。请提供更多细节和最终目标。你试过使用元组或列表吗?用你的代码我只能移动一个rect对象。我需要附加多个矩形(与stripe1的x坐标相同)并将它们一起移动。目标是这样的:它是红绿灯协调图如果可以同时选择多个矩形,您可以尝试使用一个列表作为
selected\u stripe
s变量,并将所选条带附加到它。