- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上,我正在尝试编写一些在屏幕上显示食物的代码,并且我正在使用类结构来创建这些对象。然后我将它们放入一个 Sprite 组中,然后在屏幕上显示该 Sprite 组但是我在运行代码时收到此错误:
Traceback (most recent call last): File "N:/Y12/Computer science/Consumo/Consumo V8.5.py", line 172, in menuScreen() File "N:/Y12/Computer science/Consumo/Consumo V8.5.py", line 113, in menuScreen button("Play Game",850,340,120,50,blue,gameLoop)#Puts theree button on the screen File "N:/Y12/Computer science/Consumo/Consumo V8.5.py", line 99, in button action() File "N:/Y12/Computer science/Consumo/Consumo V8.5.py", line 160, in gameLoop allSprites.add(food) File "C:\Python33\lib\site-packages\pygame\sprite.py", line 378, in add sprite.add_internal(self) AttributeError: 'FoodItems' object has no attribute 'add_internal'
这是我的代码,我想知道是否有人可以帮助我修复此错误,因为我什至不知道该错误意味着什么。:
#Imports the libaries I will use
import pygame,time,random
pygame.init()
#Sets up the variables for the screen size
displayWidth = 1000
displayHeight = 600
weight = 0
lives = 0
clock= pygame.time.Clock()
#sets up the variables for the colours
black = (0 ,0 ,0)
red = (200 ,0 ,0)
grey = (128 ,128 ,128)
blue = (0, 0, 200)
green= (0,255,0)
#Sets up the images and re-sizes them and also adds the music file
character =pygame.image.load("character.png")
character = pygame.transform.scale(character,(60,60))
sumo = pygame.image.load("sumo .jpg")
sumo = pygame.transform.scale(sumo,(60,60))
#both background images are set to displayHeight,displayWidth so they fit the screen
dojoBackDrop = pygame.image.load("dojo.png")
dojoBackDrop = pygame.transform.scale(dojoBackDrop,(displayWidth,displayHeight))
menuBackDrop = pygame.image.load("menu screen .png")
menuBackDrop = pygame.transform.scale(menuBackDrop,(displayWidth,displayHeight))
foodSizeX = 45#I have set up a variable to re-size all the images to as
foodSizeY = 45
0#I want them to be the same size
#loads all the food images for my food
apple = pygame.image.load("Apple .png")
apple = pygame.transform.scale(apple,(foodSizeX,foodSizeY))
fish = pygame.image.load("Fish.png")
fish = pygame.transform.scale(fish,(foodSizeX,foodSizeY))
rice = pygame.image.load("Rice_fresh.png")
rice = pygame.transform.scale(rice,(foodSizeX,foodSizeY))
rottenApple = pygame.image.load("rottenapple.png")
rottenApple = pygame.transform.scale(rottenApple,(foodSizeX,foodSizeY))
rottenFish = pygame.image.load("rottenfish.png")
rottenFish = pygame.transform.scale(rottenFish,(foodSizeX,foodSizeY))
rottenRice = pygame.image.load("rottenrice.png")
rottenRice = pygame.transform.scale(rottenRice,(foodSizeX,foodSizeY))
#loads the music which will be playing during the game
#file has been converted from mp3 to ogg as python can't read mp3
#backGroundMusic = pygame.mixer.Sound("Bully Scholarship Edition Soundtrack - Arcade Game - ConSumo (Game) (1).ogg")
#sets up the pygame window with height and width variables created above
gameDisplay = pygame.display.set_mode((displayWidth,displayHeight))
images =[apple,rice,fish]
class FoodItems:
def __init__(self,weightIncrease,xChange,yChange):
pygame.sprite.Sprite.__init__(self)
self.weightIncrease = weightIncrease
#sets attributes for the class by using the self
# this is so they can be set to What I chose
self.image = random.choice(images)
self.rect= self.image.get_rect()
self.rectX = random.randrange(displayWidth - self.rect.width) #creates a hit box for my object
self.rectY = random.randrange(displayHeight-self.rect.height)
self.xChange = xChange # sets a random speed for x and y
self.yChange = yChange
def update():#conitnusly updates the sprite on the screen
self.rectX += self.xChange
self.rectY += sel.yChange #keeps moving the objects
if self.rectx > displayHeight +10:
self.rect.x = random.randrange(displayWidth-self.rect.width)
self.rect.y = random.randrange(displayHeight-self.rect.height)
#this will make the object be able to move
allSprites=pygame.sprite.Group()#creates a group of sprites on the screen
def textObjects(text,font):#passes parameters by value
textSurface = font.render(text,True,black)#renders the font and sets the color as black
return textSurface,textSurface.get_rect()#gets the dimensions of the text
def button(msg,x,y,w,h,c,action=None):#passes the parameters by value
mouse = pygame.mouse.get_pos()#this gets the postion of the mouse
click = pygame.mouse.get_pressed()#checks to see where the mouse has been clicked
pygame.draw.rect(gameDisplay, c,(x,y,w,h))#draws a rectangle with on the game display with the parameters given
if x+w > mouse[0] >x and y+h > mouse[1] > y:#checks to see if the mouse has been clicked anywhere in the button
if click[0] == 1 and action != None:#if the mouse has been clicked performed an action
action()
smallText = pygame.font.SysFont("comicsansms",20)
textSurf , textRect = textObjects(msg ,smallText)
#sets the message of the button
textRect.center = ((x+(w/2),(y+(h/2))))#centers the text on screen
gameDisplay.blit(textSurf, textRect)#puts the button on screen with the text
def menuScreen():
screenOn = True
while screenOn ==True:
for event in pygame.event.get():
if event.type == pygame.QUIT:#allows user to quit the game
quit()
gameDisplay.blit(menuBackDrop,(0,0))#sets the image to the size of the screen
button("Play Game",850,340,120,50,blue,gameLoop)#Puts theree button on the screen
button("Leaderboard",850,440,120,50,blue)
button("Quit game",850,540,120,50,blue)
pygame.display.update()#updates the screen so things can appear on it
def gameLoop():
#backGroundMusic.play()
playerX =500 #used to set the characters position as the center of the screen
playerY = 300
playerXChange = 0#value at which x and ywill change when a Key is pressed
playerYChange = 0
playing = True #boolean so that the while loop knows when to run till
while playing ==True:
for event in pygame.event.get():
if event.type == pygame.QUIT:#allows user to quit the game
quit()
#if any key is pressed perfomr an action
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:#if A is pressed move ten to the left
playerXChange = -5
elif event.key == pygame.K_d:#if D is pressed move ten to the right
playerXChange = 5
elif event.key == pygame.K_w:#if W is pressed move ten up
playerYChange = -5
elif event.key == pygame.K_s:#if S is pressed move ten down
playerYChange = 5
if event.type ==pygame.KEYUP:
playerXChange =0
playerYChange =0
if playerX < 0: #if players x value is less than set it to zero
playerX = 0
if playerX >940:#if players x value is greater than 940 than set it to 940
playerX = 940
if playerY < 0:#if players y value is less than set it to zero
playerY = 0
if playerY >540:#if players y value is greater than 540 set it to 540
playerY = 540
playerX = playerX+playerXChange
playerY = playerY+playerYChange#makes the character x and y change by the value set in the code above
gameDisplay.blit(dojoBackDrop,(0,0))
for i in range(8):#loops to get 8 food on the screen at once
food =FoodItems(5,5,5)#instatiates an object
allSprites.add(food)#adds object to sprite group
#instantiate a new object
allSprites.update()#updates all sprites
allSprites.draw(gameDisplay)#draws all the sprites on the screen
gameDisplay.blit(character,(playerX,playerY))
pygame.display.update()
menuScreen()
我希望图像显示在屏幕上并继续移动,但我就是无法克服这个错误。
最佳答案
如果你想和一个对象到 pygame.sprite.Group
,那么该对象的类必须派生自 pygame.sprite.Sprite
:
class FoodItems(pygame.sprite.Sprite):
def __init__(self,weightIncrease,xChange,yChange):
pygame.sprite.Sprite.__init__(self)
# [...]
此外,方法 .update()
中缺少“self”属性:
class FoodItems(pygame.sprite.Sprite):
# [...]
def update(self): #conitnusly updates the sprite on the screen
self.rectX += self.xChange
self.rectY += self.yChange #keeps moving the objects
if self.rect.x > displayHeight +10:
self.rect.x = random.randrange(displayWidth-self.rect.width)
self.rect.y = random.randrange(displayHeight-self.rect.height)
#this will make the object be able to move
关于python - 尝试在 pygame 的 for 循环中创建对象并得到 AttributeError : 'FoodItems' object has no attribute 'add_internal' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56948388/
我遇到了这两个错误,“AttributeError:在 DataLoader 工作进程 0 中捕获 AttributeError”,“AttributeError:模块“torchvision.tra
以下是我的代码。在最后一行中,我无法将Items[node.ind].v值设置为我想要的node.v,并收到错误。我不知道出了什么问题,但一定是基于语法,因为使用node.v+=1这样的语句也会显示相
我们准备了以下python脚本来显示word表格中的图片。 import matplotlib.pyplot as plt import pylab import win32com.client as
我需要一种方法来获取 TensorFlow 中任何类型层(即 Dense、Conv2D 等)的输出张量的形状。根据文档,有 output_shape 属性可以解决这个问题。但是,每次我访问它时,我都会
除了我之前的问题,关于如何在 Python 中打开 csv 文件,我仍然没有成功地做到这一点,并且从一个错误到另一个错误。 我的Python代码如下: @app.route("/admin", met
这是我在Google Colab中使用的代码。当我打这些电话的时候。我收到以下错误。这很奇怪。我以前从来没有见过这个问题。有没有人能帮我一下?我是不是做错了什么?
我想将Excel中的数据添加到词典中。但是,当我使用.append(TOTAL_SALES)时出现错误,当然,如果我使用+=TOTAL_SALES,则没有问题,只是我获得的是总和,而不是3个单独月份的
我想将Excel中的数据添加到词典中。但是,当我使用.append(TOTAL_SALES)时出现错误,当然,如果我使用+=TOTAL_SALES,则没有问题,只是我获得的是总和,而不是3个单独月份的
我正在尝试使用 gr_modtool.py 在 gnuradio 中创建一个新的 DSP 模块。 gnuradio 版本是 3.3.0。我在 include 文件夹中的 abc.h 文件中有以下代码
AttributeError:尝试在序列化器 UserKeywordSerializer 上获取字段 user 的值时出现 AttributeError。序列化程序字段可能命名不正确,并且与 Quer
我有以下使用Chatterbot第三方库的代码:。当我尝试使用代码时,从Visual Studio收到如下错误:。我安装了以下程序包:。我尝试了使用Python3.9和3.11以及Chatterbot
我有以下使用Chatterbot第三方库的代码:。当我尝试使用代码时,从Visual Studio收到如下错误:。我安装了以下程序包:。我尝试了使用Python3.9和3.11以及Chatterbot
我有以下使用Chatterbot第三方库的代码:。当我尝试使用代码时,从Visual Studio收到如下错误:。我安装了以下程序包:。我尝试了使用Python3.9和3.11以及Chatterbot
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我试图配置预提交挂接,在运行预提交运行--所有文件时,我收到以下错误:。我已尝试升级pip以解决此问题pip安装--升级pip,但我收到另一个错误:。我尝试检查PIP和PIP3的版本,但现在我也收到了
我收到一个 AttributeError 我似乎无法解决。我正在处理两个类(class)。 第一个类就是这样。 class Partie: def __init__(self):
在 Django (1.4) 中迁移 South (0.7.5) 时,我遇到了这个错误。我最近将时区设置更改为 false,即 USE_TZ = False 以解决另一个问题。有任何想法吗?谢谢 ~/
当我尝试在两个序列化程序之间创建嵌套关系时出现 AttributeError。奇怪的是,我正在做与另一个 API 完全相同的事情,但这次我没有让它工作。这是代码: class UserSerializ
试图获得 manytomany django 中的关系,但我收到以下错误 - Got AttributeError when attempting to get a value for field n
我是一名优秀的程序员,十分优秀!