Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python和pygame在游戏板上创建和放置游戏块_Python_Python 3.x_Pygame - Fatal编程技术网

使用python和pygame在游戏板上创建和放置游戏块

使用python和pygame在游戏板上创建和放置游戏块,python,python-3.x,pygame,Python,Python 3.x,Pygame,我只是一个初级程序员。我决定用pygame做一个棋盘游戏。我使用以下代码创建了该板: import pygame import sys pygame.init() #define colour white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) yellow= (255,255,0) #opening window screen = pygame.displa

我只是一个初级程序员。我决定用pygame做一个棋盘游戏。我使用以下代码创建了该板:

import pygame
import sys


pygame.init()
#define colour
white = (255,255,255)
black = (0,0,0)

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow= (255,255,0)



#opening window
screen = pygame.display.set_mode((800,600))

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)

#updateing window
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.display.update()

现在,我想在每个角和线的交点添加两个不同颜色的游戏块,比如黑色和红色。我该怎么做呢?

每当你发现自己用数字块重复行时,比如你对矩形函数的调用——想想如何将数字存储在某种列表、元组或数据结构中可以简化代码

例如:

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)
可能成为:

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 point

< >确定直线和矩形的交点的一种方法是将矩形视为一组4行,并确定每一行的给定行。当然,如果直线是平行的,它们就不会相交(它们是相交的,但是是无限的),所以我们也必须检查一下。最后,我们不关心这些线是否在屏幕外相交,这也被认为是不相交的

这给了我们一个看起来很糟糕的交集函数,但它实际上只是一系列简单的几何计算:

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 = []                                 # empty list for result-set
    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 given 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
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)

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

#updateing window
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.display.update()
def linerect相交点(直线,矩形):
“”“获取直线和矩形
相交,结果可能是零、一或两点。
错误:当线路和侧面发生故障时,此功能失败
矩形的重叠部分“”
def管路并联(x1、y1、x2、y2、x3、y3、x4、y4):
“”“如果给定的行(x1,y1)-(x2,y2)和
(x3,y3)-(x4,y4)是平行的
返回((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4))==0)
def交叉点(x1、y1、x2、y2、x3、y3、x4、y4):
“”“返回线穿过(x1,y1)-(x2,y2)的点”
和(x3,y3)-(x4,y4)交叉。这可能不在屏幕上“”
#使用行列式方法,根据
#参考: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)))
返回Px,Py
###开始交叉测试
结果=[]#结果集的空列表
行_x1、行_y1、行_x2、行_y2=行#拆分为组件
位置x,位置y,宽度,高度=矩形
###将矩形转换为4行
直线=[(位置x,位置y,位置x+宽度,位置y),(位置x,位置y+高度,位置x+宽度,位置y+高度),#顶部和底部
(位置x,位置y,位置x,位置y+高度),(位置x+宽度,位置y,位置x+宽度,位置y+高度)]左右
###将每个矩形与给定直线相交
对于直线中的r:
rx1,ry1,rx2,ry2=r
如果(非线路平行(线路x1、线路y1、线路x2、线路y2、rx1、R1、rx2、R2)):#非平行
pX,pY=相交点(线x1,线y1,线x2,线y2,rx1,R1,rx2,R2)#因此在某处相交
#线相交,但它在屏幕上吗?
如果(pX>=0和pY>=0和pX<窗宽和pY<窗高):#是!屏幕上
结果。追加((圆形(pX),圆形(pY))#保留它
如果(len(result)==2):
中断#一旦我们找到两个交点,就这样了
返回结果
把它们放在一起:

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 = []                                 # empty list for result-set
    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 given 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
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)

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

#updateing window
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    pygame.display.update()
导入pygame
导入系统
窗宽=800
窗户高度=600
#定义颜色
白色=(255255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0255,0)
蓝色=(121202255)
黄色=(255255,0)
def LineRect相交点(直线、矩形):
“”“获取直线和矩形
相交,结果可能是零、一或两点。
错误:当线路和侧面发生故障时,此功能失败
矩形的重叠部分“”
def管路并联(x1、y1、x2、y2、x3、y3、x4、y4):
“”“如果给定的行(x1,y1)-(x2,y2)和
(x3,y3)-(x4,y4)是平行的
返回((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4))==0)
def交叉点(x1、y1、x2、y2、x3、y3、x4、y4):
“”“返回线穿过(x1,y1)-(x2,y2)的点”
和(x3,y3)-(x4,y4)交叉。这可能不在屏幕上“”
#使用行列式方法,根据
#参考: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)))
返回Px,Py
###开始交叉测试
结果=[]
行_x1、行_y1、行_x2、行_y2=行#拆分为组件
位置x,位置y,宽度,高度=矩形
###将矩形转换为4行
直线=[(位置x,位置y,位置x+宽度,位置y),(位置x,位置y+高度,位置x+宽度,位置y+高度),#顶部和底部
(位置x,位置y,位置x,位置y+高度),(位置x+宽度,位置y,位置x+宽度,位置y+高度)]左右
###将每个矩形与直线相交
对于直线中的r:
rx1,ry1,rx2,ry2=r
如果(不是)线路