0

Getting images from the file

start = pygame.image.load(os.path.join("Images/Dino", "DinoStart.png"))
dead = pygame.image.load(os.path.join("Images/Dino", "DinoDead.png"))
running = [pygame.image.load(os.path.join("Images/Dino", "DinoRun1.png")),
        pygame.image.load(os.path.join("Images/Dino", "DinoRun2.png"))]
jumping = pygame.image.load(os.path.join("Images/Dino", "DinoJump.png"))
ducking = [pygame.image.load(os.path.join("Images/Dino", "DinoDuck1.png")),
        pygame.image.load(os.path.join("Images/Dino", "DinoDuck2.png"))]

small_cactus = [pygame.image.load(os.path.join("Images/Cactus", "SmallCactus1.png")),
                pygame.image.load(os.path.join("Images/Cactus", "SmallCactus2.png")),
                pygame.image.load(os.path.join("Images/Cactus", "SmallCactus3.png"))]
large_cactus = [pygame.image.load(os.path.join("Images/Cactus", "LargeCactus1.png")),
                pygame.image.load(os.path.join("Images/Cactus", "LargeCactus2.png")),
                pygame.image.load(os.path.join("Images/Cactus", "LargeCactus3.png"))]

bird = [pygame.image.load(os.path.join("Images/Bird", "Bird1.png")),
        pygame.image.load(os.path.join("Images/Bird", "Bird2.png"))]

cloud = pygame.image.load(os.path.join("Images/Other", "Cloud.png"))

bg = pygame.image.load(os.path.join("Images/Other", "Track.png"))

Reading the all time high score

def get_saved_high_score():
    try:
        with open('high_score.json') as s:
            return json.load(s)
    except FileNotFoundError:
        return 0

Making the Pterodactyls and Cactie

class Obstacle:
    def __init__(self, image, type):
        self.image = image
        self.type = type
        self.rect = self.image[self.type].get_rect()
        self.rect.x = screen_width

    def update(self):
        self.rect.x -= game_speed
        if self.rect.x < - self.rect.width:
            obstacles.pop()

    def draw(self, screen):
        screen.blit(self.image[self.type], self.rect)

Making the small cactie

class SmallCactie(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 325

Making the big cactie

class LargeCactie(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 300

Making the pterodactyls

class Bird(Obstacle):
    def __init__(self, image):
        self.type = 0
        super().__init__(image, self.type)
        self.rect.y = 260
        self. index = 0

    def draw(self, screen):
        if self.index >= 9:
            self.index = 0
        screen.blit(self.image[self.index//5], self.rect)
        self.index += 1

Displaying the score

def display_score():
    global points

    text = font.render("Score: " + str(points), True, (0, 0, 0))
    textRect = text.get_rect()
    textRect.center = (1000, 40)
    screen.blit(text, textRect)

Displaying the high score

def display_high_score(hs):
    text = font.render("HI: " + str(hs), True, (0, 0, 0))
    textRect = text.get_rect()
    textRect.center = (900, 40)
    screen.blit(text, textRect)

def background():
    global x_pos_bg, y_pos_bg
    image_width = bg.get_width()
    screen.blit(bg, (x_pos_bg, y_pos_bg))
    screen.blit(bg, (image_width + x_pos_bg, y_pos_bg))
    if x_pos_bg <= - image_width:
        screen.blit(bg, (image_width + x_pos_bg, y_pos_bg))
        x_pos_bg = 0
    x_pos_bg -= game_speed

The main fuction to run the game

def main():
    global game_speed, x_pos_bg, y_pos_bg, points, obstacles
    run = True
    death_count = 0
    obstacles = []
    high_score = get_saved_high_score()

    while run:

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

        screen.fill((245, 130, 100))
        userInput = pygame.key.get_pressed()
        
        background()
        
        player.draw(screen)
        player.update(userInput)
    # Making the odstacles appear on the screen
        if len(obstacles) == 0:
            if random.randint(0, 2) == 0:
                obstacles.append(SmallCactie(small_cactus))
            elif random.randint(0, 2) == 1:
                obstacles.append(LargeCactie(large_cactus))
            elif random.randint(0, 2) == 2:
                obstacles.append(Bird(bird))
    # What happens when the dino hits an obstacle
        for obstacle in obstacles:
            obstacle.draw(screen)
            obstacle.update()
            if player.dino_rect.colliderect(obstacle.rect):
                with open('high_score.json', 'w') as s:
                    json.dump(high_score, s)
                pygame.time.delay(1000)
                death_count += 1
                points = 0    #Resets the score
                game_speed = 13
                menu(death_count)

        cloud.draw(screen)
        cloud.update()
    # Incresing the points and the speed of the game
        points += 1
        if points % 100 == 0:
            game_speed += 1
    # Displaying the score
    display_score()
    
    # Checking to see if there is a new high score
    if points > high_score:
        high_score = points

    # Displaying the high score
    display_high_score(high_score)

    clock.tick(30)
    pygame.display.update()

The different menu screens

def menu(death_count):
    global points, high_score
    run = True
    high_score = get_saved_high_score()
    while run:
        screen.fill((255, 130, 100))
        font = pygame.font.Font('freesansbold.ttf', 30)

        # The start screen
        if death_count == 0:
            text = font.render("Press any Key to Start", True, (0, 0, 0))
            screen.blit(start, (screen_width // 2 - 20, screen_hight // 2 - 140))
        # The restart/your high score screen
        else:
            text = font.render("Press any Key to Restart", True, (0, 0, 0))
            score = font.render("High Score: " + str(high_score), True, (0, 0, 0))
            scoreRect = score.get_rect()
            scoreRect.center = (screen_width // 2, screen_hight // 2 + 50)
            screen.blit(score, scoreRect)
            screen.blit(dead, (screen_width // 2 - 20, screen_hight // 2 - 140))
        textRect = text.get_rect()
        textRect.center = (screen_width // 2, screen_hight // 2)
        screen.blit(text, textRect)

        # Saving the high score when the game is closed
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                main()
            if event.type == pygame.QUIT:
                sys.exit()
            
menu(0)

#Code

# Making the Pterodactyls and Cactie
class Obstacle:
    def __init__(self, image, type):
        self.image = image
        self.type = type
        self.rect = self.image[self.type].get_rect()
        self.rect.x = screen_width

    def update(self):
        self.rect.x -= game_speed
        if self.rect.x < - self.rect.width:
            obstacles.pop()

    def draw(self, screen):
        screen.blit(self.image[self.type], self.rect)

# Making the small cactie
class SmallCactie(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 325

# Making the big cactie
class LargeCactie(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 300

# Making the pterodactyls
class Bird(Obstacle):
    def __init__(self, image):
        self.type = 0
        super().__init__(image, self.type)
        self.rect.y = 260
        self. index = 0

    def draw(self, screen):
        if self.index >= 9:
            self.index = 0
        screen.blit(self.image[self.index//5], self.rect)
        self.index += 1


# The main fuction to run the game
def main():
    global game_speed, x_pos_bg, y_pos_bg, points, obstacles
    run = True
    death_count = 0
    obstacles = []
    high_score = get_saved_high_score()

    while run:

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

        screen.fill((245, 130, 100))
        userInput = pygame.key.get_pressed()
        
        background()
        
        player.draw(screen)
        player.update(userInput)

        # Making the odstacles appear on the screen
        if len(obstacles) == 0:
            if random.randint(0, 2) == 0:
                obstacles.append(SmallCactie(small_cactus))
            elif random.randint(0, 2) == 1:
                obstacles.append(LargeCactie(large_cactus))
            elif random.randint(0, 2) == 2:
                obstacles.append(Bird(bird))
Nora
  • 1
  • 1
  • 3
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community May 27 '22 at 04:46
  • Your obstacles all spawn on the right, You just need to decide when to create new obstacles. You can use a [timer](https://www.pygame.org/docs/ref/time.html#pygame.time.set_timer) to generate custom events that you handle to create new obstacles. – import random May 28 '22 at 06:39

0 Answers0