fff.py

Created by wperez274

Created on September 09, 2023

3.15 KB


from math import *
from random import *
from kandinsky import *
from ion import *
from time import sleep

# Constants
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)

# Game variables
GAME_OVER = False
float_energy = 30
times_almost_fell = 0
traveled = 0
score = 0
life = 3

# Player attributes
player_x = 115
player_y = 30
player_width = 14
player_height = 22
player_color = (randint(100, 255), randint(100, 255), randint(100, 255))
player_jump = False
player_jump_height = 0

# Gravity settings
gravity = 1.0

# Map attributes
MAPX = 0
MAPY = 0
MAPW = 5000
MAPH = 222

# Map boundary boxes
MAPBOX_X1 = [MAPX, MAPY, 5, MAPH, BLACK]
MAPBOX_X2 = [MAPX + MAPW - 5, MAPY, 5, MAPH, BLACK]
MAPBOX_Y1 = [MAPX, MAPY, MAPW, 5, BLACK]
MAPBOX_Y2 = [MAPX, MAPY + MAPH - 5, MAPW, 5, BLACK]

# Ropes
rope_x = MAPX + randint(200, 500)
rope_h = randint(50, 120)
rope_y = MAPY + MAPH - rope_h - 5
rope_w = randint(500, 1200)
rope_c = (randint(0, 255), randint(0, 255), randint(0, 255))

rope = [rope_x, rope_y, rope_w, rope_h, rope_c]

# Walls
wall_list_x = []

for _ in range(3):
    r_x = MAPX + randint(1000, 3000)
    r_h = randint(50, 160)
    r_y = MAPY + MAPH - r_h - 5
    r_w = randint(180, 1200)
    r_c = (randint(0, 255), randint(0, 255), randint(0, 255))
    wall_list_x.append([r_x, r_y, r_w, r_h, r_c])

# Background color
bg = WHITE

# Main game loop
while not GAME_OVER:
    # Clear the screen
    fill_rect(0, 0, 322, 222, bg)

    # Draw map boundaries
    fill_rect(*MAPBOX_X1)
    fill_rect(*MAPBOX_X2)
    fill_rect(*MAPBOX_Y1)
    fill_rect(*MAPBOX_Y2)

    # Apply gravity
    if not player_jump and player_y + player_height < MAPH:
        player_y += gravity

    # Draw the player
    fill_rect(int(player_x), int(player_y), player_width, player_height, player_color)
    fill_rect(int(player_x + 3), int(player_y + 3), 4, 5, BLACK)
    fill_rect(int(player_x + 9), int(player_y + 3), 4, 5, BLACK)

    # Handle user input
    if keydown(KEY_LEFT):
        player_x -= 2

    if keydown(KEY_RIGHT):
        player_x += 2

    # Jumping
    if keydown(KEY_OK) and not player_jump:
        player_jump = True

    if player_jump:
        if player_jump_height < 40:
            player_y -= 2
            player_jump_height += 2
        else:
            player_jump = False
            player_jump_height = 0

    # Check for collisions with obstacles
    if get_pixel(int(player_x), int(player_y + player_height + 2)) == RED and \
       get_pixel(int(player_x + player_width), int(player_y + player_height - 1)) == RED:
        sleep(0.25)
        fill_rect(int(player_x), int(player_y), player_width, player_height, WHITE)
        player_x = 15
        player_y = 30
        life -= 1

    # Handle game over
    if life < 1:
        sleep(0.4)
        fill_rect(0, 0, 322, 222, BLACK)
        sleep(1)
        draw_string("GAME OVER", 100, 80, RED, BLACK)
        sleep(1)
        draw_string("Score:" + str(score), 100, 130, WHITE, BLACK)
        sleep(1.5)
        draw_string("PRESS [OK]", 120, 180, BLACK, GREEN)
        GAME_OVER = True

    # Update the screen
    sleep(0.03)  # Adjust this value to control the game speed

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.