鱼C论坛

 找回密码
 立即注册
查看: 1194|回复: 6

pygame“玩个球”教程(来自第一版)第一次测试报错

[复制链接]
发表于 2020-4-28 22:55:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 weiter 于 2020-4-28 23:03 编辑

如下是报错(需要放源代码吗?):

  1. Traceback (most recent call last):
  2.   File "C:\Users\q\Desktop\编程\编程\py\project_1\main.py", line 58, in <module>
  3.     main()
  4.   File "C:\Users\q\Desktop\编程\编程\py\project_1\main.py", line 38, in main
  5.     ball = Ball(ball_image, position, speed)
  6.   File "C:\Users\q\Desktop\编程\编程\py\project_1\main.py", line 12, in __init__
  7.     self,image = pygame.image.load(image).convert_alpha()
  8. TypeError: cannot unpack non-iterable pygame.Surface object
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-28 22:56:49 | 显示全部楼层
本帖最后由 weiter 于 2020-4-28 23:01 编辑

如下是源代码(因为是改写……所以有一些不同的地方)

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *

  5. #import console

  6. class Ball(pygame.sprite.Sprite):
  7.     def __init__(self, image, position, speed):
  8.         pygame.sprite.Sprite.__init__(self)

  9.         self,image = pygame.image.load(image).convert_alpha()
  10.         self.rect = self.image.get_rect()
  11.         self.rect.left, self.rect.top = position
  12.         self.speed = speed


  13. def main():
  14.     pygame.init()

  15.     ball_image = "healthy.png"
  16.     bg_image = "bg.png"

  17.     running = True

  18.     bg_size = width, height = 1024, 681
  19.     screen = pygame.display.set_mode(bg_size)
  20.     pygame.display.set_caption("感染模拟器")

  21.     backgruond = pygame.image.load(bg_image).convert_alpha()

  22.     balls = []

  23.     #输入的变量
  24.     for i in range(2):
  25.         position = randint(0, width-96), randint(0, height-96)
  26.         speed = [randint(-10, 10), randint(-10, 10)]
  27.         ball = Ball(ball_image, position, speed)
  28.         balls.append(ball)

  29.     clock = pygame.time.Clock()
  30.         
  31.     while running:
  32.         for event in pygame.event.get():
  33.             if event.type == QUIt:
  34.                 sys.exit()

  35.         screen.blit(background (0, 0))

  36.         for each in balls:
  37.             screen.blit(each.image, each.rect)

  38.         pygame.display.flip()
  39.         clock.tick(30)


  40. if __name__ == "__main__":
  41.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-28 22:57:39 | 显示全部楼层
为什么会出现这个报错?(迷茫)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-28 23:11:08 | 显示全部楼层
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *

  5. #import console

  6. class Ball(pygame.sprite.Sprite):
  7.     def __init__(self, image, position, speed):
  8.         pygame.sprite.Sprite.__init__(self)

  9.         self,image = pygame.image.load(image).convert_alpha()
  10.         self.rect = self.image.get_rect()
  11.         self.rect.left, self.rect.top = position
  12.         self.speed = speed


  13. def main():
  14.     pygame.init()

  15.     ball_image = "healthy.png"
  16.     bg_image = "bg.png"

  17.     running = True

  18.     bg_size = width, height = 1024, 681
  19.     screen = pygame.display.set_mode(bg_size)
  20.     pygame.display.set_caption("感染模拟器")

  21.     backgruond = pygame.image.load(bg_image).convert_alpha()

  22.     balls = []

  23.     #输入的变量
  24.     for i in range(2):
  25.         position = randint(0, width-96), randint(0, height-96)
  26.         speed = [randint(-10, 10), randint(-10, 10)]
  27.         ball = Ball(ball_image, position, speed)
  28.         balls.append(ball)

  29.     clock = pygame.time.Clock()
  30.         
  31.     while running:
  32.         for event in pygame.event.get():
  33.             if event.type == QUIt:
  34.                 sys.exit()

  35.         screen.blit(background (0, 0))

  36.         for each in balls:
  37.             screen.blit(each.image, each.rect)

  38.         pygame.display.flip()
  39.         clock.tick(30)


  40. if __name__ == "__main__":
  41.     main()
复制代码



这是源码(有小改)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-7 11:24:48 | 显示全部楼层
11行的self.image 写成了self,image
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-7 13:00:46 From FishC Mobile | 显示全部楼层
这都啥时候的帖子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 10:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表