首页 > 解决方案 > TypeError: playerMovement() 接受 1 个位置参数,但给出了 2 个

问题描述

我是一个相对较新的程序员,我一直在尝试制作一个简单的赛车游戏,但我不断收到错误

回溯(最近一次通话最后):

文件“C:/Users/ASUS/Desktop/Programing/Other Development/Projects/Racing Game.py”,第 141 行,在

playerMovement(x,y) TypeError: playerMovement() 接受 1 个位置参数,但给出了 2 个

我不知道如何解决它。我发现了其他错误,但我不知道从哪里开始

作为旁注,这是我的第一个项目,所以任何建议表示赞赏

import pygame
import pygame.gfxdraw
from pygame.locals import *

# Intilization --------------------------------------------------------------------------------------------------------#
pygame.init()
FONT = pygame.font.Font(None, 15)
# Dev Options ---------------------------------------------------------------------------------------------------------#
developerVision = True

# Display -------------------------------------------------------------------------------------------------------------#
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Pope Stadium')
clock = pygame.time.Clock()
# Graphics ------------------------------------------------------------------------------------------------------------#
car = pygame.image.load(r'C:\Users\ASUS\Desktop\Programing\Resources\Pictures\Car.png')
carIMG = pygame.transform.scale(car, (100,160))
# Variables -----------------------------------------------------------------------------------------------------------#
x = display_width * 0.45
y = display_height * 0.8

x_change = 0

# Colors --------------------------------------------------------------------------------------------------------------#
FUCHSIA = (255, 0, 255)
PURPLE = (128, 0, 128)
TEAL = (0, 128, 128)
LIME = (0, 255, 0)
GREEN = (0, 128, 0)
OLIVE = (128, 128, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
RED = (255, 0, 0)
MAROON = (128, 0, 0)
SILVER = (192, 192, 192)
GRAY = (128, 128, 128)
BLUE = (0, 0, 255)
NAVY = (0, 0, 128)
AQUA = (0, 255, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


# Functions -----------------------------------------------------------------------------------------------------------#
def car(x, y):
    return(gameDisplay.blit(carIMG, (x, y)))

def text(l, j, t, c=0):
    global gameDisplay, FONT
    text_image = FONT.render(t, 1, (255, 0, 0))
    if c == 1:
        r = text_image.get_rect()
        gameDisplay.blit(text_image, (l - r.width / 2, j))
    elif c == 2:
        r = text_image.get_rect()
        gameDisplay.blit(text_image, (l, j - r.height / 2))
    elif c == 3:
        r = text_image.get_rect()
        gameDisplay.blit(text_image, (l - r.width / 2, j - r.height / 2))
    else:
        gameDisplay.blit(text_image, (l, j))

def playerMovement(event):
    for event in pygame.event.get():
        if (event.type == pygame.KEYDOWN):
            if (event.key == pygame.K_LEFT or event.key == K_a):
                x_change = -5
            elif (event.key == pygame.K_RIGHT or event.key == K_d):
                x_change = 5
        if (event.type == pygame.KEYUP):
            if (event.key == pygame.K_RIGHT or event.key == K_d):
                x_change = 0
            elif (event.key == pygame.K_LEFT or event.key == pygame.K_a):
                x_change = 0
    return(x_change)

def developerTools():
    if developerVision == True:
        draw_ruler()
        draw_center_indicator()
        draw_mouse_position()

def drawGame(x,y):
    gameDisplay.fill(WHITE)
    car(x, y)
    pygame.display.update()
    clock.tick(60)

# Flags ---------------------------------------------------------------------------------------------------------------#
crashed = False
if (developerVision == True):
    rulerEnabled = True
    centercrosshairEnabled = True
    displayMousePosition = True
if (developerVision == False):
    rulerEnabled = False
    centercrosshairEnabled = False
    displayMousePosition = False
# Helpful Tools -------------------------------------------------------------------------------------------------------#
def draw_ruler():
    global gameDisplay
    for x in range(10, display_width - 10, 10):
        if x % 50 == 0:
            pygame.draw.line(gameDisplay, (255, 0, 0), (x, 0), (x, 10), 1)
            text(x, 13, "{0}".format(x), 1)
        else:
            pygame.draw.line(gameDisplay, (255, 0, 0), (x, 0), (x, 5), 1)
    for y in range(10,display_height - 10, 10):
        if y % 50 == 0:
            pygame.draw.line(gameDisplay, (255, 0, 0), (0, y), (10, y), 1)
            text(13, y, "{0}".format(y), 2)
        else:
            pygame.draw.line(gameDisplay, (255, 0, 0), (0, y), (5, y), 1)

def draw_center_indicator():
    global gameDisplay
    pygame.draw.line(gameDisplay, (255, 0, 0), (display_width, display_height - 25), (display_width, display_height- 5), 1)
    pygame.draw.line(gameDisplay, (255, 0, 0), (display_width, display_height + 5), (display_width, display_height + 25), 1)
    pygame.draw.line(gameDisplay, (255, 0, 0), (display_width - 25, display_height), (display_height - 5, display_height), 1)
    pygame.draw.line(gameDisplay, (255, 0, 0), (display_width + 5, display_height), (display_width + 25, display_height), 1)


def draw_mouse_position():
    global gameDisplay
    x, y = pygame.mouse.get_pos()
    text(x, y - 15, "x: {0}, y: {1}".format(x, y), 1)

# Game Loop -----------------------------------------------------------------------------------------------------------#

while not crashed:

    for event in pygame.event.get():
        #How to handle Exiting
        if event.type == pygame.quit:
            crashed = True
        #Handeling movement
        playerMovement(x,y)
        developerTools()
        drawGame()


quit()

标签: pythonpython-3.x

解决方案


在您的代码中,您定义的函数只接受一个参数。您似乎在主循环中输入了 2 个参数。您可能想输入 pygame.mouse.get_pos() 而不是 2 个参数。pygame.mouse.get_pos() 将返回您的函数工作所需的信息。


推荐阅读