Posts: 4
Threads: 1
Joined: Dec 2017
Dec-29-2017, 02:47 PM
(This post was last modified: Dec-29-2017, 02:54 PM by Hyperimus.)
Greetings, I'm new here as I only now started developing in python due to a college class, and for this one as a end semester group work I need to develop a game, however simple it is. And for that I download a pygame called PyCatch-master and am using it's code as base since it is very close to what I need. But to simplify it's code even further (so that I don't have to risk explaining things that I do not entirely understand, or my colleague who understands far less than I) I've deleted a few lines of code to try and make the game as simple as possible. No menu, no levels, just open and play straight away. And I might even make it simpler without high-scores or lives (pointless game but would work for what is asked.
But enough background, I'm getting a problem like this.
Quote:C:\Users\user\Desktop\PyCatch-master> PyCatch.py
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:263: Syntaxwarning: name ‘lives’ is assigned to before global declaration global lives
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:266: Syntaxwarning: name ‘InGame’ is assigned to before global declaration global InGame
C:\Users\user\Oesktop\PyCatch-master>
To try and fix this I tried changing the line 31 code to global lives = 5 but then this happens
Quote:C:\Users\user\Desktop>cd PyCatch-master
C:\Users\user\Desktop\PyCatch-master> PyCatch.py
File "C:\Useps\usGp\Desktop\PyCatch-mastGp\PyCatch.py"
global lives = 5
>\
SyntaxEppop: invalid syntax C:\UsGPs\usGp\DGsktop\PyCatch-mastGP>
line 31
The files I am working with can be retrieved here.
Many thanks in advance to whoever tries to help me.
Posts: 3,458
Threads: 101
Joined: Sep 2016
The syntax global {variablename} is used to hint that a variable exists in the global scope, not within a block's scope. You can't add other things to that, that's the only thing it can do. So you can't assign a value to it at the same time that you're hinting that it exists somewhere else.
Try this instead: global lives
lives = 5
Posts: 4
Threads: 1
Joined: Dec 2017
Thanks fore the reply, I tried what you said, sadly however it still gives me the first error about the global lives and ingame. e.e god damn python :c
Posts: 3,458
Threads: 101
Joined: Sep 2016
What's the error? Please copy/paste the entire traceback.
Posts: 4
Threads: 1
Joined: Dec 2017
Quote:C:\Users\user\Desktop\PyCatch-master> PyCatch.py
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:263: Syntaxwarning: name ‘lives’ is assigned to before global declaration global lives
C:\Users\user\Desktop\PyCatch-master\PyCatch.py:266: Syntaxwarning: name ‘InGame’ is assigned to before global declaration global InGame
C:\Users\user\Oesktop\PyCatch-master>
This is the error but since I changed the
Quote:lives = 5
to Quote:global lives
Lives = 5
the lines are 264 and 267 (1 more)
Posts: 591
Threads: 26
Joined: Sep 2016
Whole program please. Also, you shouldn't be using global for non-constants in the first place.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Dec-29-2017, 06:13 PM)Hyperimus Wrote: global lives
Lives = 5
lives is not the same as Lives . Capitalization matters.
But that's unrelated. Just set it to whatever the starting value is at the top of the file, right under your imports.
Posts: 4
Threads: 1
Joined: Dec 2017
(Dec-29-2017, 06:39 PM)Mekire Wrote: Whole program please. Also, you shouldn't be using global for non-constants in the first place.
It's on my first reply, on a link for download.
Posts: 3,458
Threads: 101
Joined: Sep 2016
Quote:# 1 - Import library
import pygame
import random
import time
import threading
import sys
import dumbmenu as dm
from threading import Thread
from operator import itemgetter
from pygame.locals import *
#Initial Declarations
red = 255, 0, 0
green = 0, 255, 0
blue = 0, 0, 255
white = 255, 255, 255
GAMEPHASE = 1 #0 = menu, 1=game, 2=other menu item
size = width, height = 800, 500
screen = pygame.display.set_mode((width, height))
screen = pygame.display.set_mode((1920, 1080))
background = pygame.image.load("resources/background.jpg")
pygame.mouse.set_visible(False)
score = 0
newObj = False
lives = 5
Running = True
speed = [0, 20]
inscores = False
invert = False
leveltext = "Easy"
def changeMode(levelmode):
global speed
global leveltext
if levelmode == 0: #easy
speed = [0,2]
leveltext = "Easy"
elif levelmode == 1: #medium
speed = [0,4]
leveltext = "Medium"
elif levelmode == 2: #hard
speed = [0,6]
leveltext = "Hard"
elif levelmode == 3: #EXTREME
speed = [0,8]
leveltext = "EXTREME"
class Oneup(pygame.sprite.Sprite):
def __init__(self,x):
pygame.sprite.Sprite.__init__(self)
self.oneuprect = self.oneup.get_rect()
self.oneuprect = self.oneuprect.move(x, -50)
self.rect = self.oneuprect
def update(self):
if self.rect.bottom > 920:
self.kill()
self.oneuprect = self.oneuprect.move(speed)
screen.blit(self.oneup, self.oneuprect)
self.rect = self.oneuprect
class Ball(pygame.sprite.Sprite):
def __init__(self,x):
pygame.sprite.Sprite.__init__(self)
self.ballrect = self.ball.get_rect()
self.ballrect = self.ballrect.move(x, -50)
self.rect = self.ballrect
global newObj
newObj = False
def update(self):
if self.rect.bottom > 920:
self.kill()
self.ballrect = self.ballrect.move(speed)
screen.blit(self.ball, self.ballrect)
self.rect = self.ballrect
class Bomb(pygame.sprite.Sprite):
def __init__(self,x):
pygame.sprite.Sprite.__init__(self)
self.bombrect = self.bomb.get_rect()
self.bombrect = self.bombrect.move(x, -50)
self.rect = self.bombrect
def update(self):
if self.rect.bottom > 920:
self.kill()
self.bombrect = self.bombrect.move(speed)
screen.blit(self.bomb, self.bombrect)
self.rect = self.bombrect
def explode(self):
global explosionobj
explosionobj = Explosion(self.rect.topleft)
explosionobj.explode()
class Explosion(pygame.sprite.Sprite):
def __init__(self, coordinates):
pygame.sprite.Sprite.__init__(self)
self.Exploding = False
self.explosionindex = 0
self.x, self.y = coordinates
global explosionimgs
self.explosionimgs = []
for i in range(0, 47):
self.explosionimgs.append(pygame.image.load("resources/explosion2/%d.png" % (i)))
self.explosionimg = self.explosionimgs[self.explosionindex]
global explosionfx
explosionfx = pygame.mixer.Sound("resources/explosion.wav")
def explode(self):
explosionfx.play()
self.Exploding = True
self.explosionindex = 0
def update(self):
if self.Exploding and self.explosionindex < 47:
screen.blit(self.explosionimgs[self.explosionindex], (self.x-80, self.y-80))
self.explosionindex += 1
if self.explosionindex == 47:
self.kill()
def stopExploding(self):
self.Exploding = False
self.kill()
class Bucket(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.bucketrect = self.bucket.get_rect()
#self.bucketrect = self.bucketrect.move(50,50)
self.rect = self.bucketrect
def update(self):
x, y = pygame.mouse.get_pos()
self.rect.midtop = (x,800)
screen.blit(self.bucket, self.rect)
self.rect = self.bucketrect
class Lives(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.livesimg = pygame.image.load("resources/heart.png")
self.livesrect = self.livesimg.get_rect()
self.rect = self.livesrect
def update(self):
for i in range(lives):
screen.blit(self.livesimg, (i*26+1600, 50))
class Timer():
def __init__(self, seconds):
print "Timer started"
self.seconds = seconds
def start(self):
global newObj
newObj = True
self.t = threading.Timer(self.seconds,self.start)
self.t.start()
def stop(self):
self.t.cancel()
def blowWind(on):
global speed
if on:
speed[0] = random.choice([-3,3])
elif not on:
speed[0] = 0
pygame.mixer.init()
explosionobj = Explosion([0,0])
pygame.mixer.music.load("resources/happy.mp3")
def main():
#Init screen
pygame.init()
global score
global explosionobj
global Running
InGame = False
pygame.key.set_repeat(500, 30)
bg = background
myfont = pygame.font.SysFont("monospace", 38)
ballgroup = pygame.sprite.Group()
bombgroup = pygame.sprite.Group()
oneupgroup = pygame.sprite.Group()
bucketobj = Bucket()
livesobj = Lives()
#timer = Timer(1) #every 1 second newObj is set to True
#timer.start()
blowWind(False)
pygame.mixer.music.play(-1)
while Running:
if InGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
Running = False
break
# while InGame:
# for event in pygame.event.get():
# if event.type == QUIT:
# timer.stop()
# pygame.quit()
# return
# global lives
# if lives == 0:
# InGame = False
scoretext = myfont.render("Score {0}".format(score), 1, (0, 0, 0))
diftext = myfont.render("{0}".format(leveltext), 1, (0, 0, 0))
if random.randrange(0, 70) < 1:
ballgroup.add(Ball(random.randint(0,1920)))
if random.randrange(0,50) < 1:
bombgroup.add(Bomb(random.randint(0,1920)))
if random.randrange(0,1000) < 1:
oneupgroup.add(Oneup(random.randint(0,1920)))
if invert:
screen.blit(background_inverted, (0, 0))
elif not invert:
screen.blit(background, (0, 0))
screen.blit(scoretext, (5, 10))
screen.blit(diftext, (1750, 900))
ballgroup.update()
bombgroup.update()
bucketobj.update()
livesobj.update()
oneupgroup.update()
explosionobj.update()
pygame.display.update()
if pygame.sprite.spritecollide(bucketobj, oneupgroup, True):
lives += 1
if pygame.sprite.spritecollide(bucketobj, ballgroup, True):
score += 1
list = pygame.sprite.spritecollide(bucketobj, bombgroup, True)
for i in list:
if i is not None:
i.explode()
lives -= 1
global lives
if lives == 0:
#Score stuff
global InGame
InGame = False
gameovertext = myfont.render("Game Over", 1, (0, 0, 0))
for i in range(0,47):
screen.blit(bg, (0, 0))
explosionobj.update()
pygame.display.update()
screen.blit(gameovertext, (350,200))
explosionobj.stopExploding()
#Kill Sprites
for ball in ballgroup:
ball.kill()
for bomb in bombgroup:
bomb.kill()
for oneups in oneupgroup:
oneups.kill()
pygame.display.update()
time.sleep(2)
elif not InGame:
if inscores:
highscores()
elif not inscores:
menu()
Ok, so you're trying to use the variable, before you let python know where that variable is.
for i in list:
if i is not None:
i.explode()
lives -= 1
global lives
if lives == 0: Move the global declaration to the top of the function.
And then, when you start writing real code, never use globals again lol. It's one of the worst things to do, right next to "goto" abuse.
|