0

I am writing a game using python's Pygame module. In this game you need to jump on platforms while avoiding falling out of the screen. I borrowed the code from a previous game so some of the variables don't match up. Nevertheless, I want to make it so that if you are on top of a platform, you can stand on it, but I cant get the character to stand on the platforms, it just falls.

Here is my code:

    # Import modules and initialize
import pygame as p
import sys
import random
p.init()
# Define variables
player_movement = 0
GRAVITY = 0.25
asteroid_height = [650, 625]
score = 0
high_score = 0
game_font = p.font.Font('fonts/PressStart2P-Regular.ttf',20)
game_active = False
FPS = p.time.Clock()
# Make some functions
def create_asteroid():
    random_asteroid_pos = random.choice(asteroid_height)
    new_asteroid = asteroid_surface.get_rect(midtop = (650, random_asteroid_pos))
    return new_asteroid
def move(asteroids):
    for asteroid in asteroids:
        asteroid.centerx -= 5
    return asteroids
def draw(asteroids):
    for asteroid in asteroids:
        sc.blit(asteroid_surface, asteroid)
def collision(asteroids):
    for asteroid in asteroids:
        if player_rect.colliderect(asteroid):
            GRAVITY = 0
            player_movement += 1
        else:
            GRAVITY = 0.25
            player_movement = 0
        if player_rect.bottom >= 600:
            GRAVITY = -1
        else:
            GRAVITY = 0.25
    return True
def display_score(game_state):
    if game_state == 'in_game':
        score_surface = game_font.render(f'Score: {int(score)}',True, (255,255,255))
        score_rect = score_surface.get_rect(center = (110, 30))
        sc.blit(score_surface, score_rect)
    elif game_state == 'game_over':
        high_score_surface = game_font.render(f'High Score: {int(high_score)}',True, (255,255,255))
        high_score_rect = high_score_surface.get_rect(center = (320, 370))
        sc.blit(high_score_surface, high_score_rect)
def update_score(score, high_score):
    if score > high_score:
        high_score = score
    return high_score
# Make a screen
sc = p.display.set_mode((700, 700))
p.display.set_caption('The Adventures of Cubeman')
# Make a backround
bg = p.image.load('images/backround.png')
bg = p.transform.scale2x(bg)
# Put in main music
# Play main music
# Import character
ship = p.image.load('images/cube.png').convert()
# Import asteroid
asteroid_surface = p.image.load('images/platform.png').convert()
# Asteroid logic
asteroid_list = []
SPAWN = p.USEREVENT
p.time.set_timer(SPAWN, 1500)
# Hitbox variables
player_rect = ship.get_rect(center = (100, 350))
# Sound FX
jump_sound = p.mixer.Sound('sound/jump.wav')
hit_sound = p.mixer.Sound('sound/impact.wav')
# Begin code logic
while True:
    # Make the game exit cleanly
    for event in p.event.get():
        if event.type == p.QUIT:
            p.quit()
            sys.exit()
        if event.type == p.KEYDOWN:
            if event.key == p.K_SPACE and game_active:
                player_movement = 0
                player_movement -= 6
                jump_sound.play()
            if event.type == p.KEYDOWN:
                if event.key == p.K_SPACE and game_active == False:
                    game_active = True
                    asteroid_list.clear()
                    player_movement = 0
                    score = 0
                    player_rect.center = (100, 350)
        if event.type == SPAWN:
            asteroid_list.append(create_asteroid())
        # INDENTATION

    #<--|

    sc.blit(bg, (0, 0))
    if game_active:
        player_movement += GRAVITY
        player_rect.centery += player_movement
        # Draw asteroids and player
        sc.blit(ship,player_rect)
        asteroid_list = move(asteroid_list)
        draw(asteroid_list)
        game_active = collision(asteroid_list)
        # Score system
        score += 0.01
        display_score('in_game')
        # Game info
        info_surface = game_font.render('SPACE to move!',True, (255,255,255))
        info_rect = info_surface.get_rect(center = (550, 30))
        sc.blit(info_surface, info_rect)
    else:
        high_score = update_score(score, high_score)
        display_score('game_over')
        # Make the main menu/game over screen
        game_over_surface = game_font.render('The Adventures of Cubeman',True, (255,255,255))
        game_over_rect = game_over_surface.get_rect(center = (320, 330))
        sc.blit(game_over_surface, game_over_rect)
        game_over_surface1 = game_font.render('SPACE to play!',True, (255,255,255))
        game_over_rect1 = game_over_surface1.get_rect(center = (320, 350))
        sc.blit(game_over_surface1, game_over_rect1)
    FPS.tick(120)
    p.display.update()

Any help is appreciated!

Nathan
  • 27
  • 6
  • so the concept I can give You is that You get the position to which the object can fall and then when it reaches that spot You make it stop falling. So if the next cube's top position ir 650 You set it so that when player reaches that pos it stops falling – Matiiss Mar 30 '21 at 11:06
  • Can you please post how I would do that. I didn't understand what you are trying to say. Thanks :)! – Nathan Mar 31 '21 at 20:55
  • I actually cannot since I am a bit rusty in Pygame and it is pretty hard for me without being able to properly test code since I do not have the media. Only suggestion is take a look at these tutorials (I have looked at them before and they were great and could possibly answer other questions too): https://www.youtube.com/watch?v=i6xMBig-pP4&list=PLzMcBGfZo4-lp3jAExUCewBfMx3UZFkh5&index=1 and obviously You do not need to watch all of them just the ones You think will be helpful – Matiiss Mar 31 '21 at 21:03
  • Thanks a lot for the videos and suggestions. Will take a look! – Nathan Apr 01 '21 at 16:43

0 Answers0