-2

I am writing a small dodging game and my idea was to create a kind of power up,that would reset the objects(in this case Spikeball) to the starting point and make them twice as big.When the player hits the block it prints out the 'hit',but somehow the values don't change!I am using python 2.7.

Here is my code:

import pygame
import os
import shutil
import time
from PIL import Image,ImageDraw
import random

    pygame.init()

    display_hight = 1100
    display_width = 1100

    h=0
    g=0
    r=0
    t=0
    j=0
    k=0
    m=0
    l=0
    q=0
    w=0
    f=0
    s=0

    black = (0,0,0)
    white = (255,255,255)
    red = (255,0,0)

    gameDisplay = pygame.display.set_mode((display_width,display_hight))
    pygame.display.set_caption("ZOMPS")
    clock = pygame.time.Clock()

    mydir = os.path.dirname('/home/arne/Desktop/Python-project/Game/Demonsave.png')
    playerImg = pygame.image.load(os.path.join(mydir,'Demonsave.png'))
    playerImg = pygame.transform.scale(playerImg,(180,200))

    mydir1 = os.path.dirname('/home/arne/Desktop/Python-project/Block.png')
    blockImg = pygame.image.load(os.path.join(mydir1,'Block.png'))


    def spikeball(m,l,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[m,l,spike_width,spike_hight])

    def spikeball2(j,k,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[j,k,spike_width,spike_hight])

    def spikeball3(q,w,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[q,w,spike_width,spike_hight])

    def spikeball1(f,s,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[f,s,spike_width,spike_hight])

    def spikeball4(r,t,spike_width,spike_hight,color):
        pygame.draw.ellipse(gameDisplay,color,[r,t,spike_width,spike_hight])

    def power():
            p=1
            if p == 1:
                    spike_width=160
                    spike_hight=160
                    q=-100
            w=random.randrange(90,display_width-90)
            r=random.randrange(90,display_width-90)
                t=-200
                f=random.randrange(90,display_width-90)
                s=-400
                start_m=random.randrange(90,display_width-90)
                start_l =-800

            else:
                    print('fail') 


    def lost():
        message_display('You lost!')

    def text_objects(text,font):
        textSurface = font.render(text,True,black)
        return textSurface,textSurface.get_rect()

    def message_display(text):
        largeText = pygame.font.Font('freesansbold.ttf',50)
        TextSurf,TextRect =text_objects(text,largeText)
        TextRect.center = (display_width/2,display_hight/2)
        gameDisplay.blit(TextSurf,TextRect)
        pygame.display.update()
        time.sleep(2)
        game_loop()

    def player(x,y):
        gameDisplay.blit(playerImg,(x,y))

    def block(g,h):
        gameDisplay.blit(blockImg,(g,h))

    def game_loop():
        j=random.randrange(0,display_width)
        k=-300
        x = display_width*0.4
        y = display_hight*0.75
        x_change=0
        f=random.randrange(90,display_width-90)
        s=-400
        start_m=random.randrange(90,display_width-90)
        start_l =-800
        spike_speed=random.randrange(5,10)
        spike_speed3=random.randrange(5,10)
        spike_speed2=random.randrange(5,10)
        spike_speed1=random.randrange(5,10)
        spike_speed4=random.randrange(5,10)
        spike_width=80
        spike_hight=80
        w=-100
        q=random.randrange(0,display_width)
        r=random.randrange(90,display_width-90)
        t=-200
        g=random.randrange(90,display_width-90)
        h=-1300
        block_speed=random.randrange(3,5)
        GameExit = False

        while not GameExit:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    GameExit =True
                elif event.type == pygame.KEYDOWN: 
                    if event.key == pygame.K_d:
                         x_change+=4
                    elif event.key == pygame.K_a:
                         x_change-=4
                    elif event.key == pygame.K_ESCAPE:
                        quit()
                elif event.type == pygame.KEYUP:
                    if event.key== pygame.K_a or pygame.K_d:
                        x_change=0
            if y+50 < h+40 and y+160 >h:
                        if x+60 >g and x+60 < g+40 or x+101 > g and x + 101 < g+40:
                            power()
                            print('hit')

            x+=x_change
            k+=spike_speed2
            s+=spike_speed3
            q+=spike_speed1
            t+=spike_speed4
            gameDisplay.fill(red)
            player(x,y)
            block(g,h)
            spikeball(start_m,start_l,spike_width,spike_hight,black)
            spikeball1(w,q,spike_width,spike_hight,black)
            spikeball2(j,k,spike_width,spike_hight,black)
            spikeball3(f,s,spike_width,spike_hight,black)
            spikeball3(r,t,spike_width,spike_hight,black)
            start_l+=spike_speed
            h+=block_speed

            if x > display_width or x < 0:
                x = display_width*0.4
                y = display_hight*0.75

            elif k > display_hight:
                j=random.randrange(90,display_width-90) 
                k=-100
                spike_speed2=random.randrange(6,12) 

            elif h > display_hight:
                g=random.randrange(90,display_width-90) 
                h=-900
                block_speed=random.randrange(1,6) 

            elif t > display_hight:
                r=random.randrange(90,display_width-90) 
                t=-100
                spike_speed4=random.randrange(6,12) 

            elif start_l > display_hight:
                start_l =-100
                start_m=random.randrange(90,display_width-90) 
                spike_speed=random.randrange(5,10) 

            elif s > display_hight:
                s=-100
                f=random.randrange(90,display_width-90)
                spike_speed3=random.randrange(6,12)

            elif q > display_hight:
                q=-100
                w=random.randrange(90,display_width-90)
                spike_speed1=random.randrange(6,12)

            elif y+50 < start_l+spike_hight and y+160 >start_l:
                if x+60 >start_m and x+60 < start_m+spike_width or x+101 > start_m and x + 101 < start_m+spike_width:
                    lost()
            elif y+50 < k+spike_hight and y+160 >k:
                if x+60 >j and x+60 < j+spike_width or x+101 > j and x + 101 <j+spike_width:
                    lost()
            elif y+50 < s+spike_hight and y+160 >s:
                if x+60 >f and x+60 < f+spike_width or x+101 > f and x + 101 < f+spike_width:
                    lost()
            elif y+50 < q+spike_hight and y+160 >q:
                if x+60 >w and x+60 < w+spike_width or x+101 > w and x + 101 < w+spike_width:
                    lost()
            elif y+50 < t+spike_hight and y+160 >t:
                if x+60 >r and x+60 < r+spike_width or x+101 > r and x + 101 < r+spike_width:
                    lost()

            pygame.display.update() 
            clock.tick(60)


    game_loop()
    pygame.quit()
    quit()

Thank you for your help!

arno
  • 15
  • 1
  • Stackoverflow is not a debugging service. If you want to ask a question you have to create a [mcve]. We cannot read through 200+ lines of code to find an error for you. – Ted Klein Bergman May 27 '17 at 15:58

1 Answers1

1

Your indentation doesn't look right but it looks like you are trying to set a global variable inside a function.

As seen here, you can use this:

globvar = 0

def set_globvar_to_one():
    global globvar    # Needed to modify global copy of globvar
    globvar = 1

def print_globvar():
    print(globvar)     # No need for global declaration to read value of globvar

set_globvar_to_one()
print_globvar()       # Prints 1

But using global variables is a bad practice and you might be better of using classes. Python is an object oriented language and it is very easy to use. You can find the documentation here.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50