python - 在矩形列表中移动单个矩形pygame
问题描述
大家好,我正在制作跳棋游戏,但我很迷茫,因为我需要在矩形列表中移动一个 pygame 矩形,但我不知道该怎么做。所以我看到 id 没有提供信息我很抱歉伙计们所以基本上我所做的是我创建了一个创建棋盘和棋子的类所以 Classe 棋盘包含一个创建棋子并返回矩形列表的函数.
class Board :
def __init__(self):
self.screen = pygame.display.set_mode((800, 800))
self.height = 800 / 8
self.width = 800/8
self.white = (255, 255, 255)
self.x = 0
self.y = 0
self.red = (99, 59, 47)
self.beige = (224, 180, 112)
self.black = (0,0,0)
def draw_board(self , num , color):
#self.screen.fill(self.white)
self.y = -100
for i in range(0,9) :
self.y = self.y + 100
#print(self.y)
if i % 2 == 0 :
if num == 1 :
self.x = -200
else :
self.x = -100
for j in range(0,4) :
self.x = self.x + 200
pygame.draw.rect(self.screen, color,(self.x, self.y ,self.height, self.width))
if i % 2 != 0 :
if num == 1 :
self.x = -100
else :
self.x = -200
for j in range(0,4) :
self.x = self.x + 200
pygame.draw.rect(self.screen, color, (self.x, self.y, self.height, self.width))
def draw_pieces(self , num,color):
black_beginning = -100
white_beginning = 400
starting = 0
black_list = []
white_list = []
perks = 0
if num == 1 :
perks = black_list
starting = black_beginning
else :
starting = white_beginning
for i in range(0,3) :
starting = starting + 100
if i % 2 == 0 :
self.x = -200
if num != 1 :
self.x = -100
else :
perks = white_list
self.x = -100
if num != 1 :
self.x = -200
for j in range(0,4) :
self.x = self.x + 200
circle = pygame.draw.circle(self.screen, color, (self.x + 50 , starting + 50 ), 40)
black_list.append(circle)
return black_list
def gameloop() :
board = Board()
board.draw_board(1, red)
board.draw_board(2, beige)
board.draw_pieces(1, black)
board.draw_pieces(2, white)
rectangle_draging = False
smth = board.draw_pieces(2, white) + board.draw_pieces(1, black) #this is the list of all rectangles
while running :
#board.draw_pieces(2)
for event in pygame.event.get() :
if event.type == pygame.QUIT :
running == False
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN : #and here i'm trying to drag and drop it
pos = pygame.mouse.get_pos()
for circle in smth :
circling = circle.collidepoint(pos)
if circling == 1 :
#circle.x = circle.x + 100
print((circle.x,circle.y))
pygame.display.update()
gameloop()````
解决方案
推荐阅读
- sql - 在嵌套选择中使用别名时 SQL 查询引发错误
- java - 为什么这个“最大”操作没有找到流中的最高数字?
- mysql - 选择列值大于另一列值的行
- javascript - 如何使用 jQuery 按自定义列对表格进行排序
- javascript - 忽略 Javascript 中的连字符(邮递员)
- python-3.x - Python:如果任何条目为空,则复制其上方的条目
- javascript - `Object.defineProperty(exports, '__esModule', { value: true })` vs `exports.__esModule = true`
- asp.net-core-mvc - 在数据传输对象(dto)模型上使用 DataAnnonations 约束来更新域模型
- html - gulp-pug 可以使用哪些选项?
- mysql - 如何使用 if else 使用列选择 sql