将图像放在Pygame网格上
红色和蓝色的侧面每个都将其瓷砖替换为图像(没有一个图像将它们全部重叠,而是一个图像粘贴到每个瓷砖上)。我不太确定该如何解决,任何帮助都将不胜感激!
import pygame
TILESIZE = 22
margin = 1
BOARD_POS = (10, 10)
def create_board_surf():
board_surf = pygame.Surface((TILESIZE * 50 + margin * 51, TILESIZE * 26 + margin * 27))
board_surf.set_colorkey((0, 0, 0))
board_surf.set_alpha(210)
for row in range(26):
for column in range(0, 25):
color = (170, 70, 70)
rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
pygame.draw.rect(board_surf, pygame.Color(color), rect)
for column in range(25, 50):
color = (70, 95, 195)
rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
pygame.draw.rect(board_surf, pygame.Color(color), rect)
return board_surf
def create_board():
board = []
for row in range(26):
board.append([])
for column in range(50):
board[row].append(None)
return board
def main():
global screen
pygame.init()
screen = pygame.display.set_mode((1200, 650))
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
create_board()
board_surf = create_board_surf()
screen.fill(pygame.Color((200, 200, 200)))
screen.blit(board_surf, BOARD_POS)
pygame.display.flip()
clock.tick(60)
main()
The red and blue sides would each have their tiles replaced by an image (not one image overlapping them all, but one image pasted onto each tile). I'm not really sure how to go about it, and any help would be appreciated!
import pygame
TILESIZE = 22
margin = 1
BOARD_POS = (10, 10)
def create_board_surf():
board_surf = pygame.Surface((TILESIZE * 50 + margin * 51, TILESIZE * 26 + margin * 27))
board_surf.set_colorkey((0, 0, 0))
board_surf.set_alpha(210)
for row in range(26):
for column in range(0, 25):
color = (170, 70, 70)
rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
pygame.draw.rect(board_surf, pygame.Color(color), rect)
for column in range(25, 50):
color = (70, 95, 195)
rect = pygame.draw.rect(screen, color, [(margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin, TILESIZE, TILESIZE])
pygame.draw.rect(board_surf, pygame.Color(color), rect)
return board_surf
def create_board():
board = []
for row in range(26):
board.append([])
for column in range(50):
board[row].append(None)
return board
def main():
global screen
pygame.init()
screen = pygame.display.set_mode((1200, 650))
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
create_board()
board_surf = create_board_surf()
screen.fill(pygame.Color((200, 200, 200)))
screen.blit(board_surf, BOARD_POS)
pygame.display.flip()
clock.tick(60)
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
def create_board_surf()下:
在每个瓷砖绘制矩形的位置,替换pygame.draw.Rect
board_surf.blit(image_surface(image_surface), ) *列 +边距,(保证金 +图块) *行 +边距)将图像放置在瓷砖矩形所在的位置。您可以制作
image_surface
byimg = pygame.image.image.load(“ imagename.png”)
>这些图像必须缩放到瓷砖尺寸,以使它们正确拟合:
img = pygame.transform.scale(img,(tilesize,tilesize)))
如果某些图块应具有不同的图像,则可以使用列和行位置来确定要使用的图像,以相同的方式使用有些瓷砖是红色的,而另一些则根据其列为蓝色。
Under
def create_board_surf():
where you draw rectangles for each tile, replace thepygame.draw.rect
withboard_surf.blit(IMAGE_SURFACE, ((margin + TILESIZE) * column + margin, (margin + TILESIZE) * row + margin))
to put an image where the tile rectangle would of been.You can make the
IMAGE_SURFACE
byimg = pygame.image.load("IMAGENAME.png")
These images would have to be scaled to the tile size so they fit properly:
img = pygame.transform.scale(img, (TILESIZE, TILESIZE))
If some tiles should have different images, you could maybe use the column and row location to determine which image to use, the same way some tiles are red while others are blue based on their column.