我得到了这段代码的无效语法,似乎很容易弄清楚但对我来说真的很难。
#Svårighetsgrader
enditlevels = False
while (enditlevels == False):
windowSurface.fill(WHITE)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonEasy.collidepoint(mouse_coordinates):
MOVE_SPEED = 9
NEW_RABBIT = 40
end_it_levels = True
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonNormal.collidepoint(mouse_coordinates):
MOVE_SPEED = 7
NEW_RABBIT = 30
end_it_levels = True
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonHard.collidepoint(mouse_coordinates):
MOVE_SPEED = 5
NEW_RABBIT = 20
end_it_levels = True
错误:
C:\Users\Rickard\My Programs\Python\slutarbete\Slutarbete>updatedSlutarbete.py
File "C:\Users\Rickard\My Programs\Python\slutarbete\Slutarbete\updatedSlutarb
ete.py", line 182
enditlevels = False
^
SyntaxError: invalid syntax
更多代码,因为我一直都会收到这些错误:
# -*- coding: utf-8 -*-
#Börjar med att importera alla moduler för att få spelkoden att funka här.
#Importerar först alla tillgängliga moduler till pygame paketet.
#Sedan så sätter jag en begränsad uppsättning av konstanter och funktioner
#i global namespace av scriptet. Efter det importerar jag timern som är
#essentiell i spel osv.
import pygame, sys, random, math
from pygame.locals import *
from threading import Timer
#Börjar sätta upp spel funktionerna och klockan.
pygame.init()
pygame.mixer.init()
mainClock = pygame.time.Clock()
_image_library = {}
def get_image(path):
global _image_library
image = _image_library.get(path)
if image == None:
canonicalized_path = path.replace('/', os.sep).replace('\\', os.sep)
image = pygame.image.load(canonicalized_path)
_image_library[path] = image
return image
def movementVariables():
global moveUp
global moveDown
global moveLeft
global moveRight
global levelOne
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#Tangentbords variabler
if event.type == KEYDOWN:
#testkey
if event.key == K_SPACE:
levelOne = False
if event.key == K_LEFT:
moveRight = False
moveLeft = True
if event.key == K_RIGHT:
moveRight = True
moveLeft = False
if event.key == K_UP:
moveDown = False
moveUp = True
if event.key == K_DOWN:
moveDown = True
moveUp = False
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT:
moveLeft = False
if event.key == K_RIGHT:
moveRight = False
if event.key == K_UP:
moveUp = False
if event.key == K_DOWN:
moveDown = False
def movementMechanism():
if moveDown and player.bottom < WINDOW_HEIGHT:
player.top += MOVE_SPEED
if moveUp and player.top > 0:
player.top -= MOVE_SPEED
if moveLeft and player.left > 0:
player.left -= MOVE_SPEED
if moveRight and player.right < WINDOW_WIDTH:
player.right += MOVE_SPEED
windowSurface.blit(playerImage, player)
#Ger resolutionen till spelfönstret samt ger namnet för spelet och fönstret.
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 400
windowSurface = pygame.display.set_mode ((WINDOW_WIDTH,
WINDOW_HEIGHT), 0)
icon = pygame.image.load('textures/systemicon.png')
pygame.display.set_icon(icon)
pygame.display.set_caption('Catch the rabbits!')
#Ger alla färger dess rgb koder, och vilken font och vinn texten.
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
textFont = pygame.font.SysFont("impact", 60)
textLevelOne = textFont.render("YOU THE FIRST LEVEL!", True, (193, 0, 0))
textLevelTwo = textFont.render("YOU THE SECOND LEVEL!", True, (193, 0, 0))
textLevelThree = textFont.render("YOU THE THIRD AND LAST LEVEL!/nCONGRATULATIONS!", True, (193, 0, 0))
#Spelarens och fiendernas data structurer.
#Fiendernas storlek.
#Laddar alla bitmap (.png) och ljud (.wav) filer till spelaren och fienderna
#samt bakgrundsbilderna
rabbitCounter = 0
NEW_RABBIT = 40
RABBIT_SIZE = 64
pigCounter = 0
NEW_PIG = 40
PIG_SIZE = 64
boarCounter = 0
NEW_BOAR = 40
BOAR_SIZE = 64
#"Who Likes To Party" Kevin MacLeod (incompetech.com)
#"No Frills Salsa" Kevin MacLeod (incompetech.com)
#Licensed under Creative Commons: By Attribution 3.0
#http://creativecommons.org/licenses/by/3.0/
backgroundMusicMenu = pygame.mixer.Sound('music/Who Likes To Party.ogg')
pygame.mixer.music.load('music/No Frills Salsa.ogg')
foxSound = pygame.mixer.Sound('sounds/foxSound.wav')
rabbitSound = pygame.mixer.Sound('sounds/rabbitSound.wav')
pigSound = pygame.mixer.Sound('sounds/pigSound.wav')
boarSound = pygame.mixer.Sound('sounds/boarSound.wav')
background_image = pygame.image.load('textures/bg.jpg').convert()
player = pygame.Rect(420, 100, 40, 40)
buttonStart = pygame.Rect(220, 150, 200, 90)
buttonEasy = pygame.Rect(10, 150, 200, 90)
buttonNormal = pygame.Rect(220, 150, 200, 90)
buttonHard = pygame.Rect(430, 150, 200, 90)
rabbits = []
for i in range (20):
rabbits.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- RABBIT_SIZE), random.randint (0, WINDOW_HEIGHT - RABBIT_SIZE),
RABBIT_SIZE, RABBIT_SIZE))
pigs = []
for i in range (20):
pigs.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- PIG_SIZE), random.randint (0, WINDOW_HEIGHT - PIG_SIZE),
PIG_SIZE, PIG_SIZE))
boars = []
for i in range (20):
boars.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- BOAR_SIZE), random.randint (0, WINDOW_HEIGHT - BOAR_SIZE),
BOAR_SIZE, BOAR_SIZE))
#Rörlighetsvariablerna
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
#Rörlighetshastigheten
MOVE_SPEED = 0
#Start Meny
menuMusic = False
endit is False
while endit is False:
while menuMusic == False:
backgroundMusicMenu.play()
menuMusic = True
windowSurface.fill(WHITE)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonStart.collidepoint(mouse_coordinates):
end_it = True
windowSurface.blit(get_image('textures/buttonStart.png', buttonStart)
#Svårighetsgrader
enditlevel is False
while enditlevels is False:
windowSurface.fill(WHITE)
for event in pygame.event.get():
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonEasy.collidepoint(mouse_coordinates):
MOVE_SPEED = 9
NEW_RABBIT = 40
end_it_levels = True
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonNormal.collidepoint(mouse_coordinates):
MOVE_SPEED = 7
NEW_RABBIT = 30
end_it_levels = True
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
mouse_coordinates = pygame.mouse.get_pos()
if buttonHard.collidepoint(mouse_coordinates):
MOVE_SPEED = 5
NEW_RABBIT = 20
end_it_levels = True
windowSurface.blit(get_image('textures/buttonEasy.png'), buttonEasy)
windowSurface.blit(get_image('textures/buttonNormal.png'), buttonNormal)
windowSurface.blit(get_image('textures/buttonHard.png'), buttonHard)
pygame.display.flip()
#Spel loopen
backgroundMusicMenu.stop()
levelOne = True
startSoundLevelOne = True
while levelOne == True:
while startSoundLevelOne == True:
rabbitSound.play()
foxSound.play()
pygame.mixer.music.play()
startSoundLevelOne = False
#Checkar ifall quit
movementVariables()
#Gör en loop som gör att det spawnar mera och mera kaniner
rabbitCounter += 1
if rabbitCounter >= NEW_RABBIT:
rabbitCounter = 0
rabbits.append(pygame.Rect(random.randint(0, WINDOW_WIDTH
- RABBIT_SIZE), random.randint (0, WINDOW_HEIGHT - RABBIT_SIZE),
RABBIT_SIZE, RABBIT_SIZE))
#Snöbakgrunden sätts på bakgrunden när man väljt svårighetsgrad
windowSurface.blit(get_image('textures/bg.jpg'),[0,0])
#Rörlighets mekanism så att spelaren inte går utanför skärmen samt inte går
#för fort.
movementMechanism()
#Kaninernas texture blits
for rabbit in rabbits:
windowSurface.blit(get_image('rabbitImage.png'), rabbit)
#Random movement för kaninerna
stepMovementNegativeRabbit = random.randrange(0, -3, -2)
stepMovementPositiveRabbit = random.randrange(0, 3, 2)
rabbitMovement = [((stepMovementNegativeRabbit),0), ((stepMovementPositiveRabbit), 0)
, (0, (stepMovementNegativeRabbit)), (0, (stepMovementPositiveRabbit))]
for rabbit in rabbits:
rabbit.move_ip(*random.choice(rabbitMovement))
#Checkning ifall spelaren rört en kanin
for rabbit in rabbits[:]:
if player.colliderect(rabbit):
windowSurface.blit(get_image('textures/topic_rabbit2.png'), rabbit)
windowSurface.blit(get_image('textures/Fox.png')), player)
def explosionRabbit():
for rabbit in rabbits:
if player.colliderect(rabbit) and (moveLeft == False and
moveRight == False and moveUp == False and
moveDown == False):
rabbits.remove(rabbit)
if player.colliderect(rabbit) and (moveLeft == False and
moveRight == False and moveUp == False and moveDown == False):
#timer inställningar
tRabbit = Timer(0.1, explosionRabbit)
tRabbit.start()
if len(rabbits) == 0:
rabbitCounter = 0
windowSurface.blit (get_image(textLevelOne.png), (100, 104))
levelOne = False
windowSurface.fill((0,0,0))
#Ritar fönstret
pygame.display.update()
mainClock.tick(60)
答案 0 :(得分:3)
您在此行中忘记了结尾)
:
windowSurface.blit(get_image('textures/buttonStart.png', buttonStart)
这就是下一行中出现语法错误的原因。
除此之外:你现在有很多像
这样的行endit is False
它没有做任何事情 - 或者更确切地说,它检查是否endit is False
,一个bool,然后抛出那个结果。这不会将endit
设置为False
。如果您想在此处使用endit
变量,则应使用
endit = False
while not endit:
[stuff here]