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

时间:2018-12-04 04:41:00

标签: python python-3.x

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

  

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

     

文件“ C:/用户/ ASUS /桌面/编程/其他   开发/项目/竞速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()

2 个答案:

答案 0 :(得分:0)

在您的代码中,您定义的函数仅使用一个参数。看来您在主循环中输入了2个参数。您可能要输入     pygame.mouse.get_pos()而不是2个参数。 pygame.mouse.get_pos()将返回所需的信息,以使您的功能正常工作。

答案 1 :(得分:0)

您应该专注于此行,因为它仅接受一个参数,但同时传递x和y。所以请检查!!!

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) # in this line you are giving 2 args(x,y)
    developerTools()
    drawGame()


quit()