gpt4 book ai didi

Python3 内存游戏随机洗牌图像

转载 作者:行者123 更新时间:2023-12-04 10:56:16 25 4
gpt4 key购买 nike

我正在 pygame 中开发一款名为“Memory”的游戏。我试图在表面的表面上 blit 图像。我想将 8 个图像成对传输,但每次运行都是随机顺序。但是,它在 4*4 网格中显示 16 个相同的图像,而不是 2 组 8 个不同的图像。我不知道我应该在哪里改变才能解决这个问题。有人,请帮助我!非常感谢。

import pygame
import random
import time
screen = pygame.display.set_mode((525, 435))

def main():
pygame.init()
pygame.display.set_mode((535, 435))
pygame.display.set_caption('Memory')
w_surface = pygame.display.get_surface()
game = Game(w_surface)
game.play()
pygame.quit()

class Game:

def __init__(self, surface):
self.surface = surface
self.bg_color = pygame.Color('black')
self.FPS = 10
self.game_Clock = pygame.time.Clock()
self.close_clicked = False
self.continue_game = True
Tile.set_surface(self.surface)
self.grid_size = 4
self.grid = [ [] ]
image1 = './images/1.jpg'
image2 = './images/2.jpg'
image3 = './images/3.jpg'
image4 = './images/4.jpg'
image5 = './images/5.jpg'
image6 = './images/6.jpg'
image7 = './images/7.jpg'
image8 = './images/8.jpg'
self.images = [
image1, image1,
image2, image2,
image3, image3,
image4, image4,
image5, image5,
image6, image6,
image7, image7,
image8, image8
]
self.shuffle = [i for i in range(len(self.images))]
random.shuffle(self.shuffle)
self.create_grid(self.grid_size)


def create_grid(self, grid_size):
for row_num in range(grid_size):
new_row = self.create_row(row_num, grid_size)
self.grid.append(new_row)

def create_row(self, row_num, size):
tile_height = self.surface.get_height() // size
tile_width = self.surface.get_width() // (size+1)
one_row = [ ]
for col_num in range(size):
y = row_num * tile_height + 5
x = col_num * tile_width + 5
for i in range (self.grid_size):
one_tile = Tile(x, y, tile_width, tile_height, self.images[i], self.surface)
i += 1
one_row.append(one_tile)
return one_row

def play(self):
while not self.close_clicked:
self.handle_events()
self.draw()
if self.continue_game:
self.update()
self.decide_continue()
self.game_Clock.tick(self.FPS)

def handle_events(self):
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
self.close_clicked = True

def draw(self):
self.surface.fill(self.bg_color)
for row in self.grid:
for tile in row:
tile.draw()
pygame.display.update()

def update(self):
pass

def decide_continue(self):
pass


class Tile:
def __init__(self, pos_x, pos_y, tile_numx, tile_numy, imagelist, surface):
self.pos_x = pos_x
self.pos_y = pos_y
self.numx = tile_numx
self.numy = tile_numy
self.imagelist = imagelist
self.surface = surface

@classmethod
def set_surface(cls, surface):
cls.surface = surface

def draw(self):
x = 0
y = 0
screen.blit(pygame.image.load(self.imagelist),(self.pos_x,self.pos_y))



main()

最佳答案

主要问题是,混洗的图像包含在 self.shuffle 中。 , 而不是 self.images :
one_tile = Tile(x, y, tile_width, tile_height, self.images[i], self.surface)

one_tile = Tile(x, y, tile_width, tile_height, self.shuffle[i], self.surface) 

图片的索引可以通过 i = col_num*size + row_num来计算:

class Game:

# [...]

def create_grid(self, grid_size):
for row_num in range(grid_size):
new_row = self.create_row(row_num, grid_size)
self.grid.append(new_row)

def create_row(self, row_num, size):
tile_height = self.surface.get_height() // size
tile_width = self.surface.get_width() // (size+1)
one_row = [ ]
for col_num in range(size):
x, y = col_num * tile_width + 5, row_num * tile_height + 5
i = col_num*size + row_num
one_tile = Tile(x, y, tile_width, tile_height, self.shuffle[i], self.surface)
one_row.append(one_tile)
return one_row

备注 self.grid = [ [] ]是一个包含 1 个元素的列表,这是另一个列表。它必须是 self.grid = [] .

线

screen.blit(pygame.image.load(self.imagelist),(self.pos_x,self.pos_y))


会导致每次图像为 blit 时从文件中加载图像到 window 表面。这将获得性能影响。

预加载图像:

class Game:

def __init__(self, surface):
self.surface = surface
self.bg_color = pygame.Color('black')
self.FPS = 10
self.game_Clock = pygame.time.Clock()
self.close_clicked = False
self.continue_game = True
Tile.set_surface(self.surface)
self.grid_size = 4
self.grid = []

imgnames = ['./images/' + str(i) + '.jpg' for i in range(1,9)]
self.images = [pygame.image.load(name) for name in imgnames]
self.shuffle = self.images + self.images
random.shuffle(self.shuffle)

self.create_grid(self.grid_size)
blit装在 window 上的表面:

class Tile:
def __init__(self, pos_x, pos_y, tile_numx, tile_numy, image, surface):
self.pos_x = pos_x
self.pos_y = pos_y
self.numx = tile_numx
self.numy = tile_numy
self.image = image
self.surface = surface

@classmethod
def set_surface(cls, surface):
cls.surface = surface

def draw(self):
self.surface.blit(self.image,(self.pos_x,self.pos_y))

关于Python3 内存游戏随机洗牌图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59164645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com