weiter 发表于 2020-4-28 22:55:59

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

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

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

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

weiter 发表于 2020-4-28 22:56:49

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

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

import pygame
import sys
from pygame.locals import *
from random import *

#import console

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

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


def main():
    pygame.init()

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

    running = True

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

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

    balls = []

    #输入的变量
    for i in range(2):
      position = randint(0, width-96), randint(0, height-96)
      speed =
      ball = Ball(ball_image, position, speed)
      balls.append(ball)

    clock = pygame.time.Clock()
      
    while running:
      for event in pygame.event.get():
            if event.type == QUIt:
                sys.exit()

      screen.blit(background (0, 0))

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

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


if __name__ == "__main__":
    main()

weiter 发表于 2020-4-28 22:57:39

为什么会出现这个报错?(迷茫)

weiter 发表于 2020-4-28 23:11:08

import pygame
import sys
from pygame.locals import *
from random import *

#import console

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

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


def main():
    pygame.init()

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

    running = True

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

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

    balls = []

    #输入的变量
    for i in range(2):
      position = randint(0, width-96), randint(0, height-96)
      speed =
      ball = Ball(ball_image, position, speed)
      balls.append(ball)

    clock = pygame.time.Clock()
      
    while running:
      for event in pygame.event.get():
            if event.type == QUIt:
                sys.exit()

      screen.blit(background (0, 0))

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

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


if __name__ == "__main__":
    main()


这是源码(有小改)

xjy=lwy 发表于 2022-2-7 11:24:48

11行的self.image 写成了self,image

qq1151985918 发表于 2022-2-7 13:00:46

这都啥时候的帖子
页: [1]
查看完整版本: pygame“玩个球”教程(来自第一版)第一次测试报错