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

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

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

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

import

自身のプラットフォーム・プログラム・データセットに対して、外部ソースを取り込むプロセスをimportと呼びます。

Python

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

Q&A

1回答

1636閲覧

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

MM_LL

総合スコア18

Visual Studio Code

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

import

自身のプラットフォーム・プログラム・データセットに対して、外部ソースを取り込むプロセスをimportと呼びます。

Python

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

0グッド

0クリップ

投稿2022/04/07 09:51

編集2022/04/07 23:40

python

1import pyxel 2 3SCENE_TITLE = 0 4SCENE_PLAY = 1 5SCENE_GAMEOVER = 2 6 7NUM_STARS = 100 8STAR_COLOR_HIGH = 12 9STAR_COLOR_LOW = 5 10 11PLAYER_WIDTH = 8 12PLAYER_HEIGHT = 8 13PLAYER_SPEED = 2 14 15BULLET_WIDTH = 2 16BULLET_HEIGHT = 8 17BULLET_COLOR = 11 18BULLET_SPEED = 4 19 20ENEMY_WIDTH = 8 21ENEMY_HEIGHT = 8 22ENEMY_SPEED = 1.5 23 24BLAST_START_RADIUS = 1 25BLAST_END_RADIUS = 8 26BLAST_COLOR_IN = 7 27BLAST_COLOR_OUT = 10 28 29enemies = [] 30bullets = [] 31blasts = [] 32 33 34def update_list(list): 35 for elem in list: 36 elem.update() 37 38 39def draw_list(list): 40 for elem in list: 41 elem.draw() 42 43 44def cleanup_list(list): 45 i = 0 46 while i < len(list): 47 elem = list[i] 48 if not elem.is_alive: 49 list.pop(i) 50 else: 51 i += 1 52 53 54class Background: 55 def __init__(self): 56 self.stars = [] 57 for i in range(NUM_STARS): 58 self.stars.append( 59 ( 60 pyxel.rndi(0, pyxel.width - 1), 61 pyxel.rndi(0, pyxel.height - 1), 62 pyxel.rndf(1, 2.5), 63 ) 64 ) 65 66 def update(self): 67 for i, (x, y, speed) in enumerate(self.stars): 68 y += speed 69 if y >= pyxel.height: 70 y -= pyxel.height 71 self.stars[i] = (x, y, speed) 72 73 def draw(self): 74 for (x, y, speed) in self.stars: 75 pyxel.pset(x, y, STAR_COLOR_HIGH if speed > 1.8 else STAR_COLOR_LOW) 76 77 78class Player: 79 def __init__(self, x, y): 80 self.x = x 81 self.y = y 82 self.w = PLAYER_WIDTH 83 self.h = PLAYER_HEIGHT 84 self.is_alive = True 85 86 def update(self): 87 if pyxel.btn(pyxel.KEY_LEFT): 88 self.x -= PLAYER_SPEED 89 if pyxel.btn(pyxel.KEY_RIGHT): 90 self.x += PLAYER_SPEED 91 if pyxel.btn(pyxel.KEY_UP): 92 self.y -= PLAYER_SPEED 93 if pyxel.btn(pyxel.KEY_DOWN): 94 self.y += PLAYER_SPEED 95 self.x = max(self.x, 0) 96 self.x = min(self.x, pyxel.width - self.w) 97 self.y = max(self.y, 0) 98 self.y = min(self.y, pyxel.height - self.h) 99 100 if pyxel.btnp(pyxel.KEY_SPACE): 101 Bullet( 102 self.x + (PLAYER_WIDTH - BULLET_WIDTH) / 2, self.y - BULLET_HEIGHT / 2 103 ) 104 pyxel.play(0, 0) 105 106 def draw(self): 107 pyxel.blt(self.x, self.y, 0, 0, 0, self.w, self.h, 0) 108 109 110class Bullet: 111 def __init__(self, x, y): 112 self.x = x 113 self.y = y 114 self.w = BULLET_WIDTH 115 self.h = BULLET_HEIGHT 116 self.is_alive = True 117 bullets.append(self) 118 119 def update(self): 120 self.y -= BULLET_SPEED 121 if self.y + self.h - 1 < 0: 122 self.is_alive = False 123 124 def draw(self): 125 pyxel.rect(self.x, self.y, self.w, self.h, BULLET_COLOR) 126 127 128class Enemy: 129 def __init__(self, x, y): 130 self.x = x 131 self.y = y 132 self.w = ENEMY_WIDTH 133 self.h = ENEMY_HEIGHT 134 self.dir = 1 135 self.timer_offset = pyxel.rndi(0, 59) 136 self.is_alive = True 137 enemies.append(self) 138 139 def update(self): 140 if (pyxel.frame_count + self.timer_offset) % 60 < 30: 141 self.x += ENEMY_SPEED 142 self.dir = 1 143 else: 144 self.x -= ENEMY_SPEED 145 self.dir = -1 146 self.y += ENEMY_SPEED 147 if self.y > pyxel.height - 1: 148 self.is_alive = False 149 150 def draw(self): 151 pyxel.blt(self.x, self.y, 0, 8, 0, self.w * self.dir, self.h, 0) 152 153 154class Blast: 155 def __init__(self, x, y): 156 self.x = x 157 self.y = y 158 self.radius = BLAST_START_RADIUS 159 self.is_alive = True 160 blasts.append(self) 161 162 def update(self): 163 self.radius += 1 164 if self.radius > BLAST_END_RADIUS: 165 self.is_alive = False 166 167 def draw(self): 168 pyxel.circ(self.x, self.y, self.radius, BLAST_COLOR_IN) 169 pyxel.circb(self.x, self.y, self.radius, BLAST_COLOR_OUT) 170 171 172class App: 173 def __init__(self): 174 pyxel.init(120, 160, title="Pyxel Shooter") 175 pyxel.image(0).set( 176 0, 177 0, 178 [ 179 "00c00c00", 180 "0c7007c0", 181 "0c7007c0", 182 "c703b07c", 183 "77033077", 184 "785cc587", 185 "85c77c58", 186 "0c0880c0", 187 ], 188 ) 189 pyxel.image(0).set( 190 8, 191 0, 192 [ 193 "00088000", 194 "00ee1200", 195 "08e2b180", 196 "02882820", 197 "00222200", 198 "00012280", 199 "08208008", 200 "80008000", 201 ], 202 ) 203 pyxel.sound(0).set("a3a2c1a1", "p", "7", "s", 5) 204 pyxel.sound(1).set("a3a2c2c2", "n", "7742", "s", 10) 205 self.scene = SCENE_TITLE 206 self.score = 0 207 self.background = Background() 208 self.player = Player(pyxel.width / 2, pyxel.height - 20) 209 pyxel.run(self.update, self.draw) 210 211 def update(self): 212 if pyxel.btn(pyxel.KEY_Q): 213 pyxel.quit() 214 215 self.background.update() 216 if self.scene == SCENE_TITLE: 217 self.update_title_scene() 218 elif self.scene == SCENE_PLAY: 219 self.update_play_scene() 220 elif self.scene == SCENE_GAMEOVER: 221 self.update_gameover_scene() 222 223 def update_title_scene(self): 224 if pyxel.btnp(pyxel.KEY_RETURN): 225 self.scene = SCENE_PLAY 226 227 def update_play_scene(self): 228 if pyxel.frame_count % 6 == 0: 229 Enemy(pyxel.rndi(0, pyxel.width - ENEMY_WIDTH), 0) 230 231 for enemy in enemies: 232 for bullet in bullets: 233 if ( 234 enemy.x + enemy.w > bullet.x 235 and bullet.x + bullet.w > enemy.x 236 and enemy.y + enemy.h > bullet.y 237 and bullet.y + bullet.h > enemy.y 238 ): 239 enemy.is_alive = False 240 bullet.is_alive = False 241 blasts.append( 242 Blast(enemy.x + ENEMY_WIDTH / 2, enemy.y + ENEMY_HEIGHT / 2) 243 ) 244 pyxel.play(1, 1) 245 self.score += 10 246 247 for enemy in enemies: 248 if ( 249 self.player.x + self.player.w > enemy.x 250 and enemy.x + enemy.w > self.player.x 251 and self.player.y + self.player.h > enemy.y 252 and enemy.y + enemy.h > self.player.y 253 ): 254 enemy.is_alive = False 255 blasts.append( 256 Blast( 257 self.player.x + PLAYER_WIDTH / 2, 258 self.player.y + PLAYER_HEIGHT / 2, 259 ) 260 ) 261 pyxel.play(1, 1) 262 self.scene = SCENE_GAMEOVER 263 264 self.player.update() 265 update_list(bullets) 266 update_list(enemies) 267 update_list(blasts) 268 cleanup_list(enemies) 269 cleanup_list(bullets) 270 cleanup_list(blasts) 271 272 def update_gameover_scene(self): 273 update_list(bullets) 274 update_list(enemies) 275 update_list(blasts) 276 cleanup_list(enemies) 277 cleanup_list(bullets) 278 cleanup_list(blasts) 279 280 if pyxel.btnp(pyxel.KEY_RETURN): 281 self.scene = SCENE_PLAY 282 self.player.x = pyxel.width / 2 283 self.player.y = pyxel.height - 20 284 self.score = 0 285 enemies.clear() 286 bullets.clear() 287 blasts.clear() 288 289 def draw(self): 290 pyxel.cls(0) 291 self.background.draw() 292 if self.scene == SCENE_TITLE: 293 self.draw_title_scene() 294 elif self.scene == SCENE_PLAY: 295 self.draw_play_scene() 296 elif self.scene == SCENE_GAMEOVER: 297 self.draw_gameover_scene() 298 pyxel.text(39, 4, f"SCORE {self.score:5}", 7) 299 300 def draw_title_scene(self): 301 pyxel.text(35, 66, "Pyxel Shooter", pyxel.frame_count % 16) 302 pyxel.text(31, 126, "- PRESS ENTER -", 13) 303 304 def draw_play_scene(self): 305 self.player.draw() 306 draw_list(bullets) 307 draw_list(enemies) 308 draw_list(blasts) 309 310 def draw_gameover_scene(self): 311 draw_list(bullets) 312 draw_list(enemies) 313 draw_list(blasts) 314 pyxel.text(43, 66, "GAME OVER", 8) 315 pyxel.text(31, 126, "- PRESS ENTER -", 13) 316 317 318App() 319

【シューティングゲーム】を作成にあたり、不明点がありご回答いただきたいです。
下記URL貼っていますので、確認いただきたいです。

コード1行目 import pyxel の pyxelに波線が出ます。
エラー内容は、import "pyxel" could not be resolved pylance(reportMissingImoports)
と、表示されています。

pipインストール済みです。
上記のコードは、サンプルコード通りになっています。

数日試行錯誤しましたが、まだ波線を消せずにいます。
ご教授いただければ幸いです。
よろしくお願いします。

URL

【基本情報】
・開発環境 Visual Studio Code
・言語 python
・Windows10

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

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

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

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

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

jbpb0

2022/04/07 14:40 編集

> エラー内容は、import "pyxel" could not be resolved pylance(reportMissingImoports) は、pythonのエラーではなく、pylanceが出してるエラーメッセージです > pipインストール済み https://3pysci.com/vscode-2/https://qiita.com/hruc/items/183a9d525447914f59b9 に書かれてるケースのように、pylanceがエラーメッセージを出しても、pythonは正常に実行できる場合もあります エラーメッセージを無視してpythonコードを実行したら、pyxelがインポートできたりしませんか?
guest

回答1

0

一言で言ってしまうと、VS CodeのPythonから見たときに pyxel に対してPATHが通っていません。

これ以上は環境がわからないので何とも言えないですが、適当に2つくらい可能性がありそうな事を書いておきます。
違う場合には 質問を編集して 環境を追記してください。

投稿2022/04/07 12:54

yamap55

総合スコア1376

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問