0

I'm currently working on a pygame game where the dinosaur goes to the other side of the screen like in geometry dash. I'm not using classes, I'm a beginner at pygame and this is my first project. I'm not using classes. Heres the code ->

import pygame
import random
from sys import exit
import time

pygame.init()
screen = pygame.display.set_mode((800, 700))
pygame.display.set_caption("Don't Get Sliced!")
clock = pygame.time.Clock()
test_font = pygame.font.Font('Pixeltype.ttf')
game_active = False
start_time = 0

#play again screen
dinosaur_image = pygame.image.load('Dinosaur/dinosaur.png')
dinosaur_image_enlarged = pygame.transform.scale(dinosaur_image, (245,301))
dinosaur_image_rect = dinosaur_image_enlarged.get_rect(center = (400,350))

text_1 = test_font.render("Don't Get Sliced!", False, 'Black')
text_1_surf = pygame.transform.scale_by(text_1, (9,9))
text_1_rect = text_1_surf.get_rect(center = (400,100))
text_2 = test_font.render('Press Any Key To Start', False, 'Black')
text_2_surf = pygame.transform.scale_by(text_2, (6,6))
text_2_rect = text_2_surf.get_rect(center = (400,600))

score_present = 0


butcher_knife_rect_speed_inc = 2
knife_rect_speed_inc = 2

#    score = test_font.render('Score: ', False, 'Black')
#   score_surf = pygame.transform.scale_by(score, (5,5))
#   score_rect = score_surf.get_rect(center = (400,50))

background = pygame.image.load('metalbackground2.png')
background_surf = pygame.transform.scale(background, (1200,800))

side_wall_1 = pygame.image.load('metalwall2.png')
side_wall_1_surf = pygame.transform.scale(side_wall_1, (120,700))
side_wall_2 = pygame.image.load('metalwall2.png')
side_wall_2_flipped = pygame.transform.flip(side_wall_2, True, False)
side_wall_2_surf = pygame.transform.scale(side_wall_2_flipped, (120,700))

butcher_knife = pygame.image.load('Weapons/butcherknife.png')
butcher_knife_surf = pygame.transform.scale(butcher_knife, (228,80))
butcher_knife_rect = butcher_knife_surf.get_rect(topleft=(40,170))
butcher_knife_y_pos = 570

knife = pygame.image.load('Weapons/knife.png').convert_alpha()
knife_boy = pygame.transform.scale(knife, (220,40)).convert_alpha()
knife_surf = pygame.transform.flip(knife_boy, True, False)
knife_rect = knife_surf.get_rect(topleft=(520,600))
knife_y_pos = 570

dinosaur = pygame.image.load('Dinosaur/dinosaur.png')
dinosaur_enlarged = pygame.transform.scale(dinosaur, (87.5,107.5))
dinosaur_rotation = 270
dinosaur_flipped = False
dinosaur_rotated = pygame.transform.rotate(dinosaur_enlarged, dinosaur_rotation)
dinosaur_rect = dinosaur_rotated.get_rect(topleft=(500,-400))
dinosaur_x = 120
dinosaur_y = 300
dinosaur_direction = 1
dinosaur_position = "left"


#energy cube
energycube = pygame.image.load('energycube/energycube.png')
energycube_enlarged = pygame.transform.scale(energycube,(64,64))
energycube_rect = energycube_enlarged.get_rect(topleft = (120,350))




energy_y = 500



while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if game_active:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if dinosaur_position == "left":
                        dinosaur_x = 570
                        dinosaur_position = "right"
                    else:
                        dinosaur_x = 120
                        dinosaur_position = "left"
                    dinosaur_rotation = 270
                    dinosaur_flipped = not dinosaur_flipped
        else: 
            if event.type == pygame.KEYDOWN:
                game_active = True
                butcher_knife_rect.top = 170
                knife_rect.top = 600
                start_time = int(pygame.time.get_ticks() / 1500)


    if game_active:
        screen.blit(background, (-400,-50))
        screen.blit(side_wall_1_surf, (0,0))
        screen.blit(side_wall_2_surf, (680,0))

        butcher_knife_rect.y -= butcher_knife_rect_speed_inc
        butcher_knife_rect_speed_inc +=0.0005
        if butcher_knife_rect.y <= 0:
            butcher_knife_rect.y = 700
        screen.blit(butcher_knife_surf, butcher_knife_rect)

        knife_rect.y -= knife_rect_speed_inc
        knife_rect_speed_inc +=0.0005
        if knife_rect.y <= 0:
            knife_rect.y = 700
        screen.blit(knife_surf, knife_rect)

        dinosaur_rect = pygame.transform.rotate(dinosaur_enlarged, dinosaur_rotation)
        dinosaur_image_flipped = pygame.transform.flip(dinosaur_rect, dinosaur_flipped, False)
        dinosaur_rect_2 = dinosaur_image_flipped.get_rect(topleft = (dinosaur_x, dinosaur_y))
        screen.blit(dinosaur_image_flipped, dinosaur_rect_2)

        current_time = int(pygame.time.get_ticks() / 1500) - start_time
        score_surf = test_font.render(f'{current_time}', False, (255,255,255))
        score_enlarged = pygame.transform.scale_by(score_surf, (5,5))
        score_rect = score_enlarged.get_rect(center =  (400,50))
        screen.blit(score_enlarged,score_rect)

        
        if current_time % 10 == 0 and current_time != 0:
            energycube_rect_2 = energycube_enlarged.get_rect(topleft = (120,350))
            screen.blit(energycube_enlarged,energycube_rect_2)
            energycuberandom = random.randint(0,5)
            energycubelist = [1,1,2,2,3,4]
            energycubevalue = energycubelist[energycuberandom]
            energycollide = pygame.Rect.colliderect(dinosaur_rect_2, energycube_rect_2)
            if energycollide:
                energycubevalue = energycubelist[energycuberandom]
                if energycubevalue == 1:
                    print('1')
                elif energycubevalue == 2:
                    print('2')
                elif energycubevalue == 3:
                    print('3')
                elif energycubevalue == 4:
                    print('4')
                else:
                    print('error')
                

        #screen.blit(score_surf, score_rect)
        current_time = int(pygame.time.get_ticks() / 1500) - start_time
        score_surf = test_font.render(f'{current_time}', False, (255,255,255))
        score_enlarged = pygame.transform.scale_by(score_surf, (5,5))
        score_rect = score_enlarged.get_rect(center =  (400,50))
        screen.blit(score_enlarged,score_rect)

        #energy cube in loop

        #collision
        collidewithknife = pygame.Rect.colliderect(dinosaur_rect_2, butcher_knife_rect) or pygame.Rect.colliderect(dinosaur_rect_2, knife_rect)
        if collidewithknife:
            
            score_present = current_time
            game_active = False
    else:
        screen.blit(background,(-400,-50))
        screen.blit(dinosaur_image_enlarged,dinosaur_image_rect)
        screen.blit(text_1_surf, text_1_rect)
        screen.blit(text_2_surf, text_2_rect)
        score_present_surf = test_font.render(f"Score: {score_present}", False, "Black")
        score_present_s = pygame.transform.scale_by(score_present_surf, (5,5))
        score_present_rect = score_present_s.get_rect(center = (400,150))
        screen.blit(score_present_s, score_present_rect)
        butcher_knife_rect_speed_inc = 2
        knife_rect_speed_inc = 2
        energy_y = 500
        
    
    #mouse_pos = pygame.mouse.get_pos()
    #print(mouse_pos)
    pygame.display.update()
    clock.tick(60)  # limits FPS to

The problem here is that in a part of the code, theres an if statement stating if the score modulus by 10 is equal to zero, then a new rectangle is made (energy cube rect 2) and it is being screen.blit ed onto the screen. Then I use a list and random to get a random number from a list. Then i check if the main character( the dinosaur) collides with the energy cube. Then I check what the value is taken from the list. I will later change whats ever under the if condition from print to a power up that the energy cube gives. I'm using print to test. The problem is that due to t being in a while loop which is necessary for the game, It is being printed several times. I wanted it to be printed once or the value taken once so i can have only one power up activate. Heres the part im talking about -

        if current_time % 10 == 0 and current_time != 0:
            energycube_rect_2 = energycube_enlarged.get_rect(topleft = (120,350))
            screen.blit(energycube_enlarged,energycube_rect_2)
            energycuberandom = random.randint(0,5)
            energycubelist = [1,1,2,2,3,4]
            energycubevalue = energycubelist[energycuberandom]
            energycollide = pygame.Rect.colliderect(dinosaur_rect_2, energycube_rect_2)
            if energycollide:
                energycubevalue = energycubelist[energycuberandom]
                if energycubevalue == 1:
                    print('1')
                elif energycubevalue == 2:
                    print('2')
                elif energycubevalue == 3:
                    print('3')
                elif energycubevalue == 4:
                    print('4')
                else:
                    print('error')

fyi this is part of the game_active if condition and the while True loop

I tried many things such as making another list for the values but it didnt work.

1
  • 4
    Hi, welcome to stack overflow. I expect you will get more people interested in helping you if you can reduce your code to just the problematic part, so that people don't have to try to read through a couple hundred lines just to try to understand what you're doing Commented Nov 27, 2023 at 5:41

1 Answer 1

0

The problem can be reduced to the question: "How can I print a text only once when a collision is detected in pygame?". To print a text only once when a collision is detected in pygame, you can use a boolean flag to track whether the collision occurred in the previous frame or not. Only output the text if the collision is detected but was not detected in the previous frame:

collision_detected = False

while True:
    # [...]
    
    energycollide = pygame.Rect.colliderect(dinosaur_rect_2, energycube_rect_2)
    if energycollide:
        if not collision_detected
            collision_detected = True
            energycubevalue = energycubelist[energycuberandom]
            if energycubevalue == 1:
                print('1')
            # [...]

    else:
        collision_detected = False

    # [...]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.