Function Runs 10 Times Instead of Once - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Function Runs 10 Times Instead of Once (/thread-24544.html) |
Function Runs 10 Times Instead of Once - PolarBear123 - Feb-18-2020 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: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: def check_levels(): global level if level <= 10: level += 1 if level == 10: gameStatus == 2score 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: 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() RE: Function Runs 10 Times Instead of Once - michael1789 - Feb-18-2020 Your problem is in your when you hit 100 points you check_level()every frame and add 1 to the level each frame. The way I can think to fix it is to have a variable for next_level and have it look to hit that. So when you get 100 points, you level up and then add 100 points to the next_level variable.Something like this def level_up() level += 1 next_level += 100 if score >= next_level: level_up() level_up() would be a good place to add a sound for level change too. Will make it more fun for you to test. |