2017-01-29 11 views
0

私はワイヤーに触れない(あなたはワイヤー/ラインの周りを動かさなければならない)ことに基づいている小さなゲームを構築しようとしています。ワイヤーが無作為に動くので、ワイヤーとサークルの端の間の衝突をどこからプログラムするかはあまり確かではありません。私はラインのすべての座標を格納するリストを作成する必要がありますか?ここ は、それのためのコードである:PyGame Collsion Trouble

import pygame 
from random import randint 

pygame.init() 

white = (255, 255, 255) 
black = (0, 0, 0) 
red = (255, 0, 0) 
green = (0, 255, 0) 
blue = (0, 0, 255) 

display_width = 600 
display_height = 600 

gameDisplay = pygame.display.set_mode((display_width, display_height)) 
pygame.display.set_caption("Wire") 
font = pygame.font.SysFont("aerial", 20) 
clock = pygame.time.Clock() 
pygame.display.update() 


def text_objects(text, colour): 
    textSurface = font.render(text, True, colour) 
    return textSurface, textSurface.get_rect() 

def message_to_screen(msg, colour, xpos, ypos): 
    textSurf, textRect = text_objects(msg, colour) 
    textRect.center = (xpos), (ypos) 
    gameDisplay.blit(textSurf, textRect) 

def gameLoop(FPS): 
    start = 0 
    from math import pi 
    gameExit = False 
    gameOver = False 
    num_top = 300 
    num_bot = 300 
    pygame.mouse.set_pos(display_width/2, display_height/2) 
    line_array =[] 
    line_thickness = 8 

    while not gameExit: 
     mouse = pygame.mouse.get_pos() 
     # print(mouse) 
     if (start == 0): 
      print("Game Started") 
     elif(mouse[0] <= 48) or (mouse[0] >= 547) or (mouse[1] <= 25) or (mouse[1] >=585): 
      gameOver = True 
     start += 1 
     if gameOver == True: 
      gameDisplay.fill(white) 
      message_to_screen("You Lost", red, 300, 300) 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       gameExit = True 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_q: 
        gameExit = True 

     rand_nums = [randint(-3, 3), randint(-3, 3)] 
     num_top += rand_nums[0] 
     num_bot += rand_nums[1] 
     message_to_screen("Press q or the x button to quit", blue, 100, 40) 
     pygame.display.update() 
     gameDisplay.fill(white) 
     pygame.draw.arc(gameDisplay, red, [mouse[0]-48, mouse[1]-25, 100, 40], 0, pi, 3) 
     pygame.draw.line(gameDisplay, black, [num_top, 0], [num_bot, 600], line_thickness) 
     pygame.draw.arc(gameDisplay, red, [mouse[0]-48, mouse[1]-25, 100, 40], 3 * pi, 2 * pi, 3) 

     clock.tick(FPS) 

    pygame.quit() 
    quit() 
gameLoop(100) 
+0

として知らOD インターセプト定理、ベースとcomparition dx/dy == dx_p1/dy_p1を使用しています - そして、ある点がオンラインであるかどうかを調べる数学的方法があります。行の開始点と終了点のみが必要です。あなたはライン上のすべてのポイントのリストを表示する必要はありません。 – furas

+0

私は行の開始点と終了点を指定しただけでどうしますか?ピクセルのリストを作成する必要がありますか? –

+0

http://gamedev.stackexchange.com/questions/26004/how-to-detect-2d-line-online-collision – Viney

答えて

0

Iは角度を計算する(画像上の赤と緑の角度を参照)、それらを比較します。それらがほぼ同一であれば、ポイントはオンラインです - 衝突があります。

floatは理想的な結果ではないため、角度がの場合はほぼと同じです。

if -0.01 < tanges_angle - tanges_angle_p1 < 0.01: 

なお、第1のエンジェル(緑色)を算出する0.01

より小さいか大きい値を必要とするかもしれない私は底点と黒線の頂点を必要とします。 2番目の角度を計算するには、黒い線の下点と円の点が必要です。

enter image description here

#!/usr/bin/env python3 

import pygame 
from random import randint 
from math import pi 

# --- constants --- (UPPER_CASE names) 

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

DISPLAY_WIDTH = 600 
DISPLAY_HEIGHT = 600 
CENTER_X = DISPLAY_WIDTH//2 
CENTER_Y = DISPLAY_HEIGHT//2 

FPS = 30 

LINE_THICKNESS = 8 

PI_2 = 2*pi 
PI_3 = 3*pi 

# --- functions --- (lower_cas names) 

def render_text(text, colour): 
    image = font.render(text, True, colour) 
    rect = image.get_rect() 
    return image, rect 

def message(screen, msg, colour, x, y): 
    image, rect = render_text(msg, colour) 
    rect.center = (x, y) 
    screen.blit(image, rect) 

def gameloop(): 

    num_top = 300 
    num_bot = 300 

    pygame.mouse.set_pos(CENTER_X, CENTER_Y) 

    start = True 
    game_exit = False 
    game_over = False 

    while not game_exit: 

     # - events - 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       game_exit = True 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_q: 
        game_exit = True 

     # - updates - 

     m_x, m_y = pygame.mouse.get_pos() 

     if start: 
      print("Game Started") 
      start = False 
     elif (m_x <= 48) or (m_x >= 547) or (m_y <= 25) or (m_y >= 585): 
      game_over = True 

     if game_over == True: 
      screen.fill(WHITE) 
      message(screen, "You Lost", RED, 300, 300) 

     num_top += randint(-3, 3) 
     num_bot += randint(-3, 3) 

     # proportion 

     dx = num_bot - num_top 
     dy = DISPLAY_HEIGHT 
     tanges_angle = dx/dy 

     dx_p1 = num_bot - (m_x-48) # or smaller ie. m_x-46 
     dy_p1 = DISPLAY_HEIGHT - m_y 
     tanges_angle_p1 = dx_p1/dy_p1 

     if -0.01 < tanges_angle - tanges_angle_p1 < 0.01: 
      print('Collide left') 

     dx_p2 = num_bot - (m_x+48) # or smaller ie. m_x+46 
     dy_p2 = DISPLAY_HEIGHT - m_y 
     tanges_angle_p2 = dx_p2/dy_p2 

     if -0.01 < tanges_angle - tanges_angle_p2 < 0.01: 
      print('Collide right') 

     # - draws - 

     screen.fill(WHITE) 

     pygame.draw.arc(screen, RED, (m_x-48, m_y-25, 100, 40), 0, pi, 3) 
     pygame.draw.line(screen, BLACK, (num_top, 0), (num_bot, 600), LINE_THICKNESS) 
     pygame.draw.arc(screen, RED, (m_x-48, m_y-25, 100, 40), PI_3, PI_2, 3) 

     message(screen, "Press q or the x button to quit", BLUE, 100, 40) 

     pygame.display.update() 

     # - FPS - 

     clock.tick(FPS) 

    # - end - 

    pygame.quit() 
    quit() 

# --- main --- 

pygame.init() 

screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT)) 
pygame.display.set_caption("Wire") 

font = pygame.font.SysFont("aerial", 20) 
clock = pygame.time.Clock() 

gameloop() 

はまた、あなたが、彼らは黒いライン上アーカンソー場合、円からわずか2点を確認する必要がThales' theorem