また、動画を見ながらプログラムを作成していました。
そうするとエラーが出ました。主さんも、間違えているとはいっていましたが、どう直せば良いのか、まではいっていませんでした。
python
1import pygame 2from pygame.locals import * 3import sys 4 5SCREEN_RECT = Rect(0, 0, 640, 480) 6 7def load_image(filename): 8 image = pygame.image.load(filename) 9 image = image.convert_alpha() 10 return image 11 12def get_image(sheet, x, y, width, height, useColorKey=False): 13 image = pygame.Surface([width, height]) 14 image.blit(sheet, (0, 0), (x, y, width, height)) 15 image = image.convert() 16 if useColorKey: 17 colorkey = image.get_at((0, 0)) 18 image.set_colorkey(colorkey, RLEACCEL) 19 return image 20 21DIR_DOWN = 0 22DIR_LEFT = 1 23DIR_RIGHT = 2 24DIR_UP = 3 25ANIM_WAIT_COUNT = 24 26 27class Player(pygame.sprite.Sprite): 28 def __init__(self, filename): 29 pygame.sprite.Sprite.__init__(self) 30 sheet = load_image(filename) 31 self.images = [[], [], [], []] 32 for row in range(0, 4): 33 for col in [0, 1, 2, 1]: 34 self.images[row].append(get_image(sheet, 0 + 32 * col, 0 + 32 * row, 32, 32, True)) 35 self.image = self.images[DIR_DOWN][0] 36 self.rect = self.image.get_rect() 37 self.rect.center = (SCREEN_RECT.width//2, SCREEN_RECT.height//2) 38 self.frame = 0 39 self.anim_count = 0 40 self.dir = DIR_DOWN 41 def update(self): 42 self.anim_count += 1 43 if self.anim_count >= ANIM_WAIT_COUNT: 44 self.anim_count = 0 45 self.frame += 1 46 if self.frame > 3: 47 self.frame = 0 48 self.image = self.images[self.dir][self.frame] 49 50 51class Map: 52 def __init__(self, screen, filename): 53 self.ncol = 0 54 self.nrow = 0 55 self.screen = screen 56 self.mapData = [] 57 self.readMap(filename) 58 self.sheet0 = load_image("pipo-map001.png") 59 self.sheet1 = load_image("pipo-map001_atumi.png") 60 self.images = [] 61 self.images.append([self.sheet0, 0, 0]) 62 self.images.append([self.sheet1, 0, 4]) 63 def readMap(self, filename): 64 with open("field01.map") as fi: 65 line = fi.readline() 66 self.ncol, self.nrow = [int(tok) for tok in line.split(",")] 67 for row in range(self.nrow): 68 line = fi.readline() 69 self.mapData.append([int(tok) for tok in line.split(",")]) 70 def draw(self): 71 for row, rowData in enumerate(self.mapData): 72 for col, colData in enumerate(rowData): 73 sheet, x, y = self.images[colData] 74 self.screen.blit(sheet, (col * 32, row * 32), (x * 32, y * 32, 32, 32)) 75 76def main(): 77 pygame.init() 78 screen = pygame.display.set_mode(SCREEN_RECT.size) 79 pygame.display.set_caption("RPyG") 80 player = Player("pipo-charachip021.png") 81 group = pygame.sprite.RenderUpdates() 82 group.add(player) 83 fieldMap = Map(screen, "field01.map") 84 clock = pygame.time.Clock() 85 86 while True: 87 clock.tick(60) 88 screen.fill((0, 255, 0)) 89 fieldMap.draw() 90 group.update() 91 group.draw(screen) 92 pygame.display.update() 93 94 for event in pygame.event.get(): 95 if event.type == QUIT: 96 pygame.quit() 97 sys.exit() 98 elif event.type == KEYDOWN: 99 if event.key == K_ESCAPE: 100 pygame.quit() 101 sys.exit() 102 103if __name__ == '__main__': 104 main()
AttributeError: 'Map' object has no attribute 'readMap'
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。