実現したいこと
前提
「フラッピーバード」というゲームをpygameで作っています(詳しくはフラッピーバードで調べていただければゲームの概要はわかると思います)
画面の右端から、それぞれ画面上端と画面下端に接した柱のようなブロックを生成して画面左に向かって等速で移動します。
発生している問題・エラーメッセージ
実行時は問題なく動きますが、ブロックが生成されるあるタイミングでエラーが発生してしまいます。
File ~\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals) File c:\users\yutot\onedrive\デスクトップ\hop game\main.py:21 game.run() File ~\OneDrive\デスクトップ\hop game\game.py:52 in run self.create_wall() File ~\OneDrive\デスクトップ\hop game\game.py:38 in create_wall self.wall_down = Wall(self.wall_group, s.screen_height - self.wall_height - s.hole_height, s.screen_width, self.wall_height + s.hole_height) File ~\OneDrive\デスクトップ\hop game\wall.py:11 in __init__ self.image = pygame.Surface((s.wall_width, h)) error: Invalid resolution for Surface
該当のソースコード
初心者なので、該当する可能性のあるコード全体を載せています。ご了承ください。
Python
1import pygame 2import random 3import setting as s 4from player import Player 5from wall import Wall 6 7class Game:8 def __init__(self):9 self.screen = pygame.display.get_surface()10 # グループ作成11 self.create_group()12 13 # プレイヤー14 player = Player(self.player_group, s.screen_width / 4, s.screen_height / 2)15 16 # 壁17 self.timer = 018 19 20 # 背景21 self.pre_bg_img = pygame.image.load('assets/img/background.jpg')22 self.bg_img = pygame.transform.scale(self.pre_bg_img, (s.screen_width, s.screen_height))23 self.bg_x = 024 self.scroll_speed = 0.525 26 def create_group(self):27 self.player_group = pygame.sprite.GroupSingle()28 self.wall_group = pygame.sprite.Group()29 30 def create_wall(self):31 self.timer += 132 if self.timer > 120:33 self.wall_height = random.randint(30, s.screen_height - 50 - 10)34 self.wall_up = Wall(self.wall_group, self.wall_height, s.screen_width, 0)35 self.wall_down = Wall(self.wall_group, s.screen_height - self.wall_height - s.hole_height, s.screen_width, self.wall_height + s.hole_height)36 self.timer = 037 38 def scroll_bg(self):39 self.bg_x = (self.bg_x - self.scroll_speed) % s.screen_width 40 self.screen.blit(self.bg_img, (self.bg_x - s.screen_width, 0))41 self.screen.blit(self.bg_img, (self.bg_x, 0))42 43 def run(self):44 self.scroll_bg()45 self.player_group.draw(self.screen)46 self.player_group.update()47 self.wall_group.draw(self.screen)48 self.wall_group.update()49 self.create_wall()50 51class Wall(pygame.sprite.Sprite):52 def __init__(self, groups, h, x, y):53 super().__init__(groups)54 self.screen = pygame.display.get_surface()55 56 # 画像57 self.image = pygame.Surface((s.wall_width, h))58 self.image.fill(s.BLUE)59 self.rect = self.image.get_rect(topleft = (x, y))60 61 # スピード62 self.speed = 363 64 def move(self):65 self.rect.x -= self.speed 66 67 def check_off_screen(self):68 if self.rect.right <= 0:69 self.kill()70 71 def update(self):72 self.move()73 self.check_off_screen()
試したこと
ブロックを画面中央から出現させて原因を探ろうとしたが、問題のブロックが生成される前にエラーが出るので意味がなかった。
0 コメント