hjhj.py

Created by wperez274

Created on September 09, 2023

7.34 KB


from kandinsky import *
from ion import *
from time import sleep
from random import randint, choice

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

# Player attributes
player_x = 50
player_y = 150
player_width = 15
player_height = 20
player_color = GREEN
player_jump = False
player_jump_height = 0
player_velocity = 2
player_max_jump_height = 40
player_outline_color = BLUE

# Critter monster attributes
critter_x = 200
critter_y = 170
critter_width = player_width * 2
critter_height = player_height * 2
critter_color = BLACK
critter_eye_color = RED
critter_eye_size = 2
critter_speed = 2  # Initial speed
critter_descending = False  # Flag to indicate if the critter is descending

# Small critter attributes
small_critter_x = 50
small_critter_y = 50
small_critter_size = 10
small_critter_color = BLACK
small_critter_speed = 1
small_critter_direction = choice([(1, 0), (-1, 0), (0, 1), (0, -1)])
small_critter_change_direction = randint(20, 100)
small_critter_approach_chance = 20  # Percentage chance to approach the player

# Energy attributes
player_energy = randint(25, 50)

# Gravity attributes
gravity = 2

# Platform attributes
platform_x = 0
platform_y = 180
platform_width = 320
platform_height = 10
platform_color = BLACK

# Screen boundaries
screen_width = 320
screen_height = 240

# Game variables
GAME_OVER = False

# Bullets attributes
bullet_color = BLACK
bullets = []

def create_bullet(x, y, width, height, color):
    bullets.append([x, y, width, height, color])

# Main game loop
while not GAME_OVER:
    # Clear the screen with the background color
    fill_rect(0, 0, screen_width, screen_height, (255,)*3)

    # Draw the platform on the screen
    fill_rect(platform_x, platform_y, platform_width, platform_height, platform_color)

    # Handle user input
    if keydown(KEY_LEFT) and player_x > 0:
        player_x -= player_velocity
    if keydown(KEY_RIGHT) and player_x + player_width < screen_width:
        player_x += player_velocity

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

    # Jumping
    if keydown(KEY_OK) and not player_jump and player_y + player_height == platform_y:
        player_jump = True
        player_jump_height = 0

    if player_jump:
        player_jump_height += gravity
        player_y -= gravity

        if player_jump_height >= player_max_jump_height:
            player_jump = False

    # Draw the player with eyes and blue outline
    fill_rect(player_x, player_y, player_width, player_height, player_color)
    fill_rect(player_x + 4, player_y + 4, 3, 3, WHITE)  # Left eye
    fill_rect(player_x + player_width - 7, player_y + 4, 3, 3, WHITE)  # Right eye
    fill_rect(player_x - 1, player_y - 1, player_width + 2, player_height + 2, player_outline_color)  # Outline

    # Move the critter monster
    if critter_descending:
        critter_y += critter_speed
        if critter_y + critter_height > screen_height:
            critter_descending = False
    else:
        critter_y -= critter_speed
        if critter_y < 0:
            critter_descending = True

    critter_x -= critter_speed

    # Reset critter monster position if it goes off-screen
    if critter_x + critter_width < 0:
        critter_x = screen_width
        critter_y = randint(50, 150)  # Randomize critter monster's vertical position
        critter_speed = randint(1, 5)  # Randomize critter monster's speed

    # Draw the critter monster with red eyes
    fill_rect(critter_x, critter_y, critter_width, critter_height, critter_color)
    fill_rect(critter_x + critter_width // 4, critter_y + critter_height // 4, critter_eye_size, critter_eye_size, critter_eye_color)  # Left eye
    fill_rect(critter_x + critter_width // 2 + critter_width // 4, critter_y + critter_height // 4, critter_eye_size, critter_eye_size, critter_eye_color)  # Right eye

    # Randomly approach the player
    if randint(1, 100) <= small_critter_approach_chance:
        if small_critter_x < player_x:
            small_critter_x += small_critter_speed * randint(1, 3)  # Fast approach
        else:
            small_critter_x -= small_critter_speed * randint(1, 3)  # Fast approach

        if small_critter_y < player_y:
            small_critter_y += small_critter_speed * randint(1, 3)  # Fast approach
        else:
            small_critter_y -= small_critter_speed * randint(1, 3)  # Fast approach

    # Draw the small critter
    fill_rect(small_critter_x, small_critter_y, small_critter_size, small_critter_size, small_critter_color)

    # Check for collisions with screen boundaries
    if player_x < 0:
        player_x = 0
    elif player_x + player_width > screen_width:
        player_x = screen_width - player_width

    if player_y < 0:
        player_y = 0
    elif player_y + player_height > screen_height:
        player_y = screen_height - player_height

    # Check for collisions with the platform
    if player_y + player_height > platform_y and player_x + player_width > platform_x and player_x < platform_x + platform_width:
        player_y = platform_y - player_height
        player_jump = False
        player_jump_height = 0

    # Check for collisions with the critter monster
    if (player_x + player_width > critter_x and player_x < critter_x + critter_width and
            player_y + player_height > critter_y and player_y < critter_y + critter_height):
        player_color = RED  # Player turns red
        player_energy -= randint(1, 2)  # Lose energy
        if player_energy < 0:
            GAME_OVER = True  # Player energy is less than zero, game over

    # Check for collisions with the small critter
    if (player_x + player_width > small_critter_x and player_x < small_critter_x + small_critter_size and
            player_y + player_height > small_critter_y and player_y < small_critter_y + small_critter_size):
        player_energy -= randint(1, 2)  # Lose energy
        if player_energy < 0:
            GAME_OVER = True  # Player energy is less than zero, game over

    # Update the energy bar at the top of the screen
    fill_rect(5, 5, player_energy * 2, 10, GREEN)  # Green energy bar
    fill_rect(5, 5, 50 * 2, 10, BLACK)  # Clear the energy bar background
    draw_string("Energy:", 5, 20, BLACK, WHITE)
    draw_string(str(player_energy), 80, 20, BLACK, WHITE)

    # Random bullet
    if randint(1, 100) <= 1:  # 1% chance to create a bullet
        create_bullet(screen_width, randint(50, 200), 10, 2, BLACK)

    # Update bullets
    for bullet in bullets:
        bullet[0] -= 3  # Move the bullet to the left
        fill_rect(bullet[0], bullet[1], bullet[2], bullet[3], bullet[4])  # Draw the bullet

        # Check for collisions with the player
        if (player_x + player_width > bullet[0] and player_x < bullet[0] + bullet[2] and
                player_y + player_height > bullet[1] and player_y < bullet[1] + bullet[3]):
            player_energy -= 1  # Lose energy
            if player_energy < 0:
                GAME_OVER = True  # Player energy is less than zero, game over

    # Remove bullets that go off-screen
    bullets = [bullet for bullet in bullets if bullet[0] > -bullet[2]]

    # Update the screen
    sleep(0.02)

# Game over screen
fill_rect(0, 0, screen_width, screen_height, (255,)*3)
draw_string("GAME OVER", 100, 80, RED, WHITE)
draw_string("Energy:", 100, 110, BLACK, WHITE)
draw_string(str(player_energy), 180, 110, BLACK, WHITE)

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.