Python 内置属性错误:';模块';对象没有属性';一块';
嗨,我一直从下面这行收到错误信息Python 内置属性错误:';模块';对象没有属性';一块';,python,pygame,attributeerror,Python,Pygame,Attributeerror,嗨,我一直从下面这行收到错误信息 pygame.one_tile.blit(self.i_list[img_index], pos) 这是来自下面的函数 def create_grid(self, grid_size): self.grid = [ ] tile_width = self.surface.get_width() / grid_size tile_height = self.surface.get_height() / grid_siz
pygame.one_tile.blit(self.i_list[img_index], pos)
这是来自下面的函数
def create_grid(self, grid_size):
self.grid = [ ]
tile_width = self.surface.get_width() / grid_size
tile_height = self.surface.get_height() / grid_size
# this for loop creates each row in our grid
for i in range(grid_size):
one_row = [ ]
img_index = 0
for j in range(grid_size):
y = i * tile_height
x = j * tile_width
pos = (x,y)
one_tile = Tile(pos, tile_width, tile_height, self.surface)
pygame.one_tile.blit(self.i_list[img_index], pos)
img_index += 1
one_row.append(one_tile)
self.grid.append(one_row)
我正在为记忆游戏版本1编写代码(这个游戏有8对图像,你需要记住哪张卡上的图像并匹配一对),我一直收到错误信息,但我没有;我真的不知道该怎么解决它。任何帮助都将不胜感激。非常感谢。
我的全部代码是
import pygame, random
# User-defined functions
def main():
# initialize all pygame modules (some need initialization)
pygame.init()
# create a pygame display window
pygame.display.set_mode((500, 400))
# set the title of the display window
pygame.display.set_caption('A template for graphical games with two moving dots')
# get the display surface
w_surface = pygame.display.get_surface()
# create a game object
game = Game(w_surface)
# start the main game loop by calling the play method on the game object
game.play()
# quit pygame and clean up the pygame window
pygame.quit()
# User-defined classes
class Game:
# An object in this class represents a complete game.
def __init__(self, surface):
# Initialize a Game.
# - self is the Game to initialize
# - surface is the display window surface object
# === objects that are part of every game that we will discuss
self.surface = surface
self.bg_color = pygame.Color('black')
self.FPS = 60
self.game_Clock = pygame.time.Clock()
self.close_clicked = False
self.continue_game = True
# === game specific objects
self.max_frames = 150
self.frame_counter = 0
self.i_list = []
self.images = ["image1.bmp", "image2.bmp", "image3.bmp", "image4.bmp", "image5.bmp", "image6.bmp", "image7.bmp", "image8.bmp"]
for i in self.images:
pygame.image.load(i)
self.i_list.append(i)
self.i_list.append(i)
random.shuffle(self.i_list)
self.create_grid(4)
def create_grid(self, grid_size):
self.grid = [ ]
tile_width = self.surface.get_width() / grid_size
tile_height = self.surface.get_height() / grid_size
# this for loop creates each row in our grid
for i in range(grid_size):
one_row = [ ]
img_index = 0
for j in range(grid_size):
y = i * tile_height
x = j * tile_width
pos = (x,y)
one_tile = Tile(pos, tile_width, tile_height, self.surface)
pygame.one_tile.blit(self.i_list[img_index], pos)
img_index += 1
one_row.append(one_tile)
self.grid.append(one_row)
def play(self):
# Play the game until the player presses the close box.
# - self is the Game that should be continued or not.
while not self.close_clicked: # until player clicks close box
# play frame
self.handle_events()
self.draw()
if self.continue_game:
self.update()
self.decide_continue()
self.game_Clock.tick(self.FPS) # run at most with FPS Frames Per Second
def handle_events(self):
# Handle each user event by changing the game state appropriately.
# - self is the Game whose events will be handled
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
self.close_clicked = True
def draw(self):
# Draw all game objects.
# - self is the Game to draw
self.surface.fill(self.bg_color) # clear the display surface first
for row in self.grid:
for tile in row:
tile.draw()
pygame.display.update()
def update(self):
# Update the game objects for the next frame.
# - self is the Game to update
pass
def decide_continue(self):
# Check and remember if the game should continue
# - self is the Game to check
return True
class Tile:
# A tile represents one location on a grid. Tiles hold content
# (in this case, an X or an O).
def __init__(self, screen_position, width, height, surface):
# initialize one instance of our Tile class. Tiles represent
# one 'position' in our tic-tac-toe board.
# - self: the tile being initialized
# - screen_position: the [x, y] coordinates to draw the tile at
# - surface: the surface on which to draw the tile
# - height: the height of the tile when it is drawn
# - width: the width of the tile when it is drawn
self.screen_position = screen_position
self.surface = surface
self.content = ''
self.height = height
self.width = width
def draw(self):
# draw the contents of a tile to its surface.
tile_boundary = pygame.Rect(self.screen_position[0],
self.screen_position[1],
self.width,
self.height)
pygame.draw.rect(self.surface, pygame.Color("white"), tile_boundary, 2)
main()
我建议为类
平铺添加.image
属性和.visible
属性。每个磁贴都知道关联的图像,如果磁贴上的图像可见,则每个磁贴都有一个状态:
类平铺:
#平铺表示栅格上的一个位置。磁贴保存内容
#(在本例中为X或O)。
定义初始(自身、屏幕位置、宽度、高度、表面、图像):
#初始化Tile类的一个实例。瓷砖代表
#在我们的tic tac趾板上有一个“位置”。
#-self:正在初始化的磁贴
#-屏幕位置:绘制瓷砖的[x,y]坐标
#-表面:绘制瓷砖的表面
#-高度:绘制瓷砖时的高度
#-宽度:绘制瓷砖时的宽度
self.screen\u position=屏幕位置
self.surface=曲面
self.content=“”
自我高度=高度
self.width=宽度
self.image=image
self.visible=False
def显示(自身,可见):
self.visible=可见
def牵引(自):
#将瓷砖的内容绘制到其表面。
tile_boundary=pygame.Rect(self.screen_位置[0],
自身屏幕位置[1],
自宽,
(身高)
pygame.draw.rect(self.surface,pygame.Color(“白色”),tile_边界,2)
如果自可见:
img_rect=self.image.get_rect(中心=tile_boundary.center)
self.surface.blit(self.image,img_rect.topleft)
要创建图像列表,必须加载图像。的返回值是可以附加到i\u列表中的对象
:
self.i_list=[]
self.images=[“image1.bmp”、“image2.bmp”、“image3.bmp”、“image4.bmp”、“image5.bmp”、“image6.bmp”、“image7.bmp”、“image8.bmp”]
对于self.images中的imgname:
img=pygame.image.load(imgname)
self.i_list.append(img)
随机洗牌(self.i_列表)
将图像传递给平铺的构造函数
:
范围内i的(网格大小):
一行=[]
img_指数=0
对于范围内的j(网格尺寸):
位置=(j*瓷砖宽度,i*瓷砖高度)
一块瓷砖=瓷砖(位置、瓷砖宽度、瓷砖高度、self.surface、self.i\u列表[img\u索引])
img_指数+=1
一行追加(一个平铺)
self.grid.append(一行)
注意,出于调试原因,您可以在Tiles
的构造函数中设置所有图像的所有状态为“可见”(self.visible=True
)。您想用blit实现什么?命令的语法不正确,但self.surface.blit(self.i\u list[img\u index],pos)
似乎正确。。。但这取决于你想做什么。游戏中有一个bug。\uuuu init\uuuu()
也是-代码正在丢失它加载的图像<代码>self.i_list.append(pygame.image.load(i))可能会更好。非常感谢!这真的帮了大忙!