首页 > 解决方案 > 变量语法错误

问题描述

所以这:

import pygame
import time
import random
import sys

itworks = True

pygame.init()
screen = pygame.display.set_mode((600, 600))

bootpng = pygame.image.load('bootimage.png')
bootpng = pygame.transform.scale(bootpng, (600, 600))
backgroundmenu = pygame.image.load('Menu_background.png')
backgroundmenu = pygame.transform.scale(backgroundmenu, ((600, 600))
button = pygame.image.load('technical_information_button')
button = pygame.transform.scale(infobut, ((130, 80))
screen.blit(bootpng, (0, 0))
time.sleep(3)

while itworks == True:
    screen.blit(backgroundmenu, (0, 0))
    screen.blit(tekbutton, (470, 520))
    for event in pygame.event.get():
        if ecent.type == pygame.QUIT:
            itworks = False

    #    if event.type == pygame.MOUSEBUTTONDOWN:
    # Set the x, y postions of the mouse click
    #x, y = event.pos
    #if ball.get_rect().collidepoint(x, y):

    pygame.display.flip()

pygame.quit()

是我的代码。Buuut .. 出于某种原因,python 在变量按钮第 15 行*处说“无效语法”。我真的不知道该怎么办,我尝试了很多东西,所以我希望我能从这里得到帮助:D

标签: pythonvariablessyntaxpygamesyntax-error

解决方案


您需要删除一个额外的括号。

代替:button = pygame.transform.scale(infobut, ((130, 80))

和:button = pygame.transform.scale(infobut, (130, 80))

在 Python 中,应该始终有一个左括号和右括号。

编辑:

只是补充一下,我强烈建议将您替换while itworks == Truewhile itworks is True. 更多信息可以在这里看到:布尔标识 == True vs is True

基本上,if由于不比较引用的相等性,该语句在某些情况下可能不会给出预期的结果。这只是“pythonic”的做事方式。


推荐阅读