critter5.py

Created by wperez274

Created on September 24, 2023

3.81 KB


from ion import *
from kandinsky import *
from kandinsky import fill_rect as FILL
from random import *
from time import sleep

WIDTH = 320
HEIGHT = 240
PLATFORM_COLOR = color(0, 255, 0)
CRITT_COLOR = color(0, 0, 0)
EYE_COLOR = color(255, 0, 0)
BULLET_COLOR = color(0, 0, 255)

PLATFORM_WIDTH = 40
PLATFORM_HEIGHT = 10
PLATFORM1_X = 100
PLATFORM1_Y = 100
PLATFORM2_X = 150
PLATFORM2_Y = 40

ground_x=0
ground_y=200
ground_w=320
ground_h=30
ground_c=(0,100,0)


CRITT_WIDTH = 12
CRITT_HEIGHT = 16

critt1_x = randint(PLATFORM1_X, PLATFORM1_X + PLATFORM_WIDTH - CRITT_WIDTH)
critt1_y = PLATFORM1_Y - CRITT_HEIGHT
critt1_direction = 1

critt2_x = randint(PLATFORM2_X, PLATFORM2_X + PLATFORM_WIDTH - CRITT_WIDTH)
critt2_y = PLATFORM2_Y - CRITT_HEIGHT
critt2_direction = 1

# Adjusted player dimensions
player_x = 20
player_y = 200
player_width = 14
player_height = 22
player_velocity_y = 0
player_jumps = 3

bullets = []

while True:
    FILL(ground_x,ground_y,ground_w,ground_h,ground_c)
    FILL(0, 0, WIDTH, HEIGHT, color(255, 255, 255))

    FILL(PLATFORM1_X, PLATFORM1_Y, PLATFORM_WIDTH, PLATFORM_HEIGHT, PLATFORM_COLOR)
    FILL(PLATFORM2_X, PLATFORM2_Y, PLATFORM_WIDTH, PLATFORM_HEIGHT, PLATFORM_COLOR)

    FILL(critt1_x, critt1_y, CRITT_WIDTH, CRITT_HEIGHT, CRITT_COLOR)
    FILL(critt1_x + 2, critt1_y + 2, 4, 4, EYE_COLOR)
    FILL(critt1_x + 6, critt1_y + 2, 4, 4, EYE_COLOR)

    FILL(critt2_x, critt2_y, CRITT_WIDTH, CRITT_HEIGHT, CRITT_COLOR)
    FILL(critt2_x + 2, critt2_y + 2, 4, 4, EYE_COLOR)
    FILL(critt2_x + 6, critt2_y + 2, 4, 4, EYE_COLOR)

    # Adjusted player dimensions and position
    FILL(player_x, player_y, player_width, player_height, color(0, 0, 255))

    for bullet in bullets:
        bullet_x, bullet_y, bullet_direction = bullet
        FILL(bullet_x, bullet_y, 4, 4, BULLET_COLOR)

    critt1_x += critt1_direction * randint(1, 5)
    critt2_x += critt2_direction * randint(1, 5)

    if critt1_x < PLATFORM1_X or critt1_x + CRITT_WIDTH > PLATFORM1_X + PLATFORM_WIDTH:
        critt1_direction *= -1

    if critt2_x < PLATFORM2_X or critt2_x + CRITT_WIDTH > PLATFORM2_X + PLATFORM_WIDTH:
        critt2_direction *= -1

    # Prevent player from falling off the ground
    if player_y + player_height > ground_y - player_height:
        player_y = ground_y - player_height
        player_velocity_y = 0
        player_jumps = 3

    player_velocity_y += 1
    player_y += player_velocity_y

    if keydown(KEY_UP) and player_jumps > 0:
        player_velocity_y = -12
        player_jumps -= 1

    new_bullets = []
    for bullet in bullets:
        bullet_x, bullet_y, bullet_direction = bullet
        bullet_x += bullet_direction * 5
        if 0 <= bullet_x <= WIDTH:
            new_bullets.append((bullet_x, bullet_y, bullet_direction))
    bullets = new_bullets

    if keydown(KEY_OK):
        bullet_direction = 1
        bullet_x = player_x + player_width
        bullet_y = player_y + player_height // 2
        bullets.append((bullet_x, bullet_y, bullet_direction))

    if (player_x < critt1_x + CRITT_WIDTH and player_x + player_width > critt1_x and
        player_y < critt1_y + CRITT_HEIGHT and player_y + player_height > critt1_y):
        FILL(0, 0, WIDTH, HEIGHT, color(0, 0, 0))
        sleep(1)
        FILL(0, 0, WIDTH, HEIGHT, color(255, 255, 255))
        critt1_x = randint(PLATFORM1_X, PLATFORM1_X + PLATFORM_WIDTH - CRITT_WIDTH)
        critt1_y = PLATFORM1_Y - CRITT_HEIGHT

    if (player_x < critt2_x + CRITT_WIDTH and player_x + player_width > critt2_x and
        player_y < critt2_y + CRITT_HEIGHT and player_y + player_height > critt2_y):
        FILL(0, 0, WIDTH, HEIGHT, color(0, 0, 0))
        sleep(1)
        FILL(0, 0, WIDTH, HEIGHT, color(255, 255, 255))
        critt2_x = randint(PLATFORM2_X, PLATFORM2_X + PLATFORM_WIDTH - CRITT_WIDTH)
        critt2_y = PLATFORM2_Y - CRITT_HEIGHT

    sleep(0.05)

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.