18279773861 发表于 2018-12-4 09:34:34

求解决!!!我根据小甲鱼的动画精灵那一节一起敲的代码,但是一直有错误

求解决!!!{:10_257:} 我根据小甲鱼的动画精灵那一节一起敲的代码,但是一直有错误。
这是代码


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

#球类继承自Spirite 类
class Ball(pygame.sprite.Sprite):
    def __int__(self,image,position,speed,bg_size):
      #初始化动画精灵
      pygeme.sprite.Sprite.__init__(self)

      self.image = pygame.image.load(image).covert_alpha()
      self.rect = self.image_rect()
      #将小球放在指定位置
      self.rect.left, self.rect.top = position
      self.speed = speed
      self.width, self.height = bg_size, bg_size

    def move (self):
      self.rect = self.rect.move(self.speed)

      #如果小球的左侧出了边界,那么将小球左侧的位置改为右侧的边界
      if self.rect.right < 0:
            self,rect.left = self.width

      elif self.rect.left > self.width:
            self.rect.right = 0

      elif self.rect.bottom < 0:
            self.rect.right.top = self.height

      elif self.rect.top > self.height:
            self.rect.bottom = 0

def collide_check(item,target):
    col_balls = []
    for each in target:
      distance = math.sqrt(\
            math.pow((item.center - each.rect.center),2) + \
            math.pow((item.center - each.rect.center),2))      
      if distance <= (item.rect.width + each.rect.width)/ 2:
            col_ball.append(each)

    return col_balls


def main():
    pygame.init()

    ball_image = "gray_ball.png"
    bg_image = "background.png"

    running = True

    #根据背景图片指定游戏界面尺寸
    bg_size = width, height = 1024, 681
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("Play the ball - FishC Demo")

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

    #用来存放小球对象的列表
    balls = []

    #创建小球
    BALL_NUM = 5
    for i in range (BALL_NUM):
      # 位置随机,速度随机
      position = randint(0,width-100), randint(0,height-100)
      speed =
      ball = Ball(ball_image, position, speed, bg_size)
      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:
            each.move()
            screen.blit(each.image,each.rect)

      for i in range (BALL_NUM):
            item = balls.pop(i)

            if collide_check(item,balls):
                item.speed = -item.speed
                item.speed = -item.speed

            ball.insert(i,item)


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


if __name__ =="__main__":
    main()

报错是



File "C:\Users\LY\Desktop\气泡.py", line 104, in <module>
    main()
File "C:\Users\LY\Desktop\气泡.py", line 72, in main
    ball = Ball(ball_image, position, speed, bg_size)
File "C:\Users\LY\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sprite.py", line 124, in __init__
    self.add(*groups)
File "C:\Users\LY\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sprite.py", line 142, in add
    self.add(*group)
File "C:\Users\LY\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sprite.py", line 142, in add
    self.add(*group)
File "C:\Users\LY\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pygame\sprite.py", line 142, in add
    self.add(*group)

RecursionError: maximum recursion depth exceeded





检查了好几遍也没发现哪里有问题,希望各位帮帮忙{:10_254:}

塔利班 发表于 2018-12-4 09:41:07

本帖最后由 塔利班 于 2018-12-4 09:49 编辑

pygeme.sprite.Sprite.__init__(self)

pygame拼错了
init拼错了

18279773861 发表于 2018-12-4 10:00:15

塔利班 发表于 2018-12-4 09:41
pygeme.sprite.Sprite.__init__(self)

pygame拼错了


改了后还是那些错误{:10_269:}

塔利班 发表于 2018-12-4 10:02:37

18279773861 发表于 2018-12-4 10:00
改了后还是那些错误

init你拼错了,写成int了

18279773861 发表于 2018-12-4 11:12:36

塔利班 发表于 2018-12-4 10:02
init你拼错了,写成int了

那些改完后剩下这些
Traceback (most recent call last):
File "C:\Users\LY\Desktop\气泡.py", line 106, in <module>
    main()
File "C:\Users\LY\Desktop\气泡.py", line 72, in main
    ball = Ball(ball_image, position, speed, bg_size)
File "C:\Users\LY\Desktop\气泡.py", line 13, in __init__
    self.image = pygame.image.load(image).convert_alpha()
pygame.error: Couldn't open gray_ball.png

塔利班 发表于 2018-12-4 11:26:58

提示打不开这个图片,请确认名称有无,路径要是不和Py文件一个目录写全路径

TCY 发表于 2018-12-4 12:53:03

PS:下次要用代码格式!

Uuuuup 发表于 2018-12-4 15:31:43

尝试用一下pycharm这个软件写代码,语法有错误的话都会有提示,我跑了你的代码,只是缺少背景图片,其余的错误都找到了

一去不回 发表于 2020-5-27 09:42:51

   if self.rect.right < 0:
            self,rect.left = self.width
这一句错了,应该是self.rect.eft   你写的是逗号
{:10_249:}
页: [1]
查看完整版本: 求解决!!!我根据小甲鱼的动画精灵那一节一起敲的代码,但是一直有错误