m0_53391046
幽默小白羊
采纳率60%
2021-01-28 20:54

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

如果有大神帮我看出错误原因就太好了,如果后面也有问题或是有更好的建议尽管提出来,有错必改是好猿猿嘛。

代码设计到复杂的模块引用,多贴几段代码。

代码是未完成的,右半边的人机还没做,注释more or意思是还有更多or。

准备让RBar的y坐标中心点始终与球的相等,人机永远不可能接不到球。

下面是主程序:

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
            Ball.move = randict[int(rnum(add=[10,11,12],out=[1,2,3,4,5,6,0]))]
        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.move = randict[int(rnum(add=[10,11,12],out=[4,5,6,7,8,9,0]))]
        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()

下边是myrandom模块中的rnum和rany函数:

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

def rnum(digital=1,add='0',out='None'):
    """生成一个由数字组成的随机字符串
digital:位数(整数)
add:添加的内容(列表)
out:排除的内容(列表)"""
    li=list(set(l.delete(list('1234567890')+add,out)))
    return rany(digital,li)

下面是mylist模块中的delete函数:

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

要是有不懂的语句私信我,尽管问。我一定要把这个游戏解决,这是我第一次认真的使用pygame进行游戏开发。也希望高手们多多提出建议鸭。

报错信息:

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
    Ball.move = randict[int(rnum(add=[10,11,12],out=[1,2,3,4,5,6,0]))]
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\myrandom.py", line 19, in rnum
    li=list(set(l.delete(list('1234567890')+add,out)))
TypeError: 'NoneType' object is not iterable

最后,再望大神们指导,抱拳

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

7条回答

  • m0_53391046 幽默小白羊 3月前

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

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

    分析:

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

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

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

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

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

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

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

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

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

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

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

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