aaaa.py

Created by wperez274

Created on September 09, 2023

4.12 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))

# 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)

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

    # Handle user input
    if keydown(KEY_LEFT):
        player_x -= 2
        fill_rect(player_x + player_width + 1, player_y, 2, player_height, WHITE)

    if keydown(KEY_RIGHT):
        player_x += 2
        fill_rect(player_x - 2, player_y, 2, player_height, WHITE)

    if keydown(KEY_BACKSPACE) and float_energy > 0:
        player_y -= 2
        fill_rect(230, 4, int(float_energy), 13, bg)
        float_energy -= 0.4
        fill_rect(player_x, player_y + player_height + 1, player_width, 1, WHITE)

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

    # Handle player life and 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

    # Handle player floating energy
    if get_pixel(player_x, player_y + player_height) != BLACK and \
       get_pixel(player_x + player_width, player_y + player_height) != BLACK:
        float_energy = 30

    # Adjust player position
    if player_y < 0:
        player_y = 0

    if player_x + player_width < -1:
        player_x = 322

    if player_x > 322:
        player_x = -1 - player_width

    # Scroll the map
    if not GAME_OVER:
        draw_string("E: [", 5, 5)
        fill_rect(35, 8, int(float_energy), 13, GREEN)

    if player_x > 222:
        player_x = 222
        MAPX -= 2

    if player_x < 50:
        player_x = 50
        MAPX += 2

    # Update the wall positions
    for i in range(len(wall_list_x)):
        wall_list_x[i][0] = MAPX + wall_list_x[i][0]

    # Draw walls
    for wall in wall_list_x:
        fill_rect(wall[0], wall[1], wall[2], wall[3], wall[4])

    # Update wall gaps
    for wall in wall_list_x:
        fill_rect(wall[0] + wall[2] + 1, wall[1], 3, wall[3], bg)
        fill_rect(wall[0], wall[1], wall[2], 3, BLACK)

    # Move the map
    MAPX -= 1

# End of the game
print("Traveled:", traveled)
sleep(0.2)
print("Times Almost Fell:", times_almost_fell)

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.