sine4.py

Created by wperez274

Created on September 20, 2023

4.61 KB


from math import *
from random import *
from random import randint as RAND
from kandinsky import *
from kandinsky import fill_rect as FILL
from kandinsky import draw_string as STR
from ion import *
from time import sleep

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255,) * 3
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)

S_WIDTH = 320
S_HEIGHT = 222

rect_w = 20
rect_h = 20
rect_c = RED
center_x = S_WIDTH // 2
center_y = S_HEIGHT // 2
radius = 50
angle = 0
angular_speed = 0.02

gx = 0
gy = 170
gw = S_WIDTH
gh = 6
gc = WHITE

lavax = 0
lavay = RAND(200, 218)
lavaw = 322
lavah = 30
lavac = RED

fireball_radius1 = 10
fireball_radius2 = 5
fireball_color = RED

fireball2_radius = 10
fireball2_angle = 0
fireball2_speed = 0.01
fireball2_x = S_WIDTH
fireball2_y = S_HEIGHT // 2

bullets = []

platform_width = 100
platform_height = 10
platform_x = 50
platform_y = gy + gh
platform_color = WHITE

px = 40
pw = 12
ph = 22
py = gy - ph + 1
pc = BLUE
pskin = WHITE
pe = 45
game = True
bg = (RAND(0, 80), RAND(0, 80), RAND(0, 80))
FILL(0, 0, S_WIDTH, S_HEIGHT, bg)
FILL(gx, gy, gw, gh, gc)

facing_left = False
facing_right = True

def shoot_bullet():
    if facing_left:
        bullet = {'x': px, 'y': py + ph // 2, 'direction': -1}
    elif facing_right:
        bullet = {'x': px + pw, 'y': py + ph // 2, 'direction': 1}
    bullets.append(bullet)

while game:
    sleep(0.001)

    FILL(15, 5, int(pe), 8, (255, 0, 0))
    x = center_x + int(radius * cos(angle))
    y = center_y + int(radius * sin(angle))

    FILL(px, py, pw, ph, pc)

    FILL(px, py, 1, ph, pskin)
    FILL(px + pw - 1, py, 1, ph, pskin)
    FILL(px, py, pw, 1, pskin)
    FILL(px, py + ph - 1, pw, 1, pskin)

    FILL(x - rect_w // 2, y - rect_h // 2, rect_w, rect_h, rect_c)
    FILL((x - rect_w // 2) + 3, (y - rect_h // 2) + 3, rect_w - 6, rect_h - 6, (RAND(50, 255), 0, 0))

    if px < x + rect_w // 2 and px + pw > x - rect_w // 2 and py < y + rect_h // 2 and py + ph > y - rect_h // 2:
        pe -= 10
        px = 40

    angle += angular_speed

    if keydown(KEY_LEFT):
        FILL(px + pw + 1, py, 1, ph, bg)
        px -= 1
        if keydown(KEY_TOOLBOX):
            FILL(px + pw, py, 2, ph, bg)
            FILL(15 + int(pe), 5, 1, 8, bg)
            px -= 2
            pe -= 0.8
        facing_left = True
        facing_right = False

    if keydown(KEY_RIGHT):
        FILL(px - 1, py, 1, ph, bg)
        px += 1
        if keydown(KEY_TOOLBOX):
            FILL(px - 2, py, 3, ph, bg)
            FILL(15 + int(pe), 5, 1, 8, bg)
            px += 2
            pe -= 0.8
        facing_left = False
        facing_right = True

    if px + pw < 0:
        px = S_WIDTH
    if px > S_WIDTH:
        px = -pw

    if keydown(KEY_UP) or keydown(KEY_OK) and pe > 0:
        FILL(15 + int(pe), 5, 1, 8, bg)
        FILL(px, py + ph, pw, 1, bg)
        py -= 1
        pe -= 0.4
    else:
        py += 1

    if not keydown(KEY_UP) and not keydown(KEY_OK):
        FILL(px, py - 1, pw, 1, bg)

    if py + ph - 1 > gy:
        py = gy - ph + 1

    if keydown(KEY_DOWN) and py + ph >= gy:
        ph = 10
        FILL(px, py, pw, ph, bg)
        sleep(0.01)
        py = gy - ph
    else:
        ph = 22

    pe += 0.05

    if pe > 55:
        pe = 55

    if pe < 0:
        pe = 0
        game = False

    FILL(lavax, lavay, lavaw, lavah, lavac)
    FILL(lavax + 2, lavay + 5, lavaw - 4, lavah, (RAND(150, 255), 0, 0))

    FILL(x - fireball_radius1, y - fireball_radius1, 2 * fireball_radius1, 2 * fireball_radius1, fireball_color)

    fireball2_x = center_x + int(radius * cos(fireball2_angle))
    fireball2_y = center_y + int(radius * sin(fireball2_angle))
    fireball2_angle += fireball2_speed

    if fireball2_x < 0:
        fireball2_x = S_WIDTH
        fireball2_y = RAND(0, S_HEIGHT)

    FILL(fireball2_x - fireball2_radius, fireball2_y - fireball2_radius, 2 * fireball2_radius, 2 * fireball2_radius, fireball_color)

    if keydown(KEY_BACKSPACE):
        shoot_bullet()

    for bullet in bullets:
        bullet['x'] += 5 * bullet['direction']
        FILL(bullet['x'], bullet['y'], 4, 2, YELLOW)

        if bullet['x'] > S_WIDTH or bullet['x'] < 0:
            bullets.remove(bullet)

    FILL(platform_x, platform_y, platform_width, platform_height, platform_color)

    # Generate random bullets from the right side
    if RAND(0, 100) < 2:
        bullet_x = RAND(320, 400)
        bullet_y = RAND(50, 190)
        bullet_direction = -1
        bullets.append({'x': bullet_x, 'y': bullet_y, 'direction': bullet_direction})

    STR("E", 2, 2, GREEN, bg)
    bg = BLACK
    FILL(0, 0, S_WIDTH, S_HEIGHT, bg)
    STR("GAME OVER", 100, 100, RED, BLACK)

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.