User avatar
PolarBear123
Posts: 58
Joined: Sat Sep 09, 2017 1:26 am
Location: Westchester County, NY, USA

Function Repeats itself 10 Times Instead of Once!

Tue Feb 18, 2020 6:26 pm

I have written a simple racing game in Pygame Zero. This comes from the MagPi "Retro Gaming with Raspberry Pi" Book. I am a beginner programmer so I might be asking an obvious question. I want to make levels. For every 100 score, level + 1. Win at 10 levels.

areas of interest might be my `draw()` function:

Code: Select all

def draw():
    global gameStatus, level
    screen.fill((128, 128, 128))
    if gameStatus == 0:
        car.draw()
        b = 0
        while b < len(trackLeft):
            trackLeft[b].draw()
            trackRight[b].draw()
            b += 1
        screen.draw.text("Score: " + str(score), (50, 30), color="black")
        if score % 100 == 0:
            check_levels()
        screen.draw.text("Level: " + str(level), (50, 50), color="black")
    if gameStatus == 1:
        screen.blit('rflag', (318, 268))
    if gameStatus == 2:
        screen.blit('cflag', (318, 268))
and my `level_up()` function:

Code: Select all

def check_levels():
    global level
    if level <= 10:
        level += 1
    if level == 10:
        gameStatus == 2
score is counted for every one piece of track made there is one higher score.

My problem: When I get to 100 score, level is added by 10, not by one. I don't know what seems to happen. Might it be that for every score there are a few FPS/ticks, and that as level is updated every tick, it appears that when the score hangs on 100 for about 10 ticks, the level gets update every tick?

As my program is quite short, I'll just go ahead and post all the code:

Code: Select all

import time
from random import randint
import pygame
import pgzrun


WIDTH = 700
HEIGHT = 800


car = Actor("racecar")
car.pos = 250, 700
SPEED = 4
trackLeft = []
trackRight = []
trackCount = 0
trackPosition = 250
trackWidth = 120
trackDirection = False
gameStatus = 0
score = 0
level = 1


def draw():
    global gameStatus, level
    screen.fill((128, 128, 128))
    if gameStatus == 0:
        car.draw()
        b = 0
        while b < len(trackLeft):
            trackLeft[b].draw()
            trackRight[b].draw()
            b += 1
        screen.draw.text("Score: " + str(score), (50, 30), color="black")
        if score % 100 == 0:
            check_levels()
        screen.draw.text("Level: " + str(level), (50, 50), color="black")
    if gameStatus == 1:
        screen.blit('rflag', (318, 268))
    if gameStatus == 2:
        screen.blit('cflag', (318, 268))


def update():
    global gameStatus, trackCount
    if gameStatus == 0:
        if keyboard.left: car.x -= 2
        if keyboard.right: car.x += 2
        update_track()


def make_track():
    global trackCount, trackLeft, trackRight, trackPosition, trackWidth, score
    trackLeft.append(Actor("barrier", pos=(trackPosition - trackWidth, 0)))
    trackRight.append(Actor("barrier", pos=(trackPosition + trackWidth, 0)))
    trackCount += 1
    score += 1


def update_track():
    global trackCount, trackPosition, trackDirection, trackWidth, gameStatus
    b = 0
    while b < len(trackLeft):
        if car.colliderect(trackLeft[b]) or car.colliderect(trackRight[b]):
            gameStatus = 1
        trackLeft[b].y += SPEED
        trackRight[b].y += SPEED
        b += 1
    if trackLeft[len(trackLeft) - 1].y > 32:
        if trackDirection == False: trackPosition += 16
        if trackDirection == True: trackPosition -= 16
        if randint(0, 4) == 1: trackDirection = not trackDirection
        if trackPosition > 700 - trackWidth: trackDirection = True
        if trackPosition < trackWidth: trackDirection = False
        make_track()


def check_levels():
    global level
    if level <= 10:
        level += 1
    if level == 10:
        gameStatus == 2


make_track()


pgzrun.go()
Raspberry Pi's are cool and so are Polar Bears!

jalih
Posts: 124
Joined: Mon Apr 15, 2019 3:54 pm

Re: Function Repeats itself 10 Times Instead of Once!

Tue Feb 18, 2020 7:20 pm

PolarBear123 wrote:
Tue Feb 18, 2020 6:26 pm
My problem: When I get to 100 score, level is added by 10, not by one. I don't know what seems to happen.
Seems quite obvious: as long as score mod 100 is zero, you add one to level. I would just give one bonus point when level is incremented and your problem should just go away...

There seems also be comparison instead of assignment on at least one place, so check your code...

User avatar
paddyg
Posts: 2529
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Function Repeats itself 10 Times Instead of Once!

Tue Feb 18, 2020 7:27 pm

Yes. Your gameStatus variable would stop it re-running but unless you also set that to global your function won't change its value (and the == != = issue). Globals are a bit of a nightmare.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
PolarBear123
Posts: 58
Joined: Sat Sep 09, 2017 1:26 am
Location: Westchester County, NY, USA

Re: Function Repeats itself 10 Times Instead of Once!

Tue Feb 18, 2020 10:53 pm

Yeah, a little typo there. Also, I figured out the problem. I have to add the score % 100 thingy in the make_track() before score += 1, not in draw, as draw is executed more often.
Raspberry Pi's are cool and so are Polar Bears!

ankith26
Posts: 240
Joined: Mon Mar 25, 2019 11:08 am
Location: /home/pi/pythonprojects/test.py
Contact: Website

Re: Function Repeats itself 10 Times Instead of Once!

Wed Feb 19, 2020 3:40 am

In your draw() function, there is no need to define the variables as global, this is because the function can read for variable outside to function.
But you cannot change the variable inside to function (doing this will not raise error but will create a new local variable for the function)
I sat thinking for 5 minutes on what to put here. Finally I put something like this.
Check out my github page @ https://github.com/ankith26

Return to “Python”