Score system based on a timer doesn't work in Pygame

201 Views Asked by At

So, me and a few friends are working on a Python Project for school and we want to implement a score system in which the score gets bigger every few seconds. We have already searched the web for possible solutions, but we couldn't find anything that worked for us.

For now this is our code:

import pgzrun
import pygame
import time
import random

TITLE = "___Title___"
WIDTH = 800
HEIGHT = 1180
CENTRE_X = WIDTH / 2
CENTRE_Y = HEIGHT / 2
game_over = False
finalised = False
xpos_list = [225,344,463,582]
autofarbe_list = ["carblau","carrot","cargruen"]

class Vehicle(Actor):
    def __init__(self,name):
        super().__init__(name)
        self.vy = random.randint(8,10)
        self.lane = random.randint(0,len(xpos_list)-1)
        self.x = xpos_list[self.lane]
        self.y = -50
        self.vmax = 10
    def move(self):
        self.y += self.vy
    def farbwechsel(self):
        self.image = random.choice(autofarbe_list)
    def update(self):
        self.draw()
    def accelerate(self):
        self.vy += 1
        if self.vy >= self.vmax:
            self.vy = self.vmax

class Player(Vehicle):
    def __init__(self, name):
        super().__init__(name)
        self.lane = random.randint(0,len(xpos_list)-1)
        self.x = xpos_list[self.lane]
    def timer():
        global score, seconds
        score = 0
        seconds = 0
        start_ticks = pygame.time.get_ticks()
        while not game_over:
            seconds = (pygame.time.get_ticks()-start_ticks)/1000
            if seconds > 10:
                score = score + 1
                seconds = 0

class Car(Vehicle):
    def __init__(self,name):
        super().__init__(name)

player = Player("player")
player.pos = 338, 900

cars = []
for i in range(4):
    cars.append(Car(random.choice(autofarbe_list)))

def update():
    player.update()
    player.timer()
    for car in cars:
        car.move()
    if not game_over:
        if keyboard.left and player.x > 220:
            player.x -= 7
        elif keyboard.right and player.x < 582:
            player.x += 7
        for car in cars:
            car.draw()
            if car.y > 1300:
                car.y = -50
                car.farbwechsel()
                car.x = random.choice(xpos_list)

def draw():
    screen.clear()
    screen.blit("strasselang", (0, 0))
    for car in cars:
        car.draw()
    for m in cars:
        player.draw()
    screen.draw.text(
        "Progress: " +
        str(score) + " Punkte",
        topleft=(10, 10), color="black")

pgzrun.go()

How could this be fixed?

1

There are 1 best solutions below

0
On

What I would do, is, define a variable that will count the amount of ticks between an event. Every frame in your while loop, you would check if the variable was equal to whatever value you want, and add the score, otherwise you would add x to that variable (for example, if I wanted to add one every second, and I was running 60 FPS, I would add 1/60 every frame).