我正在尝试制作游戏,并且不断弹出错误消息,我不知道如何解决:
Traceback (most recent call last):
File "C:/Users/LENOVO/Desktop/GROOT!/new start.py", line 46, in <module>
man = player(300, 410, 64, 64)
TypeError: player() takes no arguments
这是错误。代码如下:
import pygame
pygame.init()
win = pygame.display.set_mode((500,480))
pygame.display.set_caption("First Game")
walkRight = [pygame.image.load('Game//R1.png'), pygame.image.load('Game//R2.png'), pygame.image.load('Game//R3.png'), pygame.image.load('Game//R4.png'), pygame.image.load('Game//R5.png'), pygame.image.load('Game//R6.png'), pygame.image.load('Game//R7.png'), pygame.image.load('Game//R8.png'), pygame.image.load('Game//R9.png')]
walkLeft = [pygame.image.load('Game//L1.png'), pygame.image.load('Game//L2.png'), pygame.image.load('Game//L3.png'), pygame.image.load('Game//L4.png'), pygame.image.load('Game//L5.png'), pygame.image.load('Game//L6.png'), pygame.image.load('Game//L7.png'), pygame.image.load('Game//L8.png'), pygame.image.load('Game//L9.png')]
bg = pygame.image.load('Game//bg.jpg')
char = pygame.image.load('Game//standing.png')
clock = pygame.time.Clock()
class player(object):
def __init_(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isjump = False
self.jumpCount = 10
self.left = False
self.right = False
self.walkCount = 0
def redrawGameWindow():
global walkCount
win.blit(bg, (0,0))
if walkCount + 1 >= 27:
walkCount = 0
if left:
win.blit(walkLeft[walkCount//3], (int(x),int(y)))
walkCount += 1
elif right:
win.blit(walkRight[walkCount//3], (int(x),int(y)))
walkCount += 1
else:
win.blit(char, (int(x), int(y)))
walkCount = 0
pygame.display.update()
man = player(300, 410, 64, 64)
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.vel - man.width:
man.x += vel
man.left = False
man.right = True
else:
man.left = False
man.right = False
man.walkCount = 0
if not(man.isJump):
if keys[pygame.K_SPACE]:
man.isJump = True
man.left = False
man.right = False
man.walkCount = 0
else:
if man.jumpCount >= -10:
man.y -= (man.jumpCount * abs(man.jumpCount)) * 0.5
man.jumpCount -= 1
else:
man.jumpCount = 10
man.isJump = False
redrawGameWindow()
pygame.quit()
我编写了整个代码,因为我不完全知道其中哪一部分是错误的。我从错误中获得的唯一信息是player()不接受任何参数。我什至在class()中定义了它,但我不知道如何解决它。
答案 0 :(得分:1)
将def __init_
更改为def __init__
(末尾用双下划线代替单个下划线)