关于python:TypeError:playerMovement()占用1个位置参数,但给出了2个

TypeError: playerMovement() takes 1 positional argument but 2 were given

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

Traceback (most recent call last):

File"C:/Users/ASUS/Desktop/Programing/Other
Development/Projects/Racing Game.py", line 141, in

playerMovement(x,y)
TypeError: playerMovement() takes 1 positional argument but 2 were
given

我不知道该怎么修。我发现了其他错误,但我不知道从哪开始

作为旁注,这是我的第一个项目,所以任何建议都会受到欢迎

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
esources\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()


您应该关注这一行,因为它只需要一个参数,但同时传递x和y。所以请检查一下!!!!

1
2
3
4
5
6
7
8
9
10
11
12
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()


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