missiles.py

Created by wperez274

Created on September 22, 2023

2.95 KB


from kandinsky import *
from kandinsky import fill_rect as FILL
from ion import keydown, KEY_OK, KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_BACKSPACE
from time import sleep, time
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(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 -= 1
    if keydown(KEY_RIGHT):
        player_x += 1
    if keydown(KEY_UP):
        player_y -= 1
    if keydown(KEY_DOWN):
        player_y += 1

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

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

    missiles_to_remove = set()
    bullets_to_remove = set()

    for missile in missiles:
        FILL(int(missile[0]), int(missile[1]), 3, 3, (0, 255, 0))
        missile[0] += missile[2] * 0.1
        missile[1] += missile[3] * 0.1
        if get_pixel(int(missile[0]), int(missile[1])) == (0, 0, 0):
            hit_time = time()
            missiles_to_remove.add(tuple(missile))
        elif missile[0] > 320 or missile[1] > 240:
            missiles_to_remove.add(tuple(missile))

    for bullet in bullets:
        FILL(bullet[0], bullet[1], 3, 3, (0, 0, 0))
        bullet[0] += 5
        if bullet[0] > 320:
            bullets_to_remove.add(tuple(bullet))

        if get_pixel(bullet[0], bullet[1]) == (0, 0, 0):
            hit_time = time()
            bullets_to_remove.add(tuple(bullet))

    for missile in missiles_to_remove:
        missiles.remove(list(missile))
    for bullet in bullets_to_remove:
        bullets.remove(list(bullet))

    direction = choice(['attack', 'random'])
    speed = randint(1, 2)

    if direction == 'attack':
        if enemy_x > player_x:
            enemy_x -= speed
        if enemy_y > player_y:
            enemy_y -= speed
        if enemy_x < player_x:
            enemy_x += speed
        if enemy_y < player_y:
            enemy_y += speed
    else:
        move = randint(0, 4)
        if move == 0:
            enemy_x += 1
        elif move == 1:
            enemy_x -= 1
        elif move == 2:
            enemy_y += 1
        else:
            enemy_y -= 1

    sleep(0.01)

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.