鱼C论坛

 找回密码
 立即注册
查看: 1998|回复: 2

[已解决]按照小甲鱼的视频编写 play the ball 的时候报错

[复制链接]
发表于 2017-8-6 10:11:25 | 显示全部楼层 |阅读模式

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

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

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





  6. class Ball(pygame.sprite.Sprite):
  7.     def __init__(self,image,position,speed,bg_size):
  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.right = position
  12.         self.speed = speed
  13.         self.width ,self.height = bg_size
  14.     def move(self):
  15.         self.rect = self.rect.move(self.speed)
  16.         if self.rect.right < 0:
  17.             self.rect.left = self.width
  18.         elif self.rect.left > self.width:
  19.             self.rect.right = 0
  20.         elif self.rect.top > self.height:
  21.             self.rect.bottom = 0
  22.         elif self.rect.bottom < 0:
  23.             self.rect.top = self.height
  24. def collide_check(item,target):
  25.     for each in target:
  26.         col_balls =[]
  27.         distance = math.sqrt(math.pow((item.rect.center[0]-each.rect.center[0]),2)+
  28.                              math.pow((item.rect.center[1] - each.rect.center[1]), 2))
  29.         if distance <= item.rect.width:
  30.             col_balls.append(each)
  31.     return col_balls



  32. def main():
  33.     pygame.init()

  34.     ball_image = "gray_ball.png"
  35.     bg_image = "background.png"
  36.     bg_size = width,height = 1024,681
  37.     screen  = pygame.display.set_mode(bg_size)
  38.     pygame.display.set_caption("Play the ball")
  39.     background = pygame.image.load(bg_image).convert_alpha()

  40.     balls= []
  41.     for i in range(5):
  42.         position = randint(0,width-100),randint(0,height-100)
  43.         speed = [randint(-10,10),randint(-10,10)]
  44.         ball = Ball(ball_image,position,speed,bg_size)
  45.         while collide_check(ball,balls):
  46.             ball.rect.left, ball.rect.right = randint(0,width-100),randint(0,height-100)

  47.         balls.append(ball)
  48.     clock = pygame.time.Clock()

  49.     running = True
  50.     while running:
  51.         for event in pygame.event.get():
  52.             if event.type == QUIT:
  53.                 sys.exit()
  54.         screen.blit(background,(0,0))

  55.         for each in balls:
  56.             each.move()
  57.             screen.blit(each.image,each.rect)
  58.         for i in range(5):
  59.             item = balls.pop(i)
  60.             if collide_check(item,balls):
  61.                 item.speed[0] = -item.speed[0]
  62.                 item.speed[1] = -item.speed[1]
  63.             balls.insert(i,item)

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


  66.     clock.tick(30)

  67. if __name__ == "__main__":
  68.     main()
复制代码
  1. Traceback (most recent call last):
  2.   File "E:/python代码/pygame练习/小游戏-玩个球啊/cllide_check.py", line 85, in <module>
  3.     main()
  4.   File "E:/python代码/pygame练习/小游戏-玩个球啊/cllide_check.py", line 55, in main
  5.     if collide_check(ball,balls):
  6.   File "E:/python代码/pygame练习/小游戏-玩个球啊/cllide_check.py", line 36, in collide_check
  7.     return col_balls
  8. UnboundLocalError: local variable 'col_balls' referenced before assignment
复制代码

哪位大佬给看看怎么回事啊
最佳答案
2017-8-6 10:20:17
如果target是空的,那么for循环就不成立,那么就没有col_balls赋值,那么就会出这个错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-8-6 10:20:17 | 显示全部楼层    本楼为最佳答案   
如果target是空的,那么for循环就不成立,那么就没有col_balls赋值,那么就会出这个错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-6 10:38:32 | 显示全部楼层
ooxx7788 发表于 2017-8-6 10:20
如果target是空的,那么for循环就不成立,那么就没有col_balls赋值,那么就会出这个错误。

谢谢,我看到了。我把col_balls定义在了for循环之内了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-1 07:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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