0

I'm trying to make a trex game like the one in google chrome. I made a SPEEDEVENT so that whenever 1 second passes, the speed increase by 0.1. Before changing the speed, both the cactuses and the ground were moving at the same speed but once the speed changed, the cactuses started moving faster (the ground's speed also increased).. This also happens when I set the speed to increase by 0.000000001 so it's not about the value of the speed, it's about the fact that I changed the speed. Anyways here's the code, the parts that you might want to focus on the most are the move_cactus function that moves all cactuses and returns the moved list, and what's under the #Ground comment in the game loop (that's where I move the ground)

import pygame
from random import choice, randint

def manage_surface():
    global current_surface
    if leg == 'rear':
        if crouching:
            current_surface = 3
        if not crouching:
            current_surface = 1
    if leg == 'front':
        if crouching:
            current_surface = 4
        if not crouching:
            current_surface = 2

def change_leg():
    global leg
    if leg == 'rear':
        leg = 'front'
        return
    else: leg = 'rear'

def spawn_cactus():
    cactus_count = randint(1, 3)
    x_position = screen_width + 200
    big_cactus = False
    for count in range(cactus_count):
        new_cactus_surface = choice(cactus_surfaces)
        if big_cactus: new_cactus_surface = choice(cactus_surfaces[5:-1])
        new_cactus_rect = new_cactus_surface.get_rect(midbottom = (x_position, ground_level))
        new_cactus_rect.left = x_position
        new_cactus_mask = pygame.mask.from_surface(new_cactus_surface)
        cactus_list.append([new_cactus_surface, new_cactus_rect, new_cactus_mask])
        x_position += new_cactus_rect.width
        if new_cactus_surface in cactus_surfaces[0:5]: big_cactus = True

def draw_cactus():
    for cactus in cactus_list:
        screen.blit(cactus[0], cactus[1])

def move_cactus(cactuses):
    moved_cactus_list = []
    for cactus in cactuses:
        if cactus[1].right < 0:
            continue
        cactus[1].centerx -= speed
        moved_cactus_list.append(cactus)
    return moved_cactus_list

def check_collision():
    trex_x, trex_y = trex_rect.topleft
    for cactus in cactus_list:
        cactus_x, cactus_y = cactus[1].topleft
        offset = (cactus_x - trex_x, cactus_y - trex_y)
        if trex_mask.overlap(cactus[2], offset):
            return True

pygame.init()

fps = 60
clock = pygame.time.Clock()
screen_width = 1280
screen_height = 720
screen = pygame.display.set_mode([screen_width, screen_height])

#Game Variables
initial_speed = 7
speed = initial_speed
initial_gravity = 1
gravity = initial_gravity
game_active = False
SPEEDEVENT = pygame.USEREVENT + 2
pygame.time.set_timer(SPEEDEVENT, 1000)

#Ground
ground_surface = pygame.image.load('assets/ground.png').convert_alpha()
ground_width, ground_height = ground_surface.get_size()
ground_last_x_pos = 0
crouch = False

#Trex
trex_surfaces = []
trex_images = ['trex0.png', 'trex1.png', 'trex2.png', 'trex1-1.png', 'trex2-1.png', 'trex3.png']
for trex in trex_images:
    trex_surfaces.append(pygame.image.load(f'assets/{trex}').convert_alpha())
ground_level = 710
leg = 'rear'
jumping = False
jump_power = 22
crouching = False
manage_surface()
trex_rect = trex_surfaces[current_surface].get_rect(center = (150, ground_level))
trex_mask = pygame.mask.from_surface(trex_surfaces[current_surface])
TREXSTEP = pygame.USEREVENT
pygame.time.set_timer(TREXSTEP, 200)


#cactus
cactus_surfaces = []
cactus_images = ['bigcactus1.png', 'bigcactus2.png', 'bigcactus3.png', 'bigcactus4.png', 'bigcactus5.png', 'cactus1.png', 'cactus2.png', 'cactus3.png', 'cactus4.png', 'cactus5.png', 'cactus6.png']
for cactus in cactus_images:
    cactus_surfaces.append(pygame.image.load(f'assets/{cactus}').convert_alpha())
cactus_list = []
SPAWNPIKE = pygame.USEREVENT + 1
pygame.time.set_timer(SPAWNPIKE, 2000)


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and not crouching:
                if not game_active: game_active = True
                jumping = True
            if event.key == pygame.K_DOWN and not jumping:
                trex_rect.bottom += 17
                crouching = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                crouching = False
        if event.type == TREXSTEP and not jumping:
            change_leg()
        if event.type == SPAWNPIKE:
            spawn_cactus()
        if event.type == SPEEDEVENT and game_active and cactus_list:
            speed += 0.1

    if not game_active: continue

    #Background Color
    screen.fill((255, 255, 255))

    #Ground
    screen.blit(ground_surface, (ground_last_x_pos, 676))
    screen.blit(ground_surface, (ground_last_x_pos+ground_width, 676))
    if ground_last_x_pos < -ground_width:
        ground_last_x_pos = 0
    ground_last_x_pos -= speed

    
    #Cactus
    draw_cactus()
    cactus_list = move_cactus(cactus_list)
    
    

    #Trex
    manage_surface()
    trex_rect = trex_surfaces[current_surface].get_rect(center = trex_rect.center)
    trex_mask = pygame.mask.from_surface(trex_surfaces[current_surface])
    if jumping: trex_rect.bottom -= jump_power
    trex_rect.bottom += gravity
    gravity += initial_gravity
    if trex_rect.bottom > ground_level:
        trex_rect.bottom = ground_level
        gravity = initial_gravity
        jumping = False
    if check_collision():
        game_active = False
        current_surface = 5
        speed = initial_speed
    screen.blit(trex_surfaces[current_surface], trex_rect)



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

pygame.quit()
Dtomper
  • 79
  • 7
  • use `print()` to see values in variables. Maybe your ground always use the same speed. Or maybe you keep it as `integer` and then it is doesn't see so small changes - it rounds it to nearest integer. – furas Jan 28 '21 at 01:27
  • 1
    you keep background position in `ground_last_x_pos` which can be `float` value but you keep cactus position in `pygame.Rect` which can keep only `integer` values – furas Jan 28 '21 at 01:32
  • @furas Oh my god, you're amazing. You got that right <3 Thank you – Dtomper Jan 29 '21 at 01:50

0 Answers0