在python中使用pygame查找多条线的每个交点,以创建游戏板

如何解决在python中使用pygame查找多条线的每个交点,以创建游戏板

我需要找到代码中线的每个交点。在这些时候,我想放我的游戏作品。游戏的逻辑就像井字游戏一样,如果玩家连续放置3个相同颜色的大理石,他/她就可以抓住另一位未按顺序排列的玩家。

到目前为止,我的代码

import pygame
 
# Define some colors
BLACK = (0,0)
WHITE = (255,255,255)
GREEN = (0,0)
RED = (255,0)
 
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 20
HEIGHT = 20
 
# This sets the margin between each cell
MARGIN = 5
 
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
for row in range(19):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(19):
        grid[row].append(0)  # Append a cell
 
# Set row 1,cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
 
pygame.init()
WINDOW_SIZE = [800,600]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Array Backed Grid")
done = False
clock = pygame.time.Clock()
while not done:
    # Set the screen background
    screen.fill(BLACK)
 
    # Draw the grid
    for row in range(19):
        for column in range(19):
            color = WHITE
            if grid[row][column] == 1:
                color = GREEN
                board_lines = [
                                ( 13,15,462,15 ),( 13,469,469 ),#lin1 and line2,outer rect
                                ( 62,86,409,86 ),( 62,389,389 ),#line3 and l4,mid reect
                                ( 114,186,360,186 ),( 114,318,318 ),#line5,l6,internl rect
                                ( 13,13,( 462,12,#line9,l10,left and right sides
                                ( 62,62,( 409,85,#l7,l8left and right sides
                                ( 114,114,316),( 360,187,#l11,lin12left and right sides
                                ( 237,237,( 237,320 ),#upper V.line,lowerV
                                ( 13,242,113,242 ),242 ) #rIGHT LEFT hoRIZONTAL LINE
                                 ] 
                                
                for line in board_lines:
                    line_from = ( line[0],line[1] )
                    line_to   = ( line[2],line[3] )
                    pygame.draw.line( screen,RED,line_from,line_to,3)

    # Limit to 60 frames per second
    clock.tick(60)
    pygame.display.flip()
 
pygame.quit()

解决方法

以下函数计算由点(P0P1和(Q0Q1)给定的2条线的交点:

def lineLineIntersect(P0,P1,Q0,Q1):  
    d = (P1[0]-P0[0]) * (Q1[1]-Q0[1]) + (P1[1]-P0[1]) * (Q0[0]-Q1[0]) 
    if d == 0:
        return None
    t = ((Q0[0]-P0[0]) * (Q1[1]-Q0[1]) + (Q0[1]-P0[1]) * (Q0[0]-Q1[0])) / d
    u = ((Q0[0]-P0[0]) * (P1[1]-P0[1]) + (Q0[1]-P0[1]) * (P0[0]-P1[0])) / d
    if 0 <= t <= 1 and 0 <= u <= 1:
        return round(P1[0] * t + P0[0] * (1-t)),round(P1[1] * t + P0[1] * (1-t))
    return None

Problem with calculating line intersections的答案中详细解释了该算法:

P     ... point on the 1. line
R     ... normalized direction of the 1. line

Q     ... point on the 2. line
S     ... normalized direction of the 2. line

alpha ... angle between Q-P and R
beta  ... angle between R and S

gamma  =  180° - alpha - beta

h  =  | Q - P | * sin(alpha)
u  =  h / sin(beta)

t  = | Q - P | * sin(gamma) / sin(beta)

t  =  dot(Q-P,(S.y,-S.x)) / dot(R,-S.x))  =  determinant(mat2(Q-P,S)) / determinant(mat2(R,S))
u  =  dot(Q-P,(R.y,-R.x)) / dot(R,R)) / determinant(mat2(R,S))

X  =  P + R * t  =  Q + S * u

您必须评估线段是否与任何其他线段相交。通过使用函数lineLineIntersect,遍历嵌套循环中的各行,并找到一条线与除自身以外的任何其他线的交点:

intersectionPoints = []
for i,line1 in enumerate(board_lines):
    for line2 in board_lines[i:]:
        isectP = lineLineIntersect(line1[:2],line1[2:],line2[:2],line2[2:])
        if isectP:
            intersectionPoints.append(isectP)

在相交点绘制圆:

for isectP in intersectionPoints:
    pygame.draw.circle(screen,GREEN,isectP,5)

查看示例:

import pygame
import math

def lineLineIntersect(P0,round(P1[1] * t + P0[1] * (1-t))
    return None

board_lines = [
    ( 13,15,462,15 ),( 13,469,469 ),#lin1 and line2,outer rect
    ( 62,86,409,86 ),( 62,389,389 ),#line3 and l4,mid reect
    ( 114,186,360,186 ),( 114,318,318 ),#line5,l6,internl rect
    ( 13,13,( 462,12,#line9,l10,left and right sides
    ( 62,62,( 409,85,#l7,l8left and right sides
    ( 114,114,316),( 360,187,#l11,lin12left and right sides
    ( 237,237,( 237,320 ),#upper V.line,lowerV
    ( 13,242,113,242 ),242 ) #rIGHT LEFT hoRIZONTAL LINE
] 

pygame.init() 

intersectionPoints = []
for i,line2[2:])
        if isectP:
            intersectionPoints.append(isectP)
 
# Define some colors
BLACK = (0,0)
WHITE = (255,255,255)
GREEN = (0,0)
RED = (255,0)
 
# This sets the WIDTH and HEIGHT of each grid location
WIDTH = 20
HEIGHT = 20
 
# This sets the margin between each cell
MARGIN = 5
 
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid = []
for row in range(19):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(19):
        grid[row].append(0)  # Append a cell
 
# Set row 1,cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
 
WINDOW_SIZE = [800,600]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Array Backed Grid")
done = False
clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # Set the screen background
    screen.fill(BLACK)
 
    # Draw the grid
    for row in range(19):
        for column in range(19):
            color = WHITE
            if grid[row][column] == 1:
                color = GREEN
                
    for line in board_lines:
        pygame.draw.line(screen,RED,line[:2],line[2:],3)

    for isectP in intersectionPoints:
        pygame.draw.circle(screen,5)

    # Limit to 60 frames per second
    clock.tick(60)
    pygame.display.flip()
 
pygame.quit()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?