我试图使用pygame库编写游戏但由于某种原因它继续抛出 TypeError:期望的整数参数,浮动 错误以下行:
if surface.get_at((player["x"], player["y"] + player["height"])) == (0,0,0,255):
leftOfPlayerOnPlatform = False
if surface.get_at((player["x"] + player["width"], player["y"] + player["height"])) == (0,0,0,255):
rightOfPlayerOnPlatform = False
if leftOfPlayerOnPlatform is False and rightOfPlayerOnPlatform is False and (player["y"] + player["height"]) + player["vy"] < windowHeight:
player["y"] += player["vy"]
我的问题的完整代码是:
import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
import pygame.time as GAME_TIME
pygame.init()
clock = pygame.time.Clock()
title_image = pygame.image.load("assets/title.jpg")
game_over_image = pygame.image.load("assets/game_over.jpg")
windowWidth = 400
windowHeight = 600
surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Drop!')
leftDown = False
rightDown = False
gameStarted = False
gameEnded = False
gamePlatforms = []
platformSpeed = 3
platformDelay = 2000
lastPlatform = 0
platformsDroppedThrough = -1
dropping = False
gameBeganAt = 0
timer = 0
player = {
"x" : windowWidth / 2,
"y" : 0,
"height" : 25,
"width" : 10,
"vy" : 5
}
def drawPlayer():
pygame.draw.rect(surface, (255,0,0), (player["x"], player["y"],player["width"], player["height"]))
def movePlayer():
global platformsDroppedThrough, dropping
leftOfPlayerOnPlatform = True
rightOfPlayerOnPlatform = True
if surface.get_at((player["x"], player["y"] + player["height"])) == (0,0,0,255):
leftOfPlayerOnPlatform = False
if surface.get_at((player["x"] + player["width"], player["y"] + player["height"])) == (0,0,0,255):
rightOfPlayerOnPlatform = False
if leftOfPlayerOnPlatform is False and rightOfPlayerOnPlatform is False and (player["y"] + player["height"]) + player["vy"] < windowHeight:
player["y"] += player["vy"]
if dropping is False:
dropping = True
platformsDroppedThrough += 1
else :
foundPlatformTop = False
yOffset = 0
dropping = False
while foundPlatformTop is False:
if surface.get_at((player["x"], (player["y"] + player["height"]) - yOffset )) == (0,0,0,255):
player["y"] -= yOffset
foundPlatformTop = True
elif (player["y"] + player["height"]) - yOffset > 0:
yOffset += 1
else :
gameOver()
break
if leftDown is True:
if player["x"] > 0 and player["x"] - 5 > 0:
player["x"] -= 5
elif player["x"] > 0 and player["x"] - 5 < 0:
player["x"] = 0
if rightDown is True:
if player["x"] + player["width"] < windowWidth and (player["x"] + player["width"]) + 5 < windowWidth:
player["x"] += 5
elif player["x"] + player["width"] < windowWidth and (player["x"] + player["width"]) + 5 > windowWidth:
player["x"] = windowWidth - player["width"]
def createPlatform():
global lastPlatform, platformDelay
platformY = windowHeight
gapPosition = random.randint(0, windowWidth - 40)
gamePlatforms.append({"pos" : [0, platformY], "gap" : gapPosition})
lastPlatform = GAME_TIME.get_ticks()
if platformDelay > 800:
platformDelay -= 50
def movePlatforms():
# print("Platforms")
for idx, platform in enumerate(gamePlatforms):
platform["pos"][1] -= platformSpeed
if platform["pos"][1] < -10:
gamePlatforms.pop(idx)
def drawPlatforms():
for platform in gamePlatforms:
pygame.draw.rect(surface, (255,255,255), (platform["pos"][0], platform["pos"][1], windowWidth, 10))
pygame.draw.rect(surface, (0,0,0), (platform["gap"], platform["pos"][1], 40, 10) )
def gameOver():
global gameStarted, gameEnded
platformSpeed = 0
gameStarted = False
gameEnded = True
def restartGame():
global gamePlatforms, player, gameBeganAt, platformsDroppedThrough, platformDelay
gamePlatforms = []
player["x"] = windowWidth / 2
player["y"] = 0
gameBeganAt = GAME_TIME.get_ticks()
platformsDroppedThrough = -1
platformDelay = 2000
def quitGame():
pygame.quit()
sys.exit()
# 'main' loop
while True:
surface.fill((0,0,0))
for event in GAME_EVENTS.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
leftDown = True
if event.key == pygame.K_RIGHT:
rightDown = True
if event.key == pygame.K_ESCAPE:
quitGame()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
leftDown = False
if event.key == pygame.K_RIGHT:
rightDown = False
if event.key == pygame.K_SPACE:
if gameStarted == False:
restartGame()
gameStarted = True
if event.type == GAME_GLOBALS.QUIT:
quitGame()
if gameStarted is True:
# Play game
timer = GAME_TIME.get_ticks() - gameBeganAt
movePlatforms()
drawPlatforms()
movePlayer()
drawPlayer()
elif gameEnded is True:
# Draw game over screen
surface.blit(game_over_image, (0, 150))
else :
# Welcome Screen
surface.blit(title_image, (0, 150))
if GAME_TIME.get_ticks() - lastPlatform > platformDelay:
createPlatform()
clock.tick(60)
pygame.display.update()
我使用Thonny来运行代码。 如果有人可以帮我解决我的问题,我会非常感激。
答案 0 :(得分:2)
您使用的是Python 2.7还是3.x?
如果您使用的是3.x,默认情况下会应用浮动分割,因此行
player["x"] = windowWidth / 2
会产生一个浮动。 PyGame要求所有坐标都是整数。在Python 3.x中使用//进行整数除法
player["x"] = windowWidth // 2 # or use
player["x"] = int(windowWidth / 2)
这可能是TypeError的意思:预期 整数 参数,得到 float 作为行你表示确实会参考坐标。
答案 1 :(得分:-1)
我在对surface.get_at()的函数调用中的每个参数周围放置了int()。这解决了我的问题。我正在使用Python3和同一教程。
示例: 发件人(错误)
if surface.get_at(( player["x"] , player["y"] + player["height"] )) == (0,0,0,255):
leftOfPlayerOnPlatform = False
收件人(无错误)
if surface.get_at((int(player["x"]), int(player["y"] + player["height"]))) == (0,0,0,255):
leftOfPlayerOnPlatform = False
在三个实例中,我更改了代码。
完成此操作后,便可以从drop.py
执行Thonny
。