2

I"m making a game with a friend and I'm having trouble putting a timer display in my screen, the timer needs to count up any ideas? i tried putting up a simple timer using an easy code which counts up (outside the while loop) n = 1 (inside the while loop) n = n + 1, sleep (1) but instead of it just made my game slower, what I mean is as the timer goes up the game frame moves only then.

import pygame
import math
import random
from time import sleep
pygame.init()#The speed kept on increasing because this line code was missing
sw = 800
sh = 900


bg = pygame.image.load("Bulletspix/Grass BG.png")
turkey = pygame.image.load("Bulletspix/Defaturk.png")
bullet1 = pygame.image.load("Bulletspix/bullet1.png")
bullet2 = pygame.image.load("Bulletspix/bullet2.png")
bullet3 = pygame.image.load("Bulletspix/bullet3.png")

pygame.display.set_caption("Turkey run")
win = pygame.display.set_mode((sw, sh))

clock = pygame.time.Clock()
gameover = False
lives = 4




class Player(object):
    def __init__(self):
        self.img = turkey
        self.w = self.img.get_width()
        self.h = self.img.get_height()
        self.x = sw // 2
        self.y = sh // 2
        self.angle = 0
        self.rotatedSurf = pygame.transform.rotate(self.img, self.angle)
        self.rotatedRect = self.rotatedSurf.get_rect()
        self.rotatedRect.center = (self.x, self.y)
        self.cosine = math.cos(math.radians(self.angle + 90))
        self.sine = math.sin(math.radians(self.angle + 90))
        self.head = (self.x + self.cosine * self.w//2, self.y - self.sine * self.h//2)


    def draw(self, win):
        #win.blit(self.img, [self.x, self.y, self.w, self.h])
        win.blit(self.rotatedSurf, self.rotatedRect)

    def turnleft(self):
        self.angle += 6
        self.rotatedSurf = pygame.transform.rotate(self.img, self.angle)
        self.rotatedRect = self.rotatedSurf.get_rect()
        self.rotatedRect.center = (self.x, self.y)
        self.cosine = math.cos(math.radians(self.angle + 90))
        self.sine = math.sin(math.radians(self.angle + 90))
        self.head = (self.x + self.cosine + self.w//2, self.y - self.sine * self.h//2)

    def turnright(self):
        self.angle -= 6
        self.rotatedSurf = pygame.transform.rotate(self.img, self.angle)
        self.rotatedRect = self.rotatedSurf.get_rect()
        self.rotatedRect.center = (self.x, self.y)
        self.cosine = math.cos(math.radians(self.angle + 90))
        self.sine = math.sin(math.radians(self.angle + 90))
        self.head = (self.x + self.cosine + self.w // 2, self.y - self.sine * self.h // 2)

    def moveforward(self):
        self.x += self.cosine * 6
        self.y -= self.sine * 6
        self.rotatedSurf = pygame.transform.rotate(self.img, self.angle)
        self.rotatedRect = self.rotatedSurf.get_rect()
        self.rotatedRect.center = (self.x, self.y)
        self.cosine = math.cos(math.radians(self.angle + 90))
        self.sine = math.sin(math.radians(self.angle + 90))
        self.head = (self.x + self.cosine + self.w // 2, self.y - self.sine * self.h // 2)
    def movedown(self):
        self.x -= self.cosine * 6
        self.y += self.sine * 6
        self.rotatedSurf = pygame.transform.rotate(self.img, self.angle)
        self.rotatedRect = self.rotatedSurf.get_rect()
        self.rotatedRect.center = (self.x, self.y)
        self.cosine = math.cos(math.radians(self.angle + 90))
        self.sine = math.sin(math.radians(self.angle + 90))
        self.head = (self.x + self.cosine + self.w // 2, self.y - self.sine * self.h // 2)

        #looping border
    def updatelocation(self):
        if self.x >sw + 50:
            self.x = 0
        elif self.x < 0 + - self.w:
            self.x = sw
        elif self.y <- 50:
            self.y = sh
        elif self.y > sh + 50:
            self.y = 0





class Bullet(object):
    def __init__(self, rank):
        self.rank = rank
        if self.rank == 1:
            self.image = bullet1
        elif self.rank == 2:
            self.image = bullet2
        else:
            self.image = bullet3
        self.w = 25 * rank #Bullet size of collission to the player
        self.h = 25 * rank
        self.ranPoint = random.choice([(random.randrange(0, sw - self.w), random.choice([-1 *self.h -5, sh + 5])),  (random.choice([-1 * self.w - 5, sw + 5]), random. randrange(0, sh - self.h))])
        self.x, self.y = self.ranPoint
        if self.x < sw//2:
            self.xdir = 8
        else:
            self.xdir = -8
        if self.y < sh //2:
            self.ydir = 8
        else:
            self.ydir = -8
        self.xv = self.xdir * random.randrange(1, 3)
        self.yv = self.ydir * random.randrange(1, 3)
    def draw(self, win):
        win.blit(self.image, (self.x, self.y))


def GameWindow():
    win.blit(bg, (0, 0))
    font = pygame.font.Font("Gameplay.ttf", 40)
    font2 = pygame.font.Font("Gameplay.ttf", 30)
    Livestext = font.render("lives: " +str (lives), 1, (255,0,0))
    Title = font2.render("RUN TURKEY!! ",1, (255, 0, 0))
    GameoverTxt = font.render("GAME OVER Restart Game ",1, (0,255,100))

    player.draw(win)
    for a in bullet:
        a. draw(win)

    if gameover:
        win.blit(GameoverTxt, (sw//2-GameoverTxt.get_width()//2, sh//2 -GameoverTxt.get_height()//2))
    win.blit(Livestext,(25, 850))
    win.blit(Title, (300, 30))
    pygame.display.update()



player = Player()
bullet = []
count = 0

run = True
while run:


    clock.tick(60)
    count += 1
    if not gameover:
        player.updatelocation()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            player.turnleft()
        if keys[pygame.K_RIGHT]:
            player.turnright()
        if keys[pygame.K_UP]:
            player.moveforward()
        if keys[pygame.K_DOWN]:
            player.movedown()





        if count % 50 == 0:
            ran = random.choice([1,1,1,2,2,3])
            bullet.append(Bullet(ran))


    for a in bullet:
        a.x += a.xv
        a.y += a.yv
        #collision and bullet break
        if (player.x >= a.x and player.x <= a.x + a.w) or (player.x + player.w >= a.x and player.x + player.w <= a.x + a.w):
            if (player.y >= a.y and player.y <= a.y +a.h) or (player.y + player.h >= a.y and player.y + player.h <= a.y + a.h):
                lives -= 1
                bullet.pop(bullet.index(a))
                break



        if lives <= 0:
            gameover = True




    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False


    GameWindow()
pygame.quit()
NYT_ SKY
  • 31
  • 3
  • In theory you could use separate threads for your mainloop and your timer... – TheHappyBee Apr 10 '22 at 00:01
  • I do not see where you implemented the sleep functionality. Can you clarify? – Christina Stebbins Apr 10 '22 at 00:07
  • Then, for example, use a separate threading.Thread(target=#counting for timer with sleep) then implement your mainloop as another thread. Due to their concurrent running the sleep will not interfere with the mainloop. – TheHappyBee Apr 10 '22 at 00:08
  • I implemented the sleep function up top "from timer import sleep". – NYT_ SKY Apr 10 '22 at 00:13
  • Ahhhh thank you for the idea im gonna try using a thread and see if it works! – NYT_ SKY Apr 10 '22 at 00:13
  • 1
    here's also a helpful tutorial on threading in case it's new: https://www.tutorialspoint.com/python/python_multithreading.htm – TheHappyBee Apr 10 '22 at 00:14
  • 2
    You imported sleep, but I don't see you using it in your code. The import just allows you to use the sleep as a function, but you are not implementing it – Christina Stebbins Apr 10 '22 at 00:17
  • Ahhh i imported sleep yes i didn't use it in my code because the code is use is bugging out my game, I just left the import sleep there just incase I need to use it again. – NYT_ SKY Apr 10 '22 at 01:18
  • 1
    You generally don't want to call sleep in an event driven program (mainloop). Either put sleeps in a separate thread or just examine the clock time, and if enough time has elapsed since the last update of your in-game timer, bump your in-game timer and refresh the display of it. – RufusVS Apr 10 '22 at 01:47

0 Answers0