space_invaders.py

Created by alexandre-merle

Created on October 10, 2023

5.14 KB


from math import *
from random import randint as allea
from kandinsky import fill_rect as rect,draw_string as text
from ion import keydown as k,KEY_LEFT as left,KEY_UP as up,KEY_DOWN as down,KEY_RIGHT as right,KEY_OK as ok
from time import *


def a_plan():rect(0,0,320,222,(50,)*3)

colors=[(255,)*3,(100,)*3,(255,0,0),(255,255,0),(0,255,0),(0,255,255),(0,0,255),(255,0,255),(150,)*3,(200,)*3]


alien1=[0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,1,0,0]
alien2=[0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,0,1,1,0,1,1,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1]
alien3=[0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,0,1,1,1,0,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1,1,0,1,1,0,0,0]
choice=[alien1,alien2,alien3]

def d_alien(list,x,y,t,color):
  rx,ry=x,y
  for i in list:
    if i!=0:rect(rx,ry,t,t,color)
    rx+=t
    if rx-x>11*t:ry+=t;rx=x

class aliens:
  def __init__(self):
    self.posx=(allea(0,7)*40)
    self.posy=-50
    self.size=3
    self.color=colors[allea(2,7)]
    self.type=choice[allea(0,2)]

  def draw(self):d_alien(self.type,self.posx,self.posy,self.size,self.color)

  def alien_win(self):
    if self.posy+(8*self.size)>=222:return True
    else:return False


navette=[
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,8,8,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,8,8,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,8,8,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,8,8,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,1,8,8,8,8,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,8,8,8,8,8,8,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,8,9,8,8,9,8,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,1,8,9,8,8,9,8,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,7,7,0,0,0,1,8,9,8,8,9,8,1,0,0,0,7,7,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,7,7,0,0,0,1,8,9,8,8,9,8,1,0,0,0,7,7,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,7,7,0,0,0,1,8,9,8,8,9,8,1,0,0,0,7,7,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,7,7,0,0,1,1,8,9,8,8,9,8,1,1,0,0,7,7,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,8,9,0,1,1,8,9,8,8,8,8,9,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0,0,0,1,8,8,1,0,0,0,0,0,0,0,0,0,1,1,9,8,1,
1,9,1,1,0,0,0,0,0,0,0,0,0,0,1,8,8,1,0,0,0,0,0,0,0,0,0,0,1,9,1,1,
0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0]

def d_navette(x,y,t,color):
  rx,ry=x,y
  for i in navette:
    if i!=0:rect(rx,ry,t,t,colors[i])
    rx+=t
    if rx-x>31*t:ry+=t;rx=x

def d_bullet(x,y):
  if y>222:return
  rect(x+1,y,2,-4,(255,180,0))
  rect(x+7,y-6,2,-4,(255,180,0))
  rect(x+31,y,2,-4,(255,180,0))
  rect(x+25,y-6,2,-4,(255,180,0))

class bullets:
  def __init__(self,x,y):
    self.posx=x
    self.posy=y
    self.deplace=True

  def draw(self):
    d_bullet(self.posx,self.posy)


_x=144;_y=180;_a=-1;_t=monotonic()
alien=[aliens()];bullet=[]
score=0;stage=1
run=True
while run:
  a_plan()
  text("Score:"+str(score),0,0,(255,)*3,(50,)*3)
  text("Stage:"+str(stage),250,0,(255,)*3,(50,)*3)
  if monotonic()-_t>2 and len(alien)<5:alien+=[aliens()];_t=monotonic()

  for i in alien:
    i.posy+=stage+1;i.draw()
    if i.alien_win():run=False;text("Game Over!",110,100,(255,)*3,(50,)*3)

  for i in bullet:
    if i.deplace:i.posy-=8;i.draw()
    if i.posy<0:i.deplace=False
    for j in alien:
      if i.posx-6<j.posx<i.posx+38 and i.posy-30<j.posy<i.posy and i.deplace==True:j.__init__();score+=1;i.deplace=False
  
  d_navette(_x,_y,1,(150,)*3)
  if k(left) and _x>10: _x-=20
  if k(right)and _x<280:_x+=20
  if k(up)   and _y>10: _y-=20
  if k(down) and _y<180:_y+=20  
  if k(ok)   and _a>2:
    _a=0
    for i in bullet:
      if i.deplace==False and _a==0:
        i.deplace=True
        i.__init__(_x,_y)
        _a+=1
    if _a==0:bullet+=[bullets(_x,_y)]
    _a=0

  if score>=10:
    score=0
    stage+=1
    text("Stage superieur.",80,100,(255,)*3,(50,)*3)
    sleep(1)
  _a+=1
  sleep(0.05)
sleep(2)

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.