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 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() 为什么会出现这个报错?(迷茫) 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()
这是源码(有小改) 11行的self.image 写成了self,image 这都啥时候的帖子
页:
[1]