swarm.py

Created by florian-allard

Created on April 29, 2025

3.69 KB

Adaptation du homebrew Swarm (sorti sur PSP).

Le but du jeu est de survivre en esquivant l’essaim (“swarm”) de carrés qui suit le joueur. Les poursuivants noirs font perdre 2 PV (points de vie) en cas de collision. Les poursuivants verts font regagner 20 PV en cas de collision (il est même possible de dépasser 100 PV !). La barre verte en haut de l’écran donne la progression avant d’atteindre le niveau suivant (progression du score par rapport au score visé). La couleur de fond du jeu (qui tire sur le rouge en cas de PV faibles) donne une indication sur les PV restants.

On se déplace avec les 4 flèches.

Bon jeu !


from kandinsky import *
from time import *
from random import *
from ion import *
from math import atan

L,H=320,222


class Poursuivant:
  def __init__(self,vie,Type="normal"):
    self.x=randint(0,L)
    self.y=randint(5,H-24)
    self.dx=atan(abs(X-self.x)/max(1,abs(Y-self.y)))*(-1,1)[X>self.x]
    self.dy=atan(abs(Y-self.y)/max(1,abs(X-self.x)))*(-1,1)[Y>self.y]
    self.vie=vie
    self.Type=Type

  def actualise(self):
    self.dx=atan(abs(X-self.x)/max(1,abs(Y-self.y)))*(-1,1)[X>self.x]
    self.dy=atan(abs(Y-self.y)/max(1,abs(X-self.x)))*(-1,1)[Y>self.y]
    self.x+=2*self.dx
    self.y+=2*self.dy

  def test_collision(self):
    if (self.x-X)**2+(self.y-Y)**2<10:
      return True
    else:
      return False

  def affiche(self):
    fill_rect(int(self.x),int(self.y),3,3,((80,80,80),(0,255,100))[self.Type=="soin"])

  def efface(self):
    fill_rect(int(self.x),int(self.y),3,3,(255,min(255,55+2*Vie),min(255,55+2*Vie)))#"w")


# les poursuivants :
NB_poursuivants=20
Vie_max=200
Niveau=1


fill_rect(0,0,L,H-21,(192,192,192))
fill_rect(0,201,L,1,"purple")
fill_rect(0,202,L,20,(216,216,216))

for (a,b,c) in ((64,0,-1),(1,65,1)):
  for i in range(a,b,c):
    #fill_rect(0,0,L,H-21,(4*i,4*i,4*i))
    draw_string("Swarm.",130,80,(256-i,256-i,256-i),(64+2*i,64+2*i,64+2*i))
    sleep(0.02)
  sleep(2)

draw_string("Vie",5,202,(0,128,0),(216,216,216))
draw_string("Niveau",125,202,"b",(216,216,216))
draw_string("Score",220,202,"r",(216,216,216))

while 1:
  Vie=100
  X,Y=L//2,H//2
  Score_objectif=100*Niveau # 3.5*Niveau (sec)
  Score=0
  fill_rect(0,0,L,H-21,(192,92+Vie,92+Vie))
  #fill_rect(0,0,L,H-21,"k")
  draw_string(str(Vie)+"/100",40,202,(0,128,0),(216,216,216))
  draw_string("0   ",275,202,"r",(216,216,216))
  draw_string(str(Niveau),190,202,"b",(216,216,216))

  for (a,b,c) in ((64,0,-1),(1,65,1)):
    for i in range(a,b,c):
      #fill_rect(0,0,L,H-21,(4*i,4*i,4*i))
      draw_string("Niveau "+str(Niveau),115,80,(256-i,256-i,256-i),(64+2*i,64+2*i,64+2*i))
      draw_string("Score visé : "+str(Score_objectif),75,100,(256-i,256-i,256-i),(64+2*i,64+2*i,64+2*i))
      sleep(0.02)
    sleep(1)
  #sleep(3)
  fill_rect(X,Y,4,4,"purple")
  Liste_poursuivants=[]
  for i in range(NB_poursuivants):
    Liste_poursuivants.append(Poursuivant(Vie_max-i))

  #sleep(2)
  while Score<Score_objectif and Vie>0:
    for poursuivant in Liste_poursuivants:
      #poursuivant.efface()
      poursuivant.actualise()
      poursuivant.vie-=1
      if poursuivant.test_collision():
        if poursuivant.Type=="soin":
          Vie+=20
        else:
          Vie-=2
        Liste_poursuivants.remove(poursuivant)
        draw_string(" "*(Vie<100)+" "*(Vie<10)+str(Vie),40,202,(0,128,0),(216,216,216))
      if poursuivant.vie>0:
        poursuivant.affiche()
    fill_rect(0,5,L,H-21-5,(192,min(255,92+Vie),min(255,92+Vie))) # à mettre 13 ou 14 lignes plus haut ?
    fill_rect(int(X),int(Y),4,4,"purple")
    for poursuivant in Liste_poursuivants:
      if poursuivant.vie>0:
        poursuivant.affiche()
      else:
        Liste_poursuivants.remove(poursuivant)
    if len(Liste_poursuivants)<NB_poursuivants:
      Liste_poursuivants.append(Poursuivant(Vie_max,("normal","soin")[randint(0,NB_poursuivants)<1]))

    dx=3*(keydown(KEY_RIGHT)-keydown(KEY_LEFT))
    dy=3*(keydown(KEY_DOWN)-keydown(KEY_UP))
    if dx or dy:
      fill_rect(int(X),int(Y),4,4,(192,min(255,92+Vie),min(255,92+Vie)))
      Coeff=[1,0.57][dx!=0 and dy!=0]
      X=(X+dx*Coeff)%(L-4)
      Y=(Y+dy*Coeff-5)%(H-20-4-5)+5
      fill_rect(int(X),int(Y),4,4,"purple")
    #sleep(0.2)
    Score+=1.2
    draw_string(str(int(round(Score,-1))),275,202,"r",(216,216,216))
    fill_rect(0,0,int(320*Score/Score_objectif),5,"g")
  if Score>=Score_objectif:
    Niveau+=1

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.