2017-05-03 7 views
2

두 개의 직사각형 (줄무늬)이 있습니다. 스트라이프를 클릭하고 마우스로 위아래로 이동 한 다음 K_UP 및 K_DOWN 키를 사용하여 같은 줄무늬를 이동하십시오. 필자가 작성한 코드를 사용하면 스트라이프를 마우스로 움직일 수 있지만 키는 양쪽 줄무늬를 동시에 움직이면서 동시에 움직이지 않습니다. 나는 단 1 주 코더입니다, 제발 도와주세요! 다음과 같이파이 게임 마우스/키보드 컨트롤 조건

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 
+0

안녕하세요! [complete example] (http://stackoverflow.com/help/mcve)을 게시하는 것이 더 도움이됩니다. 왜냐하면 그것은 우리가 당신을 도울 수 있기 때문입니다. – skrx

+0

전체 코드 – Bartnick81

+0

게시 주셔서 감사합니다. – skrx

답변

1

나는 코드를 구성 것 : 마우스가 클릭과 스트라이프와 event.pos 충돌하는 경우, 변수 (selected_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 
+1

후속 문의 사항이 있거나 뭔가 조정해야 할 경우 알려주십시오. – skrx

+1

당신의 솔루션은 완벽하게 작동합니다! 이 방법을 가르쳐 주셔서 고마워, 너 락! 이 프로젝트와 관련하여 많은 질문이 있습니다 (스트라이프 이동을 그리드에 맞추는 방법과 같음)하지만이 포럼을 최후의 수단으로 사용하려고 노력할 것입니다. – Bartnick81

+0

안녕하세요, 변수를 selected_stripe 다른 rect 개체로 stripe1 같이 있지만 다른 y_pos 및 stripe1 (전용) 함께 움직일 색 추가 할 수 있습니다. 나는 시도했지만 작동하지 않습니다 .. – Bartnick81