Python3内存游戏无法在表面上显示图像

2024-04-26 10:49:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在pygame中编写一个名为“记忆”的游戏。对我来说,一切似乎都是对的,但它无法在表面上显示图像。我想有两对9个随机顺序的图像出现在表面上。但是,它是全黑色的,而不是随机排列的两对9幅图像。有人,请帮帮我。多谢各位

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 = []
      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) 



   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.shuffle[i], self.surface)
            i = col_num*size + row_num
         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, 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):
      x = 0
      y = 0
      screen.blit(self.image,(self.pos_x,self.pos_y))



main()

Tags: inposselfgameforsizedefdisplay
1条回答
网友
1楼 · 发布于 2024-04-26 10:49:42

Game的构造函数中缺少对create_grid的调用:

class Game:

   def __init__(self, surface):
       # [...]

       self.create_grid(self.grid_size)

create_grid/create_row中有太多的on循环create_grid有一个循环,用于创建行。因此create_row需要一个循环来创建行中的列:

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):
         y = row_num * tile_height + 5
         x = col_num * tile_width + 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

相关问题 更多 >