使用pygame或任何其他工具在python中形成2d数组后绘制一条线
我正在使用python和pygame创建一个游戏。首先,我尝试使用pygame.draw工具创建游戏,但后来我意识到使用这种方法很难调用点和训练数据集使用pygame或任何其他工具在python中形成2d数组后绘制一条线,python,arrays,python-3.x,arraylist,pygame,Python,Arrays,Python 3.x,Arraylist,Pygame,我正在使用python和pygame创建一个游戏。首先,我尝试使用pygame.draw工具创建游戏,但后来我意识到使用这种方法很难调用点和训练数据集 import pygame import sys WINDOW_WIDTH = 800 WINDOW_HEIGHT= 600 #define colour white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (121,202,255)
import pygame
import sys
WINDOW_WIDTH = 800
WINDOW_HEIGHT= 600
#define colour
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (121,202,255)
yellow= (255,255,0)
def lineRectIntersectionPoints( line, rect ):
""" Get the list of points where the line and rect
intersect, The result may be zero, one or two points.
BUG: This function fails when the line and the side
of the rectangle overlap """
def linesAreParallel( x1,y1, x2,y2, x3,y3, x4,y4 ):
""" Return True if the given lines (x1,y1)-(x2,y2) and
(x3,y3)-(x4,y4) are parallel """
return (((x1-x2)*(y3-y4)) - ((y1-y2)*(x3-x4)) == 0)
def intersectionPoint( x1,y1, x2,y2, x3,y3, x4,y4 ):
""" Return the point where the lines through (x1,y1)-(x2,y2)
and (x3,y3)-(x4,y4) cross. This may not be on-screen """
#Use determinant method, as per
#Ref: https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
Px = ((((x1*y2)-(y1*x2))*(x3 - x4)) - ((x1-x2)*((x3*y4)-(y3*x4)))) / (((x1-x2)*(y3-y4)) - ((y1-y2)*(x3-x4)))
Py = ((((x1*y2)-(y1*x2))*(y3 - y4)) - ((y1-y2)*((x3*y4)-(y3*x4)))) / (((x1-x2)*(y3-y4)) - ((y1-y2)*(x3-x4)))
return Px,Py
### Begin the intersection tests
result = []
line_x1, line_y1, line_x2, line_y2 = line # split into components
pos_x, pos_y, width, height = rect
### Convert the rectangle into 4 lines
rect_lines = [ ( pos_x, pos_y, pos_x+width, pos_y ), ( pos_x, pos_y+height, pos_x+width, pos_y+height ), # top & bottom
( pos_x, pos_y, pos_x, pos_y+height ), ( pos_x+width, pos_y, pos_x+width, pos_y+height ) ] # left & right
### intersect each rect-side with the line
for r in rect_lines:
rx1,ry1, rx2,ry2 = r
if ( not linesAreParallel( line_x1,line_y1, line_x2,line_y2, rx1,ry1, rx2,ry2 ) ): # not parallel
pX, pY = intersectionPoint( line_x1,line_y1, line_x2,line_y2, rx1,ry1, rx2,ry2 ) # so intersecting somewhere
# Lines intersect, but is it on-screen?
if ( pX >= 0 and pY >= 0 and pX < WINDOW_WIDTH and pY < WINDOW_HEIGHT ): # yes! on-screen
result.append( ( round(pX), round(pY) ) ) # keep it
if ( len( result ) == 2 ):
break # Once we've found 2 intersection points, that's it
return result
#opening window
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
#pygame.draw.rect(screen, red, (200,100,250,250), 2)
#pygame.draw.rect(screen, green, (150,75,350,300), 2)
#pygame.draw.rect(screen, red, (100,50,450,350), 2)
##draw lines
##upper line
#pygame.draw.line(screen,white,(325,50),(325,100),3)
##lower line
#pygame.draw.line(screen,white,(325,350),(325,400),3)
##left line
#pygame.draw.line(screen,white,(100,200),(200,200),3)
##right line
#pygame.draw.line(screen,white,(450,200),(550,200),3)
#@kingsley correction of the code
board_rects = [ ( 200,100,250,250, red ), ( 150,75,350,300, green ), ( 100,50,450,350, red ) ]
for rect in board_rects:
x_pos, y_pos = rect[0], rect[1]
width, height = rect[2], rect[3]
colour = rect[4]
pygame.draw.rect( screen, colour, ( x_pos, y_pos, width, height ), 2 )
board_lines = [ ( 325,50,325,100 ), ( 325,350,325,400 ), ( 100,200,200,200 ), ( 450,200,550,200 ) ]
for line in board_lines:
line_from = ( line[0], line[1] )
line_to = ( line[2], line[3] )
pygame.draw.line( screen, white, line_from, line_to, 3)
for line in board_lines:
for rect in board_rects:
rect = rect[ 0: -1 ] # trim off the colour
intersection_points = lineRectIntersectionPoints( line, rect )
for p in intersection_points:
pygame.draw.circle( screen, blue, p, 10 ) # Draw a circle at the intersection point
#updating window
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
我想保留的箱子
我想在选定的框上绘制LIN
盒子的坐标
您不能“删除”在显示表面上绘制的框,不要绘制它们:
未完成时:
# [...]
对于范围(19)中的行:
对于范围(19)中的列:
如果网格[行][列]==1:
颜色=绿色
方框矩形=[(边距+宽度)*列+边距,
(边距+高度)*行+边距,
宽度,
高度]
pygame.draw.rect(屏幕、颜色、方框)
要画线,你必须找到盒子的中心。使用对象查找长方体的中心点。带有索引(
行
,列
)的框的中心点可通过以下方式计算:
box\u rect=pygame.rect((边距+宽度)*列+边距,
(边距+高度)*行+边距,
宽度,
高度)
box\u center=box\u rect.center
您不能“删除”在显示表面上绘制的框,不要绘制它们:
未完成时:
# [...]
对于范围(19)中的行:
对于范围(19)中的列:
如果网格[行][列]==1:
颜色=绿色
方框矩形=[(边距+宽度)*列+边距,
(边距+高度)*行+边距,
宽度,
高度]
pygame.draw.rect(屏幕、颜色、方框)
要画线,你必须找到盒子的中心。使用对象查找长方体的中心点。带有索引(
行
,列
)的框的中心点可通过以下方式计算:
box\u rect=pygame.rect((边距+宽度)*列+边距,
(边距+高度)*行+边距,
宽度,
高度)
box\u center=box\u rect.center
我想知道点/框的确切位置,以便以后参考。我只需要突出显示的框,但我找不到创建它们的方法,因为我对编程和编码还不熟悉。你能指出我该如何做吗?可能我误解了你的问题。您的代码运行良好。你想达到什么目标?你需要有人为你计算坐标吗?我只想保留突出显示的框,并想删除/隐藏/无法访问所有其他框,还想画一条线,如图所示。我已经找到了一种在这些点上画线的方法,现在我只想删除/删除/隐藏其他框。你不能删除框,只是不绘制它们。我想获得点/框的确切位置,以便以后参考它们。我只需要突出显示的框,但我找不到创建它们的方法,因为我对编程和编码还不熟悉。你能指出我该如何做吗?可能我误解了你的问题。您的代码运行良好。你想达到什么目标?你需要有人为你计算坐标吗?我只想保留突出显示的框,并想删除/隐藏/无法访问所有其他框,还想画一条线,如图所示。我已经找到了在这些点上画线的方法,现在我只想删除/删除/隐藏其他框。你不能删除框,只是不绘制它们。然后整个网格将消失,突出显示的框将不会出现。但是是的,你给我一个主意,让我先检查一下……@shyckh@shyckh为什么?我以为绿色框由网格[row][column]==1表示(如图所示)?然后整个网格将消失,突出显示的框将不会出现。但是,是的,你给了我一个想法,让我先检查一下……@shyckh@shyckh为什么?我以为绿色框由网格[row][column]==1表示(如图所示)?
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
WIDTH = 20
HEIGHT = 20
MARGIN = 5
#set the array
grid = []
for row in range(19):
grid.append([])
for column in range(19):
grid[row].append(0)
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[0][0] = 1
pygame.init()
# Set the width and height of the screen [width, height]
size = [600, 600]
screen = pygame.display.set_mode(size)
#set the tittle of the game
pygame.display.set_caption("My Array Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
#while not done:
# --- Main event loop
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
# Set that location to one
grid[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
# --- Game logic should go here
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(BLACK)
# --- Drawing code should go here
# Draw the grid
for row in range(19):
for column in range(19):
color = WHITE
if grid[row][column] == 1:
color = GREEN
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()