有人可以帮我发现错误吗?我不知道我做错了什么。
当我进入main.py并尝试运行游戏时,才会出现此错误。
主类:
import pygame
from pygame.locals import *
from player import *
from blocks import *
gravity = -4
display_height, display_width = 640, 480
display = pygame.display.set_mode ((display_height, display_width), 0, 16)
pygame.display.set_caption("Pixie from Outerspace")
clock = pygame.time.Clock()
FPS = 30
player = Player(display_height/2)
levelOne = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
blockList = []
for y in range(0, len(levelOne)):
for x in range(0, len(levelOne[y])):
if (levelOne[y][x]==1):
blockList.append(Block(x*32, y*32))
moveX, moveY = 0, 0
running = True
while running:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
running = False
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_RIGHT):
moveX = 3
elif (event.key == pygame.K_LEFT):
moveX = -3
elif (event.key == pygame.K_d):
moveX = 10
elif (event.key == pygame.K_UP):
player.jump()
if (event.type == pygame.KEYUP):
if (event.key == pygame.K_RIGHT):
moveX = 0
elif (event.key == pygame.K_LEFT):
moveX = 0
elif (event.key == pygame.K_d):
moveX = 0
display.fill(pygame.Color("lightblue3"))
for block in blockList:
block.render(display)
player.x += moveX
player.y -= moveY
player.update(gravity, blockList)
clock.tick(FPS)
pygame.display.flip()
pygame.quit()
玩家等级:
import pygame
class Player(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.width = 60
self.height = 56
self.velocity = 0
self.falling = True
self.onGround = False
self.pixie0 = pygame.image.load("pixie1.png")
self.pixie1 = pygame.image.load("pixie2.png")
self.timeTarget = 10
self.timeNumber = 0
self.currentImage = 0
self.cloud = pygame.image.load("cloud.png")
self.tree = pygame.image.load("tree.png")
self.bush = pygame.image.load("bush.png")
def jump(self):
if (self.onGround == False):
return
self.velocity = 30
self.onGround = False
def detectCollision(self, 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.currentImage == 0):
self.currentImage += 1
else:
self.currentImage == 0
self.render()
if (self.velocity < 0):
self.falling = True
collision = False
blockX, blockY = 0, 0
for block in blockList:
collision = self.detectCollision(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height)
if (collision == True):
blockX = block.x
blockY = block.y
break
if (collision == True):
if (self.falling == True):
self.falling = False
self.onGround = True
self.velocity = 0
self.y = blockY - self.height
if (self.onGround == False):
self.velocity += gravity
self.y -= self.velocity
def render(self):
display.blit (self.bush, (270, 367))
display.blit (self.bush, (440, 367))
display.blit (self.tree, (70, 232))
display.blit (self.cloud, (150, 100))
display.blit (self.cloud, (350, 50))
display.blit (self.cloud, (450, 150))
display.blit (self.cloud, (50, 50))
if (self.currentImage == 0):
display.blit(self.pixie0, (self.x, self.y))
else:
display.blit(self.pixie1, (self.x, self.y))
答案 0 :(得分:0)
错误告诉您到底发生了什么。当您实例化它时,Player类需要两个参数(以及自动传递的“self”):x
和y
。但是你只用display_height/2
实例化它,这只是一个参数。
您应该查看告诉您错误发生在哪一行的追溯,而不是发布所有不相关的代码,如果您无法弄清楚发生了什么,那么您应该发布相关的部分
答案 1 :(得分:0)
在 Player 类的构造函数中,它清楚地显示3个参数,一个是self和另外两个,但是在初始化类的对象时你只提供了一个参数,你需要提供y参数太