2017-11-25 4 views
1

파이 게임에 대한 도움을 찾고 있습니다. 나는 파이 게임에서 파이 게임을 배우기 위해 간단한 게임을 개발 중이다. 나는 회전 할 수있는 우주선을 만들고 레이저 라인으로 촬영할 수 있습니다. 나는 화살표 키로 제어 할 수 있었고 마우스 위치로 우주선을 회전시킬 수도 있지만 촬영에는 문제가있다. 우주선 위치에서 마우스 방향으로 무한 길이의 선을 만들고 싶습니다. 내가 어떻게 할 수 있니? 이 작업은 우주선의 오른쪽에 있기 때문에 그것은 제대로 작동하지파이 게임에서 커서 방향으로 무한 길이의 드로잉 라인

def draw_objects(self): 
     SCREEN.fill(BLACK) 
     self.target = pygame.mouse.get_pos() 
     self.x = self.player.position[0] #player x position 
     self.y = self.player.position[1] #player y position 
     self.mx = self.target[0] #mouse x position 
     self.my = self.target[1] #mouse y position 
     self.slope=float(float(self.y-self.my)/float(self.x-self.mx+0.1)) #slope 
     self.x_new = DISPLAY_WIDTH #ray length 
     self.y_new = self.y + self.slope * (self.x_new - self.x) 
     self.player.draw() 
     self.draw_columns() 
     for agent in self.all_agents: 
      agent.draw() 
      agent.draw_vectors() 
     if self.player.shoot == True: 
      pygame.draw.line(SCREEN, GREEN, self.player.position,(self.x_new, self.y_new), 2) 

     pygame.display.update() 

enter image description here

: 여기 내 코드입니다. 다른 경우 커서로 반사 된 선을 그립니다.

enter image description here

나는 당신의 도움에 감사 할 것이다.

+0

당신은 계산에 사용하는 변수에 값을 표시하고 종이에 계산 된 값과 비교하기 위해'print()'를 사용할 수 있습니다. – furas

+0

'슬로프'는 방향을 유지하지 않으므로 혼자서해야합니다. 'self.y-self.my'와'self.x-self.mx + 0.1'에서 표지판 (+/-)을 얻을 수 있으며이 기호를'self.x_new'와'self.y_new'와 함께 사용하면 올바른 결과를 얻을 수 있습니다 방향. (PL : powodzenia) – furas

답변

1

slope 방향을 유지하지 못합니다. 라인을 이동

  • 이동 마우스 플레이어 새로운 위치를 설정하는
  • 클릭 왼쪽 버튼 :
    당신은 player_x - mouse_x + 0.1의 기호를 얻을 x_new

    dx = player_x - mouse_x + 0.1 
    
        reversed_sign_x = 1 if dx < 0 else -1 
    
        x_new = reversed_sign_x * DISPLAY_WIDTH 
    

    전체 작업 예제를 사용할 필요가 .

.

import pygame 

# --- constants --- 

BLACK = (0, 0, 0) 
GREEN = (0, 255, 0) 
DISPLAY_WIDTH = 800 
DISPLAY_HEIGHT = 600 

# --- functions --- 

def calculate(player_x, player_y, mouse_x, mouse_y): 
    dx = player_x - mouse_x + 0.1 
    dy = player_y - mouse_y 

    reversed_sign_x = 1 if dx < 0 else -1 
    #reversed_sign_y = 1 if dy < 0 else -1 

    slope = dy/dx 

    x_new = reversed_sign_x * DISPLAY_WIDTH 
    y_new = player_y + slope * (x_new - player_x) 

    return x_new, y_new 

# --- main --- 

# - init - 

pygame.init() 
SCREEN = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT)) 

# - objects - 

player_x = DISPLAY_WIDTH // 2 
player_y = DISPLAY_HEIGHT // 2 

mouse_x = player_x 
mouse_y = player_y 

x_new, y_new = calculate(player_x, player_y, mouse_x, mouse_y) 

# - mainloop - 

clock = pygame.time.Clock() 
running = True 

while running: 

    # - events - 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      running = False 
     elif event.type == pygame.MOUSEBUTTONDOWN: 
      player_x, player_y = event.pos 
     elif event.type == pygame.MOUSEMOTION: 
      x_new, y_new = calculate(player_x, player_y, event.pos[0], event.pos[1]) 

    # - updates - 

    # empty 

    # - draws - 

    SCREEN.fill(BLACK) 
    pygame.draw.line(SCREEN, GREEN, (player_x, player_y), (x_new, y_new), 2) 
    pygame.display.flip() 

    # - FPS - 

    clock.tick(25) 

# - end - 

pygame.quit() 
+0

대단히 고맙습니다. 내 문제를 이해하는 것이 매우 도움이되었습니다. (PL : dziękuję :)) –

1

furas 잘, 당신은 마우스가 플레이어의 왼쪽이나 오른쪽에 있는지 여부를 확인하고 왼쪽의 경우 DISPLAY_WIDTH을 부정해야한다. 나는 비슷한 해결책을 찾았습니다 :

이 함수는 대상 좌표를 계산하고 반환합니다 (함수는 오직 한 가지만 수행해야 함). 선과 다른 모든 것을 다른 함수로 그립니다.

또 다른 해결책이 있습니다. pygame vectors을 사용하고 먼저 대상에 대한 벡터를 계산하고 정규화 한 다음 원하는 길이 (DISPLAY_WIDTH)로 확장합니다.

import pygame 
from pygame.math import Vector2 


pygame.init() 
DISPLAY_WIDTH = 640 
GREEN = pygame.Color('aquamarine1') 
screen = pygame.display.set_mode((640, 480)) 
clock = pygame.time.Clock() 
position = Vector2(300, 200) # A pygame.math.Vector2 as the position. 
done = False 

while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

    screen.fill((30, 30, 30)) 
    pygame.draw.circle(screen, GREEN, (int(position.x), int(position.y)), 7) 
    # Calculate the vector to the target by subtracting pos from mouse pos. 
    # Normalizing it gives you a unit vector which you can scale 
    # by multiplying it with the DISPLAY_WIDTH. 
    target_vec = (pygame.mouse.get_pos()-position).normalize() * DISPLAY_WIDTH 
    pygame.draw.line(screen, GREEN, position, position+target_vec, 2) 

    pygame.display.flip() 
    clock.tick(30) 

pygame.quit() 
+0

벡터 2 - 좋은 지적 – furas

+0

대단히 감사합니다. 이 방법으로 (벡터 사용)이 문제를 해결하려고했지만 작동하지 않았습니다. 이제 작동합니다. 고맙습니다! –

+0

실례합니다. 문제와 관련된 질문이 하나 더 있습니다. 이제 적을 총격하기를 원합니다. 이전 문제를 해결하기 위해 벡터 솔루션을 사용합니다. 이제 레이저로 촬영하여 적을 죽일 필요가 있습니다. 방황하고있는 적들의 스프라이트 그룹이 있습니다. 플레이어 레이저 라인 벡터로 어떻게 할 수 있습니까? 나는 그것을 시작하는 방법을 모른다. –