这可能是由于我无法识别的一些愚蠢错误,但是当我运行以下代码时Python返回语法错误
# Imports
import pygame
import os
# Startup
pygame.init()
# Screen
size = (500, 500)
screen = pygame.display.set_mode(size, pygame.NOFRAME)
pygame.display.set_caption = ("Swinger")
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
# -- Assign Functions --
# -- Assign Classes --
# -- Assign Variables --
#Sets the color of pure white to a variable to be called
WHITE = (255, 255, 255)
#Sets the player to an image loaded from os path Swinger
Player = pygame.image.load(os.path.join("Swinger", "player.png"))
Pointer = pygame.image.load(os.path.join("Swinger", "pointer.png"))
#Sets a variable to know whether the scrpt has run once or not
FirstRun = 0
#Sets the variables to allow you to control movement
#Y axis movement Speed
MoveYSpeed = 0
#X axis movement speed
MoveXSpeed = 0
#Assigns the values later used to call the drawing
#of the player
PlayerPos = [255 += MoveXSpeed, 255 += MoveYSpeed]
#Assigns The first value of PlayerPos as a X axis value
PlayerPosX = PlayerPos[0]
#Assigns the second value of PlayerPos as a y axis value
PlayerPosY = PlayerPos[1]
它在第一行增加语句的第31行给出了语法错误,说Equal符号是错误的:
Traceback (most recent call last):
File "...", line 31
PlayerPos = [255 += MoveXSpeed, 255 += MoveYSpeed]
^
SyntaxError: invalid syntax
我刚刚开始编写代码,因此大部分代码可能达不到正确的格式而且我没有复制所有代码,因为我认为考虑到错误可能不相关在达到任何其他代码之前。
答案 0 :(得分:0)
PlayerPos = [255 += MoveXSpeed, 255 += MoveYSpeed]
不是有效的Python语法,不是。你想用那个表达做什么? +=
不是运算符,它是语句(如=
分配,或return
或if test: <block>
),您可以&#39;把陈述放在其他陈述中。
你的意思是仅仅使用+
加法运算符吗?
PlayerPos = [255 + MoveXSpeed, 255 + MoveYSpeed]
将创建一个列表,其中包含这两个总和作为初始值。