flappy.py

Created by pikube

Created on November 25, 2019

2.37 KB

ATTENTION: CE PROGRAMME NECESSITE OMEGA POUR FONCTIONNER (voir https://github.com/omega-numworks/)

Flappy bird: Appuyez sur la flèche du haut pour sauter. Il est possible que les collisions ne fonctionnent pas de la même manière pour chaque calculatrice. Si c’est le cas pour la votre, faites <fill_rect(0,0,320,222,(255,255,255)),get_pixel(0,0)>. Remplacez (248,252,248) à la ligne 36 par la liste obtenue.


from kandinsky import *
import time
import random

class birds():
  def __init__(self):
    self.x=85
    self.y=131
    self.vy=-5
    self.mass=2
    self.speed=16
    self.living=1
  def draw(self,t2):
    fill_rect(int(self.x),int(self.y),20,20,(int(25*t2-89)%256,int(-20*t2+89)%256,int(t2*4+56)%256))
  def jump(self):
    self.vy-=self.speed
  def die(self,t2):
    self.living=0
    const=202-self.y
    for loop in range(202-self.y):
      grad=(const-loop)/const
      self.y+=1
      fill_rect(self.x,self.y,20,20,(int(grad*(int(25*t2-89)%256)),int(grad*(int(-20*t2+89)%256)),int(grad*(int(t2*4+56)%256))))
      time.sleep(0.003)
  def move(self,t2):
    fill_rect(self.x,self.y,20,20,(255,255,255))
    if (self.y+self.vy)>=202 or self.y+self.vy<0:
      self.die(t2)
      self.vy=0
    if (self.y+self.vy)<202:
      self.y+=self.vy
      if self.vy<10:
        self.vy+=self.mass
      #collisions
      if get_pixel(self.x+21,self.y+self.vy+10)!=(248,252,248):
        self.die(t2)
    self.draw(t2)      

class pipe():
  def __init__(self,y,direction):
    self.x=321
    self.y=y
    self.direction=direction
  def move(self):
    self.x-=5
  def render(self,t):
    col=(int(25*t-89)%256,int(-20*t+89)%256,int(4*t+56)%256)
    if self.direction=="up":
      fill_rect(self.x+5,self.y-111-50,50,111+75,(255,255,255))
      fill_rect(self.x,self.y-111-50,50,111+75,col)
    if self.direction=="down":
      fill_rect(self.x+5,self.y+111-25,50,320,(255,255,255))
      fill_rect(self.x,self.y+111-25,50,320,col)
    

def play():
  bird=birds()
  jumpability=1
  t1=time.monotonic()
  pipes=[]
  score=-2
  while bird.living==1:
    t2=time.monotonic()
    #jump system
    if [get_keys()].count({"right"})>0 and jumpability==1:
          bird.jump()
          jumpability=0
    if get_keys()==set():
          jumpability=1
    #create pipes
    if t2-t1>1:
      score+=1
      t1=t2
      ry=random.randint(0,140)
      pipes.append(pipe(ry,"up"))
      pipes.append(pipe(ry,"down"))
      for loop in range(len(pipes)):
        if pipes[0].x<-50:
          pipes.remove(pipes[0])
    #draw scene  
    for loop in range(len(pipes)):
      pipes[loop].render(t2)
      pipes[loop].move()
    bird.move(t2)
    time.sleep(0.03)
    draw_string("score:%s"%int((score+abs(score))/2),0,0)
  fill_rect(85,202,20,20,(0,0,0))
    
while 1:
  play()
  time.sleep(1)
  fill_rect(0,0,320,222,(255,255,255))

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.