kjkjkjk.py

Created by wperez274

Created on September 09, 2023

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

# Screen dimensions
WIDTH = 320
HEIGHT = 222

# Player attributes
player_x = WIDTH // 2
player_y = HEIGHT - 30
player_w = 20
player_h = 30
player_color = BLUE
player_jump = -4.0
player_velocity = 0.0
player_on_ground = False
player_energy = 100

# Platform attributes
platforms = [
    [50, HEIGHT - 20, 220, 10, BLACK],
    [150, HEIGHT - 70, 100, 10, BLACK],
    [50, HEIGHT - 120, 100, 10, BLACK],
]

# Critter attributes
critter_x = randint(50, WIDTH - 50)
critter_y = HEIGHT - 30
critter_w = 10
critter_h = 10
critter_color = BLACK
critter_speed = 1
critter_direction = choice([-1, 1])

# Game state
GAME_OVER = False

# Main game loop
while not GAME_OVER:
    # Clear the screen
    fill_rect(0, 0, WIDTH, HEIGHT, WHITE)

    # Draw platforms
    for platform in platforms:
        fill_rect(platform[0], platform[1], platform[2], platform[3], platform[4])

    # Draw player
    fill_rect(player_x, player_y, player_w, player_h, player_color)

    # Draw critter
    fill_rect(critter_x, critter_y, critter_w, critter_h, critter_color)

    # Update player position and energy
    if not player_on_ground:
        player_velocity += 0.2
    player_y += int(player_velocity)
    if player_y >= HEIGHT - player_h:
        player_y = HEIGHT - player_h
        player_velocity = 0.0
        player_on_ground = True
    player_energy -= 0.1

    # Update critter position
    critter_x += critter_speed * critter_direction
    if critter_x < 0:
        critter_x = 0
        critter_direction *= -1
    elif critter_x + critter_w > WIDTH:
        critter_x = WIDTH - critter_w
        critter_direction *= -1

    # Check for collisions
    if (player_x + player_w > critter_x and player_x < critter_x + critter_w and
            player_y + player_h > critter_y and player_y < critter_y + critter_h):
        player_energy -= 2.0
        critter_x = randint(50, WIDTH - 50)

    # Handle user input
    if keydown(KEY_RIGHT) and player_x < WIDTH - player_w:
        player_x += 2
    if keydown(KEY_LEFT) and player_x > 0:
        player_x -= 2
    if keydown(KEY_OK) and player_on_ground:
        player_velocity = player_jump
        player_on_ground = False

    # Draw player's energy
    fill_rect(5, 5, int(player_energy), 5, RED)

    # Check for game over
    if player_energy <= 0:
        GAME_OVER = True

    # Update the screen
    sleep(0.02)

# Game over screen
fill_rect(0, 0, WIDTH, HEIGHT, BLACK)
draw_string("GAME OVER", 120, 90, RED)
draw_string("Score: " + str(int(player_energy)), 120, 120, 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.