前提・実現したいこと
pyxelでインベーダーゲームを作っています。
下のエラーメッセージを改善しようと書き換えたところ、実行しても何も起こらなくなってしまいました。アドバイスお願いします。
発生している問題・エラーメッセージ
File "/Users/higashirinako/Downloads/pyxel_me/a.py", line 78 playerX += playerX_change ^ IndentationError: unindent does not match any outer indentation level
該当のソースコード
Python
1import pyxel 2 3class App: 4 def __init__(self): 5 pyxel.init(160, 120, caption="Invadors game", 6 palette=[1],quit_key=pyxel.KEY_NONE) 7#Player 8class player: 9 def __init__(self): 10 pyxel.image(0).load(0, 0, "./player.png") 11 playerX,playerY = 370, 480 12 playerX_change = 0 13class sound: 14 def __init__(self): 15 self.music_player = MusicPlayer('laser.wav') 16 17#kadai 18class kadai: 19 def __init__(self): 20 pyxel.image(1).load(0, 0, "./kadai.png") 21 kadaiX = random.randint(0, 736) 22 kadaiY = random.randint(50, 150) 23 kadaiX_change, kadaiY_change = 4, 40 24 25#Bullet 26class Bullet: 27 def __init__(self): 28 pyxel.image(2).load(0, 0, "./bullet.png") 29 bulletX, bulletY = 0, 480 30 bulletX_change, bulletY_change = 0, 3 31 bullet_state = 'ready' 32 33 34#Score 35class score: 36 score_value = 0 37 38def player(x, y): 39 screen.blit(playerImg, (x, y)) 40 41def kadai(x, y): 42 screen.blit(kadaiImg, (x, y)) 43 44def fire_bullet(x, y): 45 global bullet_state 46 bullet_state = 'fire' 47 screen.blit(bulletImg, (x + 16, y + 10)) 48 49def isCollision(kadaiX, kadaiY, bulletX, bulletY): 50 distance = math.sqrt(math.pow(kadaiX - bulletX, 2) + math.pow(kadaiY - bulletY, 2)) 51 if distance < 27: 52 return true 53 else: 54 return False 55 56def __init__(self): 57 running = True 58 while running: 59 screen.fill((0, 0, 0)) 60 font = pyxel.font.SysFont(None, 80) 61 message = font.render('Hello World', False, (255, 255, 255)) 62 screen.blit(message, (20,50)) 63 for event in pyxel.event.get(): 64 if event.type == pyxel.QUIT: 65 running = False 66 if event.type == pyxel.KEYDOWN: 67 if event.key == pyxel.K_LEFT: 68 playerX_change = -1.5 69 if event.key == pyxel.K_RIGHT: 70 playerX_change = 1.5 71 if event.key == pyxel.K_SPACE: 72 if bullet_state == 'ready': 73 bulletX = playerX 74 fire_bullet(bulletX, bulletY) 75 76 if event.type == pyxel.KEYUP: 77 if event.key == pyxel.K_LEFT or event.key == pyxel.K_RIGHT: 78 playerX_change = 0 79 80 #Player 81def __init__(player): 82 playerX += playerX_change 83 if playerX <= 0: 84 playerX = 0 85 elif playerX >= 736: 86 playerX = 736 87 88 #kadai 89 if kadai > 440: 90 pass 91 kadaiX += kadaiX_change 92 if kadaiX <= 0: 93 kadaiX_change = 4 #go right if come to the leftend 94 kadaiY += kadaiY_change 95 elif kadaiX >= 736: 96 kadaiX_change = -4 #go left if come to the rightend 97 kadaiY += kadaiY_change 98 99 collision = isCollision(kadaiX, kadaiY, bulletX, bulletY) 100 if collosion: 101 bulletY = 480 102 bullet_state = 'ready' 103 score_value += 1 104 kadaiX = random.randint(0, 736) 105 kadaiY = random.randint(50, 150) 106 107 #bullet Movement 108 if bulletY <=0: 109 bulletY = 480 110 bullet_state = 'ready' 111 112 if bullet_state == 'fire': 113 fire_bullet(bulletX, bulletY) 114 bulletY -= bulletY_change 115 116 117 118 #Score 119 font = pyxel.font.SysFont(None, 32) #creating fonts 'None' is default 120 score = font.render("Score: {str(score_value)}", True, (255,255,255)) 121 screen.blit(score, (20,50)) 122 123 124 player(playerX, playerY) 125 kadai(kadaiX, kadaiY) 126 127 pyxel.display.update() 128 129App()
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
最終目標としては、Pygameで書かれたコードを同じ動作をするようにpyxelで書き換えることです。
Pygame
1import pygame 2from pygame import mixer 3import random 4import math 5 6 7pygame.init() 8 9 10screen = pygame.display.set_mode((800, 600)) 11screen.fill((150,150,150)) 12pygame.display.set_caption('Invaders game') 13 14 15#Player 16playerimg = pygame.image.load('player.png') 17playerX,playerY = 370, 480 18playerX_change = 0 19 20 21#mixer.Sound('laser.wav').play() 22 23 24#Enemy 25enemyImg = pygame.image.load('enemy.png') 26enemyX = random.randint(0, 736) 27enemyY = random.randint(50, 150) 28enemyX_change, enemyY_change = 4, 40 29 30 31#Bullet 32bulletImg = pygame.image.load('bullet.png') 33bulletX, bulletY = 0, 480 34bulletX_change, bulletY_change = 0, 3 35bullet_state = 'ready' 36 37 38 39 40 41 42#Score 43score_value = 0 44 45 46def player(x, y): 47 screen.blit(playerImg, (x, y)) 48 49 50def enemy(x, y): 51 screen.blit(enemyImg, (x, y)) 52 53def fire_bullet(x, y): 54 global bullet_state 55 bullet_state = 'fire' 56 screen.blit(bulletImg, (x + 16, y + 10)) 57 58def isCollision(enemyX, enemyY, bulletX, bulletY): 59 distance = math.sqrt(math.pow(enemyX - bulletX, 2) + math.pow(enemyY - bulletY, 2)) 60 if distance < 27: 61 return true 62 else: 63 return False 64 65 66running = True 67while running: 68 screen.fill((0, 0, 0)) 69 #font = pygame.font.SysFont(None, 80) 70 #message = font.render('Hello World', False, (255, 255, 255)) 71 #screen.blit(message, (20,50)) 72 for event in pygame.event.get(): 73 if event.type == pygame.QUIT: 74 running = False 75if event.type == pygame.KEYDOWN: 76 if event.key == pygame.K_LEFT: 77 playerX_change = -1.5 78 if event.key == pygame.K_RIGHT: 79 playerX_change = 1.5 80 if event.key == pygame.K_SPACE: 81 if bullet_state is 'ready': 82 bulletX = playerX 83 fire_bullet(bulletX, bulletY) 84 85 if event.type == pygame.KEYUP: 86 if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: 87 playerX_change = 0 88 89 #Player 90 playerX += playerX_change 91 if playerX <= 0: 92 playerX = 0 93 elif playerX >= 736: 94 playerX = 736 95 96 #Enemy 97 if enemy > 440: 98 break 99 enemyX += enemyX_change 100 if enemyX <= 0: 101 enemyX_change = 4 #go right if come to the leftend 102 enemyY += enemyY_change 103 elif enemyX >= 736: 104 enemyX_change = -4 #go left if come to the rightend 105 enemyY += enemyY_change 106 107 collision = isCollision(enemyX, enemyY, bulletX, bulletY) 108 if collosion: 109 bulletY = 480 110 bullet_state = 'ready' 111 score_value += 1 112 enemyX = random.randint(0, 736) 113 enemyY = random.randint(50, 150) 114 115 #bullet Movement 116 if bulletY <=0: 117 bulletY = 480 118 bullet_state = 'ready' 119 120 if bullet_state is 'fire': 121 fire_bullet(bulletX, bulletY) 122 bulletY -= bulletY_change 123 124 125 126 #Score 127 font = pygame.font.SysFont(None, 32) #creating fonts 'None' is default 128 score = font.render("Score: {str(score_value)}", True, (255,255,255)) 129 screen.blit(score, (20,50)) 130 131 132 player(playerX, playerY) 133 enemy(enemyX, enemyY) 134 135 pygame.display.update()
回答2件
あなたの回答
tips
プレビュー