endless_runner.py

Created by loic-azavant

Created on January 12, 2023

3.73 KB

fonctionne seulement avec upsilon


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

class Car:
  def __init__(self):
    self.x = 160
    self.y = 180
    self.sideAngle = 20
    self.sideLength = 20
    self.angle = 0
    self.col = (255,0,0)
    self.speed = 0
    self.aabb = (3,9)
  
  def drw(self,col):
    fill_polygon([(round(self.x+sin(radians(self.angle+self.sideAngle))*self.sideLength),round(self.y+cos(radians(self.angle+self.sideAngle))*self.sideLength)),
                  (round(self.x+sin(radians(self.angle-self.sideAngle))*self.sideLength),round(self.y+cos(radians(self.angle-self.sideAngle))*self.sideLength)),
                  (round(self.x+sin(radians(self.angle+180+self.sideAngle))*self.sideLength),round(self.y+cos(radians(self.angle+180+self.sideAngle))*self.sideLength)),
                  (round(self.x+sin(radians(self.angle+180-self.sideAngle))*self.sideLength),round(self.y+cos(radians(self.angle+180-self.sideAngle))*self.sideLength))],col)

  def rotate(self,deg):
    self.drw((255,255,255))
    self.angle += deg
    self.drw(self.col)
  
  def move(self,x):
    self.drw((255,)*3)
    self.x += x
    self.drw(self.col)
  
  def updt(self):
    moveH = keydown(3)-keydown(0)
    self.speed = max(-2,min(2,self.speed+moveH*0.1))
    if moveH==0:
      self.speed /= 1.1
    self.drw((255,)*3)
    self.angle = -self.speed*10
    oldX = self.x
    self.x = max(65,min(255,self.x+self.speed))
    if self.x==oldX:
      self.angle = -self.speed*5
      self.speed = 0
    self.drw(self.col)

class Game:
  def __init__(self):
    self.objectCoord = []
    self.objectNbr = 0
    self.objectSpeed = 1
    self.objectSize = 10
    self.objectSpawn = 20
    self.objectKilled = 0
    self.startTime = monotonic()
    self.heart = 3
    self.phase = 1
  
  def drwHeart(self):
    fill_rect(0,0,55,222,(0,180,65))
    for i in range(self.heart):
      fill_rect(5,5+25*i,20,20,(255,0,0))
      
  def updt(self):
    for i in self.objectCoord:
      fill_rect(round(i[0]),round(i[1]),self.objectSize,self.objectSize,(255,)*3)
      i[1] += self.objectSpeed
      if i[1]>222:
        self.objectCoord.remove(i)
        self.objectKilled += 1
        continue
      if not (i[0]>=a.x+a.aabb[0] or i[0]+self.objectSize<=a.x-a.aabb[0] or i[1]>=a.y+a.aabb[1] or i[1]+self.objectSize<=a.y-a.aabb[1]):
        self.heart -= choice([1,1,1,1,1,1,1,1,1,1,-1])
        if self.heart == 0:
          draw_string("Tu as perdu",10,10)
          score = self.objectKilled*self.phase
          draw_string("Ton score: "+str(score),10,40)
          try:
            f = open("best.score","r")
            bs = int(f.read())
            if score > bs:
              g = open("best.score","w")
              g.write(str(score))
              g.close()
              draw_string("meilleur score: "+str(score),10,70)
            else:
              draw_string("meilleur score: "+str(bs),10,70)
            f.close()
          except :
            g = open("best.score","w")
            g.write(str(score))
            g.close()
            draw_string("meilleur score: "+str(score),10,70)
          while 1: pass
        self.drwHeart()
        self.objectCoord.remove(i)
        continue
      fill_rect(round(i[0]),round(i[1]),self.objectSize,self.objectSize,(0,0,255))
    if randint(0,self.objectSpawn)==1:
      self.objectCoord.append([randint(65,245),-self.objectSize])
    if monotonic()-self.startTime >= 10:
      self.startTime = monotonic()
      self.objectSpawn = max(10,self.objectSpawn-1)
      self.phase += 1
      self.objectSpeed = min(3,self.objectSpeed+randint(0,1)*0.5)
    
a = Car()
a.drw(a.col)
b = Game()
fill_rect(0,0,55,222,(0,180,65))
fill_rect(265,0,55,222,(0,180,65))
b.drwHeart()
startTime = monotonic()
while 1:
  a.updt()
  b.updt()

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.