9/20/24 Blog Post

Added LOTS of stuff (more collision, lasers, rockets, pause menu, etc…)

ADDED LASERS ON THE SCREEN

def gen_laser:

laser_type = random.randint(0, 1)
    offset = random.randint(10,300) # how far off the screen
    match laser_type:
        case 0:
            laser_width = random.randint(100, 300) # horiz. laser
            laser_y = random.randint(100, HEIGHT - 100) 
            new_laser = [[WIDTH + offset, laser_y], [WIDTH + offset + laser_width, laser_y]]
        
        case 1:
            laser_height = random.randint(100, 300) # vert. laser
            laser_y = random.randint(100, HEIGHT - 400)
            new_laser = [[WIDTH + offset, laser_y], [WIDTH + offset + laser_y, laser_height]]
    
    return new_laser

Sets a random laser type (Horizontal is “0”, vertical is “1”) then sets the offset for how far it should be on the screen.

It then sees if the line is 0 or 1, then gives it a random height and offset and draws it on the screen (which is in the draw_screen function).

def draw_screen code for lasers:

# Using x values to stop lasers from moving if paused
lase[0][0] -= game_speed
lase[1][0] -= game_speed

    #drawing the laser lines on the screen

lase_line = py.draw.line(screen, "yellow", (lase[0][0], lase[0][1]), (lase[1][0], lase[1][1]), 10) #puts laser line on the screen
    py.draw.circle(screen, "yellow", (lase[0][0], lase[0][1]), 12) # top laser ball
    py.draw.circle(screen, "yellow", (lase[1][0], lase[1][1]), 12) # botton laser ball

Added an if statement in main loop. Will generate a laser if there isn’t one on already on the screen (also added laser variables to draw_screen()).

main loop:

if new_laser:
        laser = gen_laser()
        new_laser = False
    lines, top_plat, bottom_plat, laser, laser_line = draw_screen(lines, laser)

 if laser[0][0] <0 and laser[1][0] <0:
        new_laser = True

RESULT:

(Also added collision but I forgot to record it ;-;)

check_colliding(for the lasers):

 if laser_line.colliderect(player):
        rstrt = True

ADDED A RESTART FUNCTION WHEN COLLIDING WITH AN OBJECT

Made a restart global variable that is false on default.

restart_cmd = False

Made a separate restart local variable in check_colliding() which is also false by default.

check_colliding:

rstrt = False

Returns the restart variable when check_colliding() is finished.

check_colliding:

return coll, rstrt

Added restart_cmd with check_colliding in main loop.

main loop:

  colliding, restart_cmd = check_colliding()

Created an if statement that resets everything if the player hits an object.

main loop:

if restart_cmd: #literally restarts everything
        distance = 0
        rocket_active = False
        rocket_counter = 0
        pause = False
        player_y = init_y
        y_vel = 0
        restart_cmd = 0
        new_laser =True

RESULT:

(Also added distance and high score on the screen and made some simple code for it)

draw_screen:

 screen.blit(font.render(f"Distance: {int(distance)} m", True, "white"), (10, 10)) #distance text
    screen.blit(font.render(f"High Score: {int(high_score)} m", True, "white"), (10, 70))#high score text

main loop:

if distance > high_score:

    high_score = int(distance)

Only distance will reset when hitting an object. Not high score.

ADDED ROCKETS ON THE SCREEN

Author: Zende_

From PA. EHS 2025. Does computer programming and such. That's really it.

Leave a Reply

Your email address will not be published. Required fields are marked *