检测碰撞错误说当我有 8 个参数时我有 9 个参数 [重复]
Posted
技术标签:
【中文标题】检测碰撞错误说当我有 8 个参数时我有 9 个参数 [重复]【英文标题】:Detect collisions error says I have 9 arguments when I have 8 [duplicate] 【发布时间】:2016-01-01 14:54:05 【问题描述】:它说我有 9 个参数,但我只数了 8 个。我该如何解决这个问题?
错误信息
File Name Line Position
Traceback
<module> C:\Python34\2d\main.py 1
update C:\Python34\2d\main.py 53
TypeError: detectCollisions() takes 8 positional arguments but 9 were given
这是我的代码
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("use arrows")
class player:
def __init__(self ,x, y):
self.x = x
self.y = y
self.width = 32
self.height = 32
self.velocity = 0
self.falling = False
self.onGround = False
def detectCollisions(x1,y1,w1,h1,x2,y2,w2,h2):
if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):
return True
elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):
return True
elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):
return True
elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):
return True
else:
return False
def update(self, gravity, blockList):
if (self.velocity < 0):
self.falling = True
collision = False
blockX,blockY = 0,0
for block in blockList:
collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height)
if(collision == True):
(self.falling == True)
self.falling == False
self.onGround== True
self.velocity = 0
if(self,onGround == False):
self.velocity += gravity
self.y -= self.velocity
def render(self,screen):
pygame.draw.rect(screen,(0,0,0),(self.x, self.y, self.width, self.height))
class Block:
def __init__ (self, x, y):
self.x = x
self.y = y
self.width = 32
self.height = 32
def render(self,screen):
pygame.draw.rect(screen,(0,0,0),(self.x, self.y, self.width, self.height))
gravity = -0.5
black = (0,0,0)
white = (255,255,255)
blue = (50,60,200)
clock = pygame.time.Clock()
player = player(0,0)
# 25 colums and 19 rows
level1 = [
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
]
blockList = []
for y in range (0,len(level1)):
for x in range (0,len(level1[y])):
if (level1[y][x] == 1):
blockList.append(Block(x*32, y*32))
gameloop = True
while gameloop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameloop = False
screen.fill(blue)
for block in blockList:
block.render(screen)
player.update(gravity, blockList)
player.render(screen)
clock.tick(60)
pygame.display.update()
pygame.quit()
【问题讨论】:
【参考方案1】:您只是忘记将self
作为函数detectCollisions
的参数包含在内。只需将它添加为第一个参数,就像您对 update(self, gravity, blockList)
所做的那样,您会没事的。原因是detectCollisions
是类方法而不是常规函数。
请参阅this question 了解有关函数和类方法之间的区别以及为什么需要self 的更多详细信息。
【讨论】:
你能给我举个例子吗,因为我真的不明白你的意思,对不起,我一直在做这个,我很累,需要在我睡觉前修好它 我一直在 deetctCollisions 中遇到自我prntscr.com/8nuhkf 甚至查看代码 我的意思是当你定义detectCollisions
。所以def detectCollisions(x1,y1,w1,h1,x2,y2,w2,h2):
这行应该改成def detectCollisions(self,x1,y1,w1,h1,x2,y2,w2,h2):
以上是关于检测碰撞错误说当我有 8 个参数时我有 9 个参数 [重复]的主要内容,如果未能解决你的问题,请参考以下文章