cosmic_raiders.py

Created by mathieu-croslacoste

Created on January 03, 2025

11.3 KB

Adaptation of the script “cosmic_raiders” by ccsio (https://my.numworks.com/python/ccsio/cosmic_raiders).

Needs “cosmic_raiders_lb” (https://my.numworks.com/python/mathieu-croslacoste/cosmic_raiders_lb) to run


from cosmic_raiders_lb import*
def enemy_textures(x,y,col,bg=white):fr(x+2,y,4,2,black);fr(x+28,y,4,2,black);fr(x,y+2,34,10,black);fr(x+2,y+12,30,2,black);fr(x+6,y+14,22,2,black);fr(x+10,y+16,14,4,black);fr(x+12,y+20,10,4,black);fr(x+14,y+24,6,4,black);fr(x+16,y+28,2,2,black);fr(x+10,y+2,4,2,bg);fr(x+20,y+2,4,2,bg);fr(x+2,y+2,2,10,col);fr(x+30,y+2,2,10,col);fr(x+4,y+10,2,2,col);fr(x+28,y+10,2,2,col);fr(x+6,y+12,4,2,col);fr(x+24,y+12,4,2,col);fr(x+10,y+14,2,2,col);fr(x+22,y+14,2,2,col);fr(x+12,y+16,2,2,col);fr(x+20,y+16,2,2,col);fr(x+14,y+18,2,6,col);fr(x+18,y+18,2,6,col);fr(x+16,y+24,2,4,col)
class Bullet:
 def __init__(self,x,y,dir,speed,color=red):self.pos_x=x;self.pos_y=y;self.dir=dir;self.speed=speed;self.size=(2,6);self.col=color;self.hidden=0;fr(self.pos_x,self.pos_y,self.size[0],self.size[1],self.col)
 def delete_bullet(self,index):fr(self.pos_x,self.pos_y,self.size[0],self.size[1],white);del bullet_list[index]
class PlayerBullet(Bullet):
 def move(self,xx):
  if self.pos_y<6:self.delete_bullet(xx);return
  elif gp(self.pos_x,self.pos_y-6)!=white and self.pos_y<70:
   for idx,i in reversed(list(enumerate(enemy_list))):
    if i=="dead":continue
    elif i.pos_x<=self.pos_x<=i.pos_x+34:
     self.delete_bullet(xx)
     if i.is_shielded:i._draw_shield();return
     return idx
  fr(self.pos_x,self.pos_y,self.size[0],self.size[1],white);self.pos_y+=self.dir*self.speed;fr(self.pos_x,self.pos_y,self.size[0],self.size[1],self.col)
class EnemyBullet(Bullet):
 def move(self,xx):
  if self.hidden:
   if self.pos_y>70:self.hidden=0
   else:self.pos_y+=self.dir*self.speed
   return
  elif self.pos_y>210:self.delete_bullet(xx);return
  elif gp(self.pos_x,self.pos_y+6)!=white and self.pos_y<150:
   for idx,i in reversed(list(enumerate(enemy_list))):
    if i=="dead":continue
    elif i.pos_x<=self.pos_x<=i.pos_x+34:fr(self.pos_x,self.pos_y,self.size[0],self.size[1],white);enemy_list[idx].load_texture();self.hidden=1;return
  elif gp(self.pos_x,self.pos_y+6)!=white:self.delete_bullet(xx);return"player_hit"
  fr(self.pos_x,self.pos_y,self.size[0],self.size[1],white);self.pos_y+=self.dir*self.speed;fr(self.pos_x,self.pos_y,self.size[0],self.size[1],self.col)
class SpaceShip:
 def __init__(self,x,y,hitpoints,class_dat,move_dat):self.pos_x=x;self.pos_y=y;self.size=34;self.hp=hitpoints
 def hit(self)->1 or 0:
  self.hp-=1
  if self.hp<=0:fr(self.pos_x,self.pos_y,34,34,white);return 1
  return 0
class Player(SpaceShip):
 def __init__(self,x,y,hitpoints,class_dat,move_dat):super().__init__(x,y,hitpoints,class_dat,move_dat);self.shot_delay=mc();self.speed=3;self.life_colors=[red,]*3;self.load_texture()
 def move_hor(self,dir):fr(self.pos_x,self.pos_y,self.size,self.size,white);self.pos_x+=dir*self.speed;self.load_texture()
 def load_texture(self):x,y=self.pos_x,self.pos_y;fr(x+16,y,2,2,black);fr(x+14,y+2,6,4,black);fr(x+12,y+6,10,6,black);fr(x+10,y+12,14,2,black);fr(x+6,y+14,22,2,black);fr(x+2,y+16,30,2,black);fr(x,y+18,34,6,black);fr(x+2,y+24,30,2,black);fr(x+10,y+26,14,2,black);fr(x+16,y+4,2,4,self.life_colors[2]);fr(x+14,y+8,2,6,self.life_colors[2]);fr(x+18,y+8,2,6,self.life_colors[2]);fr(x+12,y+14,2,2,self.life_colors[2]);fr(x+20,y+14,2,2,self.life_colors[2]);fr(x+2,y+18,2,6,self.life_colors[0]);fr(x+4,y+22,6,2,self.life_colors[0]);fr(x+10,y+24,4,2,self.life_colors[0]);fr(x+30,y+18,2,6,self.life_colors[1]);fr(x+24,y+22,6,2,self.life_colors[1]);fr(x+20,y+24,4,2,self.life_colors[1])
 def hit(self)->1 or 0:
  self.hp-=1
  if self.hp<=0:fr(self.pos_x,self.pos_y,34,34,white);return 1
  self.life_colors[3-self.hp]=gray ;self.load_texture();return 0
 def player_died(self):enemy_list.clear();action_list.clear();moving_list.clear();fr(0,0,320,240,white);ds("Game Over",115,100,black);global player;player=None
 def shoot(self):
  if mc()-self.shot_delay>.9:bullet_list.append(PlayerBullet(self.pos_x+16,self.pos_y-6,-1,5));self.shot_delay=mc()
class Enemy(SpaceShip):
 def __init__(self,x,y,hitpoints,class_dat,move_dat):
  super().__init__(x,y,hitpoints,class_dat,move_dat);self.is_shielded=0
  if move_dat is not None:self.axis=move_dat[0];self.total_distance=move_dat[1];self.px_update=move_dat[2];self.rounds=abs(self.total_distance/self.px_update);self.loop=move_dat[3];self.wait_time=move_dat[4];self.delay=0;self.dir=1
 def _move_x(self):
  if self.is_shielded:self._draw_shield(white)
  fr(self.pos_x,self.pos_y,self.size,self.size,white);self.pos_x+=int(self.dir*self.px_update);self.load_texture()
  if self.is_shielded:self._draw_shield()
 def _move_y(self,col=None):
  if self.is_shielded:self._draw_shield(white)
  fr(self.pos_x,self.pos_y,self.size,self.size,white);self.pos_y+=int(self.dir*self.px_update)
  if col is not None:self.load_texture(col)
  else:self.load_texture()
  if self.is_shielded:self._draw_shield()
 def move(self):
  if mc()-self.delay<self.wait_time:return
  elif self.axis=="x":self._move_x()
  else:self._move_y()
  self.rounds-=1
  if self.rounds==0:
   if not self.loop:return 1
   self.dir*=-1;self.rounds=abs(self.total_distance/self.px_update);self.delay=mc()
 def shoot(self):bullet_list.append(EnemyBullet(self.pos_x+16,self.pos_y+36,1,self.bullet_speed))
 def _draw_shield(self,col=blue):
  x,y,=self.pos_x,self.pos_y;img=[(16,32),(14,30),(18,30),(12,28),(20,28)]
  for i in img:fr(x+i[0],y+i[1],2,2,col)
  fr(x+10,y+24,2,4,col);fr(x+22,y+24,2,4,col);fr(x+8,y+20,2,4,col);fr(x+24,y+20,2,4,col);fr(x+6,y+18,2,2,col);fr(x+26,y+18,2,2,col);fr(x+2,y+16,4,2,col);fr(x+28,y+16,4,2,col);fr(x,y+14,2,2,col);fr(x+32,y+14,2,2,col)
class BasicShip(Enemy):
 def load_texture(self):enemy_textures(self.pos_x,self.pos_y,(160,100,0))
class TankShip(Enemy):
 def load_texture(self):enemy_textures(self.pos_x,self.pos_y,(104,4,160))
class ShooterShip(Enemy):
 def __init__(self,x,y,hitpoints,class_dat,move_dat):super().__init__(x,y,hitpoints,class_dat,move_dat);self.shooting_type=class_dat[0];self.type_param=class_dat[1];self.bullet_speed=class_dat[2];self.shot_delay=6
 def load_texture(self):enemy_textures(self.pos_x,self.pos_y,red)
 def action(self,idx):
  if self.shooting_type=="f"and mc()-self.shot_delay>self.type_param:self.shoot();self.shot_delay=mc()
  elif self.shooting_type=="r"and randrange(self.type_param)==1:self.shoot()
class RammingShip(Enemy):
 def __init__(self,x,y,hitpoints,class_dat, move_dat):super().__init__(x,y,hitpoints,class_dat, move_dat);self.ram_type=class_dat[0];self.type_param=class_dat[1];self.ram_delay=6;self.is_ramming=0;self.rounds+=10
 def load_texture(self,col=green):enemy_textures(self.pos_x,self.pos_y,col)
 def ram(self):
  if mc()-self.delay<self.wait_time:return
  elif player.pos_x+34>=self.pos_x and player.pos_x<self.pos_x+34 and self.dir==1 and self.pos_y>130:
   if player.hit():player.player_died()
   else:fr(self.pos_x,self.pos_y,self.size,self.size,white);self.pos_y=147;self.load_texture();self.dir*=-1;self.rounds=self.total_distance/self.px_update-3;self.delay=mc()
  elif self.total_distance/self.px_update<self.rounds:self.rounds-=1;return
  self._move_y(lime);self.rounds-=1
  if self.rounds==0:
   self.dir*=-1;self.rounds=self.total_distance/self.px_update+10
   if self.is_ramming and self.pos_y<80:self.load_texture();self.is_ramming=0;return
   self.delay=mc()
 def action(self,idx):
  if self.is_ramming:self.ram()
  elif self.ram_type=="f"and mc()-self.ram_delay>self.type_param:self.is_ramming=1;self.load_texture(lime);self.ram()
  elif self.ram_type=="r"and randrange(self.type_param)==1:self.is_ramming=1;self.load_texture(lime);self.ram()
class ShieldingShip(Enemy):
 def __init__(self,x,y,hitpoints,class_dat,move_dat):super().__init__(x,y,hitpoints,class_dat,move_dat);self.shield_chance=class_dat[0];self.shield_time=class_dat[1];self.tot_ships=class_dat[2]
 def load_texture(self,col=navy_blue):enemy_textures(self.pos_x,self.pos_y,col)
 def _indexes(self):yield 0,0;yield -1,1;yield 1,2;yield 8,3;yield 7,4;yield 9,5
 def activate_shield(self,idx):
  self.load_texture(blue);self.shield_timer=mc()
  for i,j in self._indexes():
   try:
    if j>self.tot_ships:return
    elif enemy_list[idx+i]!="dead":enemy_list[idx+i].is_shielded=1;enemy_list[idx+i]._draw_shield()
   except:continue
 def remove_shield(self,idx):
  self.load_texture()
  for i,j in self._indexes():
   if j>self.tot_ships:return
   elif enemy_list[idx+i]!="dead":enemy_list[idx+i].is_shielded=0;enemy_list[idx+i]._draw_shield(white)
 def action(self,idx):
  if self.is_shielded and mc()-self.shield_timer>self.shield_time:self.remove_shield(idx)
  elif randrange(self.shield_chance)==1 and not self.is_shielded:self.activate_shield(idx)
def place_enemies(data):
 for i in(0,1):
  if len(data[i])==0:continue
  y_position=(i*36)+3
  for j in range(8):
   x_position=j*40+2;moving_dat=data[i][j][2];class_dat=data[i][j][1]
   if data[i][j]=="dead":enemy_list.append("dead");continue
   elif data[i][j][0]=="basic":enemy_list.append(BasicShip(x_position,y_position,1,class_dat,moving_dat))
   elif data[i][j][0]=="tank":enemy_list.append(TankShip(x_position,y_position,5,class_dat,moving_dat))
   elif data[i][j][0]=="shooter":enemy_list.append(ShooterShip(x_position,y_position,2,class_dat,moving_dat));action_list.append(enemy_list[8*i+j])
   elif data[i][j][0]=="ram":enemy_list.append(RammingShip(x_position,y_position,3,class_dat,moving_dat));action_list.append(enemy_list[8*i+j])
   elif data[i][j][0]=="shield":enemy_list.append(ShieldingShip(x_position,y_position,2,class_dat,moving_dat));action_list.append(enemy_list[8*i+j])
   if moving_dat is not None and 1-isinstance(enemy_list[-1],RammingShip):moving_list.append(enemy_list[8*i+j])
   enemy_list[8*i+j].load_texture()
def next_level(lvl):lvl+=1;fr(0,0,320,240,white);_="Level "+str(lvl+1);ds(_,int(160 - 5*len(_)),100,"k");sp(2);fr(0,0,320,240,white);place_enemies(levels[lvl]);player.load_texture();return lvl
def show_tips():
 ds("< > to move",105,75,"k");ds("EXE or 4 to shoot",90,95,"k");ds("ANS to pause",100,115,"k")
 while K(4)|K(52):0
 while 1:
  if K(3)|K(0)|K(52)|K(4):fr(0,74,320,60,white);break
def title_screen(text):
 fr(0,0,320,250,navy_blue);enemy_textures(250,32,red,navy_blue);enemy_textures(50,68,green,navy_blue);Player(32,184,4,None,None)
 fr(267,100,2,6,red);fr(267,142,2,6,red);fr(49,120,2,6,red)
 for i,j in zip(text,(85,105,150)):ds(i,int(160-5*len(i)),j,white,navy_blue)
 while 1:
  if K(4)|K(52):fr(0,0,320,250,white);break
title_screen(("Cosmic Raiders","by Krumpachnik","(OK) to continue"))
player=Player(143,184,4,None,None)
place_enemies(levels[level])
show_tips()
while 1:
 if K(3)and player.pos_x<296:player.move_hor(1)
 elif K(0)and player.pos_x>-12:player.move_hor(-1)
 if K(52)|K(4):player.shoot()
 elif K(51)|K(17):
  ds("ANS to unpause",90,100,black);sp(0.2)
  while 1:
   if K(51)|K(17):fr(0,100,320,20,white);break
  sp(.2)
 for idx,i in enumerate(bullet_list):
  entity_hit=i.move(idx)
  if entity_hit=="player_hit":
   if player.hit():player.player_died()
  elif(entity_hit is None)^1:
   if enemy_list[entity_hit].hit():
    for i in[moving_list,action_list]:
     if enemy_list[entity_hit]in i:i.remove(enemy_list[entity_hit])
    enemy_list[entity_hit]="dead"
    if enemy_list.count("dead")==len(enemy_list):
     bullet_list,enemy_list,moving_list,=[],[],[]
     if level+1==len(levels):title_screen(("Congratualtions!","You won!"))
     level=next_level(level);action_delay=mc()
 if mc()-action_delay>1:
  for i in action_list:i.action(enemy_list.index(i))
 if mc()-move_delay>.1:
  for i in moving_list:
   if i.move():moving_list.remove(i)
  move_delay=mc()
 if mc()-fps_limit>.05:0
 else:sp(.05-(mc()-fps_limit))
 fps_limit=mc()

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.