0

In this section of code I test:

       if y <= o_y + 160:

        if x >= o_x and x <= o_x + 80 or x + 60 > o_x and x + 60 < o_x + 80:

This basically tests for a collision between two different objects (A player based object [x] and a falling object [o_x(1,2)]) if it detects a collision it will display a 'game over' picture and take the player back to the main menu, it also sets all the objects y axis to 0. Despite this when you hit GO (play) it returns you to the same situation and disregards my code that should reset the situation (moving the falling objects back to the top of the screen [global y_o, o_y = 0])

I am new to python so an explanation is greatly appreciated

def fallingObject():
global car
car = a
global car1
car1 = c
global car2
car2 = e

objectSpawn = True
while objectSpawn:
    if o_y < height:
        global o_y
        o_y += +1
        global o_y1
        o_y1 += +1
        global o_y2
        o_y2 += +1
        objectSpawn = False
        blit_screen()
    if o_y >= height:
        global o_x1
        o_x1 = random.randrange(220, 450)
        global o_y1
        o_y1 = 0
        global o_x2
        o_x2 = random.randrange(450, 680)
        global o_y2
        o_y2 = 0
        global o_x
        o_x = random.randrange(-10, 220)                                   
        global o_y
        o_y = 0
        global car_s
        car_s = random.randrange(1,6)
        global car_s1
        car_s1 = random.randrange(1,6)
        global car_s2
        car_s2 = random.randrange(1,6)
        objectSpawn = False
        blit_screen()

    if y + 120 < o_y:
        print("y cross over")

    if y <= o_y + 160:

        if x >= o_x and x <= o_x + 80 or x + 60 > o_x and x + 60 < o_x + 80:
            screen.blit(crash, (0,0))
            pygame.display.flip()
            pygame.time.delay(3000)
            game_intro()
            global o_y
            o_y = 0
            global o_y1
            o_y1 = 0
            global o_y2
            o_y2 = 0
            blit_screen()

    if y + 120 < o_y1:
        print("y cross over")

    if y <= o_y1 + 160:

        if x >= o_x1 and x <= o_x1 + 80 or x + 60 > o_x1 and x + 60 < o_x1 + 80:
            screen.blit(crash, (0,0))
            pygame.display.flip()
            pygame.time.delay(3000)
            game_intro()
            global o_y
            o_y = 0
            global o_y1
            o_y1 = 0
            global o_y2
            o_y2 = 0
            blit_screen()


    if y + 120 < o_y2:
        print("y cross over")

    if y <= o_y2 + 160:

        if x >= o_x2 and x <= o_x2 + 80 or x + 60 > o_x2 and x + 60 < o_x2 + 80:
            screen.blit(crash, (0,0))
            pygame.display.flip()
            pygame.time.delay(3000)
            game_intro()
            global o_y
            o_y = 0
            global o_y1
            o_y1 = 0
            global o_y2
            o_y2 = 0
            blit_screen()


def blit_screen():

    if car_s == 1:
        car = a
    else:
        if car_s == 2:
            car = b
        else:
            if car_s == 3:
                car = c
            else:
                if car_s == 4:
                    car = d
                else:
                    if car_s == 5:
                        car = e
                    else:
                        print(" ")

    if car_s1 == 1:
        car1 = a
    else:
        if car_s1 == 2:
            car1 = b
        else:
            if car_s1 == 3:
                car1 = c
            else:
                if car_s1 == 4:
                    car1 = d
                else:
                    if car_s1 == 5:
                        car1 = e
                    else:
                        print(" ")

    if car_s2 == 1:
        car2 = a
    else:
        if car_s2 == 2:
            car2 = b
        else:
            if car_s2 == 3:
                car2 = c
            else:
                if car_s2 == 4:
                    car2 = d
                else:
                    if car_s2 == 5:
                        car2 = e
                    else:
                        print(" ")

    screen.fill(blue)
    screen.blit(road, (0,0))
    screen.blit(car, (o_x,o_y))
    screen.blit(car1, (o_x1,o_y1))
    screen.blit(car2, (o_x2,o_y2))
    screen.blit(carImg, (x,y))
    pygame.display.flip()

clock.tick(60)
game_intro()
Larry
  • 117
  • 2
  • 13
  • 6
    Friendly tip: no one is going to want to read through so much horrendous code to dig up the problem. – TigerhawkT3 Dec 28 '15 at 09:42
  • put all `global` at the beginning of function to make it more readable. – furas Dec 28 '15 at 10:36
  • use `elif` in place of `else: if` – furas Dec 28 '15 at 10:38
  • if you use list with images then you can use one `car = images[car_s]` in place of all `if/else`. btw: I saw your previous code with list of images and I can say you don't know how to use list. – furas Dec 28 '15 at 10:41
  • 1
    Don't put all `global` declarations at the beginning of functions but learn object oriented programming and then don't use `global` at all. It's evil™. Also make better use of *Pygame*. There are `Rect` and `Sprite` types in that package. You don't have to spread coordinates and dimensions of graphics over different variables and you don't have to code collision tests yourself. – BlackJack Dec 28 '15 at 13:01
  • Also, for the code if x >= o_x and x <= o_x + 80 or x + 60 > o_x and x + 60 < o_x + 80, add some parentheses. You've got ors and ands, and no good way to tell which have priority. – user3757614 Dec 29 '15 at 01:15

0 Answers0