shoot_it.py

Created by wperez274

Created on September 22, 2023

2.05 KB


from kandinsky import *
from ion import keydown, KEY_OK, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_BACKSPACE
from time import sleep
from random import randint, choice

missiles = []
bullets = []
player_x, player_y = 20, 100
enemy_x, enemy_y = 300, 100
enemy_color = (0, 0, 0)
hit_time = 0

def shoot_missile(x, y, target_x, target_y):
    if len(missiles) < 5:
        dx = target_x - x
        dy = target_y - y
        missiles.append([x, y, dx, dy])

def shoot_bullet(x, y):
    bullets.append([x + 10, y + 6])

def explosion(x, y):
    for i in range(-5, 6):
        for j in range(-5, 6):
            color = (randint(0, 255), randint(0, 255), randint(0, 255))
            fill_rect(x + i, y + j, 1, 1, color)

while 1:
    current_time = time()
    if current_time - hit_time < 1:
        enemy_color = (0, 255, 0)
    else:
        enemy_color = (0, 0, 0)

    FILL(player_x, player_y, 10, 13, (0, 0, 255))
    FILL(enemy_x, enemy_y, 20, 20, enemy_color)
    FILL(enemy_x + 5, enemy_y + 5, 5, 5, (0, 0, 0))
    FILL(enemy_x + 10, enemy_y + 5, 5, 5, (0, 0, 0))

    if keydown(KEY_LEFT):
        player_x -= 2
    if keydown(KEY_RIGHT):
        player_x += 2
    if keydown(KEY_UP):
        player_y -= 2
    if keydown(KEY_DOWN):
        player_y += 2

    if keydown(KEY_OK):
        shoot_missile(player_x, player_y, enemy_x, enemy_y)

    if keydown(KEY_BACKSPACE):
        shoot_bullet(player_x, player_y)

    for missile in missiles:
        missile_x, missile_y = int(missile[0]), int(missile[1])
        FILL(missile_x, missile_y, 3, 3, (0, 255, 0))
        missile[0] += missile[2] * 0.1
        missile[1] += missile[3] * 0.1
        if get_pixel(missile_x, missile_y) == enemy_color:
            hit_time = time()
            missiles.remove(missile)

    for bullet in bullets:
        bullet_x, bullet_y = bullet
        FILL(bullet_x, bullet_y, 3, 3, (0, 0, 0))
        bullet[0] += 5
        if get_pixel(bullet_x, bullet_y) == enemy_color:
            hit_time = time()
            bullets.remove(bullet)

    sleep(0.01)
    FILL(0, 0, 322, 222, (25,) * 3)

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.