2021-01-28 20:54

# Python高兴的编程中又遇到了问题

``````import pygame as g
from sys import exit
from pygame.locals import QUIT
from random import randint
from myrandom import rnum

#some var
speed = 5
angle = 3
time = g.time.Clock()
screenUp = 0
screenDown = 600
screenLeft = 0
screenRight = 800
screen = g.display.set_mode((800,600))
square = 10

#some class
class LBar():
global square
y = 600/2
x = 3*square
def show(self):
"""show the left bar"""
global screen,square,screenUp,screenDown
mx,LBar.y = g.mouse.get_pos()
if LBar.y-4*square < screenUp:
LBar.y = screenUp+4*square
elif LBar.y+4*square > screenDown:
LBar.y = screenDown-4*square
LBarRect = g.Rect( (LBar.x , LBar.y-square*4) , (square , square*8) )
g.draw.rect(screen,(255,255,255),LBarRect)

class Ball():
global square
x = 800/2-square/2
y = 600/2-square/2
move = (0,0)
def show(self):
"""show the ball"""
global screen,square,screenUp,screenDown,screenLeft,screenRight,angle
randict = {1:(angle,-square),2:(square,-square),3:(square,-angle),4:(square,angle),5:(square,square),6:(angle,square),
7:(-angle,square),8:(-square,square),9:(-square,angle),10:(-square,-angle),11:(-square,-square),12:(-angle,-square)}
lastMove = Ball.move
if Ball.move == (0,0):
Ball.move = randict[randint(1,12)]
elif Ball.x < screenLeft or (Ball.x > LBar.x+square and lastMove[0] < 0):
Ball.move = randict[int(rnum(out=[7,8,9,0]))]
elif Ball.x+square > screenRight or (Ball.x+square > LBar.x and lastMove[0] > 0):#more or
elif Ball.y < screenUp or Ball.y+square < LBar.x:
Ball.move = randict[int(rnum(out=[1,2,3,0]))]
elif Ball.y > screenDown or Ball.y > LBar.y+8*square:
Ball.x += Ball.move[0]
Ball.y += Ball.move[1]
ballRect = g.Rect( (Ball.x , Ball.y) , (square , square) )
g.draw.rect(screen,(255,255,255),ballRect)

#some define
def showBg():
"""show the bg"""
global screen,speed
BgRect = g.Rect( (0,0) , (800,600) )
g.draw.rect(screen,(0,0,0),BgRect)

#init
g.init()
g.display.set_caption('Bong!')
g.mouse.set_visible(False)
LBar = LBar()
ball = Ball()

#main
if __name__ == '__main__':
while True:
time.tick(1000)
if len(g.event.get(QUIT)) != 0:
g.quit()
exit()
showBg()
LBar.show()
ball.show()
g.display.update()``````

``````import random as r
import mylist as l

def rany(digital=1,alist=list('1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM')):
"""生成一个由指定列表中的元素组成的随机字符串
digital：位数（整数）
alist：列表（列表）"""
alist2=list(set(alist))
a=''
for i in range(int(digital)):
a+=r.choice(alist2)
return a

"""生成一个由数字组成的随机字符串
digital：位数（整数）
out：排除的内容（列表）"""
return rany(digital,li)``````

``````def delete(major,minor):
"""删除一个列表中的另一个列表
major：被删除的列表（列表）
minor：删除的内容（列表）"""
major=major
for i in major:
if i in minor:
major.remove(i)
return``````

``````Traceback (most recent call last):
File "C:\编程\Python\Bong！\Bong！.py", line 83, in <module>
ball.show()
File "C:\编程\Python\Bong！\Bong！.py", line 50, in show
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\myrandom.py", line 19, in rnum
TypeError: 'NoneType' object is not iterable``````

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 7条回答

• 幽默小白羊 3月前

谢谢修正~不过暂时没时间改哦~

点赞 评论 复制链接分享
• ~天外天中~^_^ 3月前

分析：

将错误提示中最后的代码改成->

``````a = list('1234567890') + add
for x in out:
if (x in a):
a.remove(x)
li = list(set(a))``````
点赞 1 评论 复制链接分享
• ~天外天中~^_^ 3月前

报错信息：NoneType不是一个可迭代类型

点赞 1 评论 复制链接分享
• 幽默小白羊 3月前

还有我想知道插入的代码块中为什么注释了一大片啊？

点赞 评论 复制链接分享
• 幽默小白羊 3月前

准备是还没来得及做的意思哦~~

点赞 评论 复制链接分享
• 幽默小白羊 3月前

朋友，你可能理解错了，是没来得及做，不是不会做。也感谢你能光顾。求解决问题~d=====(￣▽￣*)b

点赞 评论 复制链接分享
• 放风喽 3月前

将球对象传入板对象的update方法里面，

``````class ban():
def update(self,qiu):
self.y = qiu.y
# 将球对象传进来，直接板的Y坐标直接等于球的Y坐标``````
点赞 评论 复制链接分享