Having problems running script getting error: if event.type == pygame.QUIT: syntax Error
Question:
So I’m following this tutorial:
https://www.youtube.com/watch?v=Vg83OtRkevE
and I’ve done everything the tutorial told me to but I’m still getting this error message when I run it:
if event.type == pygame.QUIT:
syntax Error
(this was made in repl.it)
my code is:
import sys
WIN_WIDTH = 800
WIN_HEIGHT = 600
TILESIZE = 32
FPS = 60
class Spritesheet:
def __init__(self, file):
self.sheet = pygame.image.load(file).convert()
def get_sprite(self, x, y, width, height):
sprite = pygame.Surface([width, height])
sprite.blit(self.sheet, (0, 0), (x, y, width, height))
return sprite
class Ground(pygame.sprite.sprite):
def __init__(self, game, x, y):
self.game = game
self._layer = 1
self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILESIZE
self.y = y * TILESIZE
self.width, self.height = TILESIZE, TILESIZE
self.image = self.gane.terainsheet.get_sprite(0, 96, self.width, self.height)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = self.x, self.y
def build_map(self, tilemap):
for i, row in enumerate(tilemp):
for j, column in enumerate(row):
Ground(self, j, i)
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
self.clock = pygame.time.Clock()
self.running = True
self.terrainsheet = spritesheet(('terrain1.png'))
def createTilemap(self, tilemap):
build_map(self, tilemap)
def new(self, tilemap):
self.playing = True
self.all_sprites = pygame.sprite.LayeredUpdates()
self.createTilemap(tilemap)
def events(self):
for event in pygame.event.get():
if eve nt.type == pygame.QUIT:
self.playing = False
self.running = False
def update(self):
self.all_sprites.update()
def draw(self):
self.screen.fill('black')
self.all_sprites.draw(self.screen)
self.clock.tick(FPS)
pygame.display.update()
def main(self):
while self.playing:
self.Events()
self.updates()
self.draw()
TILEMAP = [
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
]
game = game()
game.new(TILEMAP)
while game.running:
game.main
pygame.quit
sys.exit()
At first it was some other syntax error I had to fix, If anyone is able to help please do!
I’m a newbie to pygame so sorry if my question seems stupid.
Answers:
Python needs indentation in order to know the scope of each section (eg. if, while, for, function, etc.). Here is comprehensive guide how to do it properly: https://peps.python.org/pep-0008/. If your code is indented as in your question. There is a problem here:
for event in pygame.event.get():
if event.type == pygame.QUIT: # This If is empty
self.playing = False # this
self.running = False # and this does not belong to that If
it must be indented according:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.playing = False
self.running = False
So I’m following this tutorial:
https://www.youtube.com/watch?v=Vg83OtRkevE
and I’ve done everything the tutorial told me to but I’m still getting this error message when I run it:
if event.type == pygame.QUIT:
syntax Error
(this was made in repl.it)
my code is:
import sys
WIN_WIDTH = 800
WIN_HEIGHT = 600
TILESIZE = 32
FPS = 60
class Spritesheet:
def __init__(self, file):
self.sheet = pygame.image.load(file).convert()
def get_sprite(self, x, y, width, height):
sprite = pygame.Surface([width, height])
sprite.blit(self.sheet, (0, 0), (x, y, width, height))
return sprite
class Ground(pygame.sprite.sprite):
def __init__(self, game, x, y):
self.game = game
self._layer = 1
self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILESIZE
self.y = y * TILESIZE
self.width, self.height = TILESIZE, TILESIZE
self.image = self.gane.terainsheet.get_sprite(0, 96, self.width, self.height)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = self.x, self.y
def build_map(self, tilemap):
for i, row in enumerate(tilemp):
for j, column in enumerate(row):
Ground(self, j, i)
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
self.clock = pygame.time.Clock()
self.running = True
self.terrainsheet = spritesheet(('terrain1.png'))
def createTilemap(self, tilemap):
build_map(self, tilemap)
def new(self, tilemap):
self.playing = True
self.all_sprites = pygame.sprite.LayeredUpdates()
self.createTilemap(tilemap)
def events(self):
for event in pygame.event.get():
if eve nt.type == pygame.QUIT:
self.playing = False
self.running = False
def update(self):
self.all_sprites.update()
def draw(self):
self.screen.fill('black')
self.all_sprites.draw(self.screen)
self.clock.tick(FPS)
pygame.display.update()
def main(self):
while self.playing:
self.Events()
self.updates()
self.draw()
TILEMAP = [
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
'............................',
]
game = game()
game.new(TILEMAP)
while game.running:
game.main
pygame.quit
sys.exit()
At first it was some other syntax error I had to fix, If anyone is able to help please do!
I’m a newbie to pygame so sorry if my question seems stupid.
Python needs indentation in order to know the scope of each section (eg. if, while, for, function, etc.). Here is comprehensive guide how to do it properly: https://peps.python.org/pep-0008/. If your code is indented as in your question. There is a problem here:
for event in pygame.event.get():
if event.type == pygame.QUIT: # This If is empty
self.playing = False # this
self.running = False # and this does not belong to that If
it must be indented according:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.playing = False
self.running = False