質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1276閲覧

お助けください。python import エラー

MM_LL

総合スコア18

Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/04/10 03:48

編集2022/04/10 03:51

python

1 2import pyxel 3 4SCENE_TITLE = 0 5SCENE_PLAY = 1 6SCENE_GAMEOVER = 2 7 8NUM_STARS = 100 9STAR_COLOR_HIGH = 12 10STAR_COLOR_LOW = 5 11 12PLAYER_WIDTH = 8 13PLAYER_HEIGHT = 8 14PLAYER_SPEED = 2 15 16BULLET_WIDTH = 2 17BULLET_HEIGHT = 8 18BULLET_COLOR = 11 19BULLET_SPEED = 4 20 21ENEMY_WIDTH = 8 22ENEMY_HEIGHT = 8 23ENEMY_SPEED = 1.5 24 25BLAST_START_RADIUS = 1 26BLAST_END_RADIUS = 8 27BLAST_COLOR_IN = 7 28BLAST_COLOR_OUT = 10 29 30enemies = [] 31bullets = [] 32blasts = [] 33 34 35def update_list(list): 36 for elem in list: 37 elem.update() 38 39 40def draw_list(list): 41 for elem in list: 42 elem.draw() 43 44 45def cleanup_list(list): 46 i = 0 47 while i < len(list): 48 elem = list[i] 49 if not elem.is_alive: 50 list.pop(i) 51 else: 52 i += 1 53 54 55class Background: 56 def __init__(self): 57 self.stars = [] 58 for i in range(NUM_STARS): 59 self.stars.append( 60 ( 61 pyxel.rndi(0, pyxel.width - 1), 62 pyxel.rndi(0, pyxel.height - 1), 63 pyxel.rndf(1, 2.5), 64 ) 65 ) 66 67 def update(self): 68 for i, (x, y, speed) in enumerate(self.stars): 69 y += speed 70 if y >= pyxel.height: 71 y -= pyxel.height 72 self.stars[i] = (x, y, speed) 73 74 def draw(self): 75 for (x, y, speed) in self.stars: 76 pyxel.pset(x, y, STAR_COLOR_HIGH if speed > 1.8 else STAR_COLOR_LOW) 77 78 79class Player: 80 def __init__(self, x, y): 81 self.x = x 82 self.y = y 83 self.w = PLAYER_WIDTH 84 self.h = PLAYER_HEIGHT 85 self.is_alive = True 86 87 def update(self): 88 if pyxel.btn(pyxel.KEY_LEFT): 89 self.x -= PLAYER_SPEED 90 if pyxel.btn(pyxel.KEY_RIGHT): 91 self.x += PLAYER_SPEED 92 if pyxel.btn(pyxel.KEY_UP): 93 self.y -= PLAYER_SPEED 94 if pyxel.btn(pyxel.KEY_DOWN): 95 self.y += PLAYER_SPEED 96 self.x = max(self.x, 0) 97 self.x = min(self.x, pyxel.width - self.w) 98 self.y = max(self.y, 0) 99 self.y = min(self.y, pyxel.height - self.h) 100 101 if pyxel.btnp(pyxel.KEY_SPACE): 102 Bullet( 103 self.x + (PLAYER_WIDTH - BULLET_WIDTH) / 2, self.y - BULLET_HEIGHT / 2 104 ) 105 pyxel.play(0, 0) 106 107 def draw(self): 108 pyxel.blt(self.x, self.y, 0, 0, 0, self.w, self.h, 0) 109 110 111class Bullet: 112 def __init__(self, x, y): 113 self.x = x 114 self.y = y 115 self.w = BULLET_WIDTH 116 self.h = BULLET_HEIGHT 117 self.is_alive = True 118 bullets.append(self) 119 120 def update(self): 121 self.y -= BULLET_SPEED 122 if self.y + self.h - 1 < 0: 123 self.is_alive = False 124 125 def draw(self): 126 pyxel.rect(self.x, self.y, self.w, self.h, BULLET_COLOR) 127 128 129class Enemy: 130 def __init__(self, x, y): 131 self.x = x 132 self.y = y 133 self.w = ENEMY_WIDTH 134 self.h = ENEMY_HEIGHT 135 self.dir = 1 136 self.timer_offset = pyxel.rndi(0, 59) 137 self.is_alive = True 138 enemies.append(self) 139 140 def update(self): 141 if (pyxel.frame_count + self.timer_offset) % 60 < 30: 142 self.x += ENEMY_SPEED 143 self.dir = 1 144 else: 145 self.x -= ENEMY_SPEED 146 self.dir = -1 147 self.y += ENEMY_SPEED 148 if self.y > pyxel.height - 1: 149 self.is_alive = False 150 151 def draw(self): 152 pyxel.blt(self.x, self.y, 0, 8, 0, self.w * self.dir, self.h, 0) 153 154 155class Blast: 156 def __init__(self, x, y): 157 self.x = x 158 self.y = y 159 self.radius = BLAST_START_RADIUS 160 self.is_alive = True 161 blasts.append(self) 162 163 def update(self): 164 self.radius += 1 165 if self.radius > BLAST_END_RADIUS: 166 self.is_alive = False 167 168 def draw(self): 169 pyxel.circ(self.x, self.y, self.radius, BLAST_COLOR_IN) 170 pyxel.circb(self.x, self.y, self.radius, BLAST_COLOR_OUT) 171 172 173class App: 174 def __init__(self): 175 pyxel.init(120, 160, title="Pyxel Shooter") 176 pyxel.image(0).set( 177 0, 178 0, 179 [ 180 "00c00c00", 181 "0c7007c0", 182 "0c7007c0", 183 "c703b07c", 184 "77033077", 185 "785cc587", 186 "85c77c58", 187 "0c0880c0", 188 ], 189 ) 190 pyxel.image(0).set( 191 8, 192 0, 193 [ 194 "00088000", 195 "00ee1200", 196 "08e2b180", 197 "02882820", 198 "00222200", 199 "00012280", 200 "08208008", 201 "80008000", 202 ], 203 ) 204 pyxel.sound(0).set("a3a2c1a1", "p", "7", "s", 5) 205 pyxel.sound(1).set("a3a2c2c2", "n", "7742", "s", 10) 206 self.scene = SCENE_TITLE 207 self.score = 0 208 self.background = Background() 209 self.player = Player(pyxel.width / 2, pyxel.height - 20) 210 pyxel.run(self.update, self.draw) 211 212 def update(self): 213 if pyxel.btn(pyxel.KEY_Q): 214 pyxel.quit() 215 216 self.background.update() 217 if self.scene == SCENE_TITLE: 218 self.update_title_scene() 219 elif self.scene == SCENE_PLAY: 220 self.update_play_scene() 221 elif self.scene == SCENE_GAMEOVER: 222 self.update_gameover_scene() 223 224 def update_title_scene(self): 225 if pyxel.btnp(pyxel.KEY_RETURN): 226 self.scene = SCENE_PLAY 227 228 def update_play_scene(self): 229 if pyxel.frame_count % 6 == 0: 230 Enemy(pyxel.rndi(0, pyxel.width - ENEMY_WIDTH), 0) 231 232 for enemy in enemies: 233 for bullet in bullets: 234 if ( 235 enemy.x + enemy.w > bullet.x 236 and bullet.x + bullet.w > enemy.x 237 and enemy.y + enemy.h > bullet.y 238 and bullet.y + bullet.h > enemy.y 239 ): 240 enemy.is_alive = False 241 bullet.is_alive = False 242 blasts.append( 243 Blast(enemy.x + ENEMY_WIDTH / 2, enemy.y + ENEMY_HEIGHT / 2) 244 ) 245 pyxel.play(1, 1) 246 self.score += 10 247 248 for enemy in enemies: 249 if ( 250 self.player.x + self.player.w > enemy.x 251 and enemy.x + enemy.w > self.player.x 252 and self.player.y + self.player.h > enemy.y 253 and enemy.y + enemy.h > self.player.y 254 ): 255 enemy.is_alive = False 256 blasts.append( 257 Blast( 258 self.player.x + PLAYER_WIDTH / 2, 259 self.player.y + PLAYER_HEIGHT / 2, 260 ) 261 ) 262 pyxel.play(1, 1) 263 self.scene = SCENE_GAMEOVER 264 265 self.player.update() 266 update_list(bullets) 267 update_list(enemies) 268 update_list(blasts) 269 cleanup_list(enemies) 270 cleanup_list(bullets) 271 cleanup_list(blasts) 272 273 def update_gameover_scene(self): 274 update_list(bullets) 275 update_list(enemies) 276 update_list(blasts) 277 cleanup_list(enemies) 278 cleanup_list(bullets) 279 cleanup_list(blasts) 280 281 if pyxel.btnp(pyxel.KEY_RETURN): 282 self.scene = SCENE_PLAY 283 self.player.x = pyxel.width / 2 284 self.player.y = pyxel.height - 20 285 self.score = 0 286 enemies.clear() 287 bullets.clear() 288 blasts.clear() 289 290 def draw(self): 291 pyxel.cls(0) 292 self.background.draw() 293 if self.scene == SCENE_TITLE: 294 self.draw_title_scene() 295 elif self.scene == SCENE_PLAY: 296 self.draw_play_scene() 297 elif self.scene == SCENE_GAMEOVER: 298 self.draw_gameover_scene() 299 pyxel.text(39, 4, f"SCORE {self.score:5}", 7) 300 301 def draw_title_scene(self): 302 pyxel.text(35, 66, "Pyxel Shooter", pyxel.frame_count % 16) 303 pyxel.text(31, 126, "- PRESS ENTER -", 13) 304 305 def draw_play_scene(self): 306 self.player.draw() 307 draw_list(bullets) 308 draw_list(enemies) 309 draw_list(blasts) 310 311 def draw_gameover_scene(self): 312 draw_list(bullets) 313 draw_list(enemies) 314 draw_list(blasts) 315 pyxel.text(43, 66, "GAME OVER", 8) 316 pyxel.text(31, 126, "- PRESS ENTER -", 13) 317 318 319App() 320 321```python 322 323【シューティングゲーム】を作成にあたり、不明点がありご回答いただきたいです。 324下記URL貼っていますので、確認いただきたいです。 325 326コード1行目 import pyxel の pyxelに波線が出ます。 327エラー内容は、import "pyxel" could not be resolved pylance(reportMissingImoports) 328と、表示されています。 329 330pipインストール済みです。 331上記のコードは、サンプルコード通りになっています。 332 333数日試行錯誤しましたが、まだ波線を消せずにいます。 334ご教授いただければ幸いです。 335よろしくお願いします。 336 337 338[URL](https://github.com/coding-youtuber/pyxel/blob/master/README.ja.md)URL 339 340 341【基本情報】 342・開発環境 Visual Studio Code 343・言語 python 344・Windows10URLURL

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

otn

2022/04/10 04:08

実行したらどうなるのでしょう?
shiracamus

2022/04/10 04:39 編集

pip か pip3 コマンドを使ってpyxelをインストールしましたか? visual studio code の python実行環境が別に存在するなら、そちら側にも。
guest

回答1

0

ベストアンサー

こちらの記事が参考になるかもしれません。
https://startlab.jp/learning-python/vscode-settings/

①VSCodeのターミナルで対話モードに入り、pyxelモジュールの場所を把握

shell

1$ python 2>>> import pyxel 3>>> print(pyxel.__file__) 4<モジュールのパスが出る>

②モジュールのパス(site-packagesまでの部分)をコピーして、VSCodeの[Python > Analysis: Extra Paths]設定にパスを追加する

投稿2022/04/10 11:11

consel

総合スコア74

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

MM_LL

2022/04/11 10:40

出来ました! ありがとうございます! 助かりました!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問