如何解决使用python和pygame在游戏板上创建游戏部件并将其放置
我只是一个初学者。我决定使用pygame制作棋盘游戏。我使用以下代码创建了开发板:
import pygame
import sys
pygame.init()
#define colour
white = (255,255,255)
black = (0,0)
red = (255,0)
green = (0,0)
blue = (0,255)
yellow= (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),(100,50,450,350),2)
#draw lines
#upper line
pygame.draw.line(screen,white,(325,50),100),3)
#lower line
pygame.draw.line(screen,400),3)
#left line
pygame.draw.line(screen,200),3)
#right line
pygame.draw.line(screen,(450,(550,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),(100,50,450,350),2)
可能会变成:
board_rects = [ ( 200,red ),( 150,300,green ),( 100,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-rects
添加另一个数据元组,就没有逻辑上的改变。
类似地:
board_lines = [ ( 325,325,100 ),( 325,400 ),200,200 ),( 450,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,y4 ):
""" Return the point where the lines through (x1,y2)
and (x3,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,height = rect
### Convert the rectangle into 4 lines
rect_lines = [ ( pos_x,pos_x+width,pos_y ),( pos_x,pos_y+height,pos_y+height ),# top & bottom
( pos_x,pos_x,( 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_y2,rx1,ry2 ) ): # not parallel
pX,pY = intersectionPoint( line_x1,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)
red = (255,0)
green = (0,0)
blue = (121,202,255)
yellow= (255,0)
def lineRectIntersectionPoints( line,Py
### Begin the intersection tests
result = []
line_x1,pos_y+height ) ] # left & right
### intersect each rect-side with the line
for r in rect_lines:
rx1,that's it
return result
#opening window
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
#pygame.draw.rect(screen,2)
#pygame.draw.rect(screen,2)
##draw lines
##upper line
#pygame.draw.line(screen,(325,50),100),3)
##lower line
#pygame.draw.line(screen,400),3)
##left line
#pygame.draw.line(screen,200),3)
##right line
#pygame.draw.line(screen,(450,(550,3)
board_rects = [ ( 200,2 )
board_lines = [ ( 325,3)
for line in board_lines:
for rect in board_rects:
rect = rect[ 0: -1 ] # trim off the colour
intersection_points = lineRectIntersectionPoints( line,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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。