Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
from processing import * from math import cos, sin, atan2 from random import randint INTERVAL = 25 GRID_SIZE = 500/INTERVAL class Bird: instance = [] lives = 2 def __init__(self,x,y,vx=0,vy=0): self.x = x self.y = y self.vx = vx self.vy = vy Bird.instance.append(self) def move(self): self.x += self.vx*bg['slowdown'] self.y += self.vy*bg['slowdown'] self.vy += .1*bg['slowdown'] class Leaf: instance = [] timer = 0 def __init__(self,x,y,vx=0,vy=0): self.x = x self.y = y self.vx = vx self.vy = vy self.cells = set() Leaf.instance.append(self) def move(self): self.x += self.vx*bg['slowdown'] self.y += self.vy*bg['slowdown'] '''for i in self.cells: i.subtract(self) self.cells = set() Grid.retrieve((self.x-10)/INTERVAL,(self.y-10)/INTERVAL) += self Grid.retrieve((self.x+10)/INTERVAL,(self.y-10)/INTERVAL) += self Grid.retrieve((self.x-10)/INTERVAL,(self.y+10)/INTERVAL) += self Grid.retrieve((self.x+10)/INTERVAL,(self.y+10)/INTERVAL) += self self.cells += (Grid.retrieve((self.x-10)/INTERVAL,(self.y-10)/INTERVAL) self.cells += (Grid.retrieve((self.x+10)/INTERVAL,(self.y-10)/INTERVAL) self.cells += (Grid.retrieve((self.x-10)/INTERVAL,(self.y+10)/INTERVAL) self.cells += (Grid.retrieve((self.x+10)/INTERVAL,(self.y+10)/INTERVAL class Grid: grid = [] def retrieve(x,y): x = int(x) y = int(y) return Grid.grid[(y*GRID_SIZE)+x] ''' #lol dictionaries!!!! bg = { 'red':150, 'keydown':0, 'meter':100, 'slowdown':1.0, 'targetslow':1.0, 'cooldown':0, 'countdown':0, 'birdr':200, 'birdb':0, 'targetr':200, 'targetb':0, 'mode':'waiting', 'score':0, } def setup(): global bird, birdimage, leafimage '''for i in range(GRID_SIZE**2): Grid.grid.append(set())''' size(500,500) bird = Bird(250,250,0,-3) birdimage = loadImage('http://oyosite.s3.amazonaws.com/userdata/img5/1633644161-59ff7bf1ca292c38749df130.png') leafimage = loadImage('http://oyosite.s3.amazonaws.com/userdata/img9/1633645629-59ff7bf1ca292c38749df130.png') def lag(a,b,scale): if a != b: if abs(b - a) < scale: a = b elif a > b: a -= scale elif a < b: a += scale return a def keyPressed(): bg['keydown'] = 1 def keyReleased(): bg['keydown'] = 0 def mousePressed(): if Bird.lives == 2: if mouse.x >= 50 and mouse.y >= 200 and mouse.x <= 200 and mouse.y <= 300: Bird.lives = 1 bg['mode'] = 'normal' bg['score'] = 600 if mouse.x >= 300 and mouse.y >= 200 and mouse.x <= 450 and mouse.y <= 300: Bird.lives = 1 bg['mode'] = 'endless' elif Bird.lives == 1: angle = atan2(mouse.y-bird.y,mouse.x-bird.x) bird.vx = cos(angle)*5 bird.vy = sin(angle)*5 def draw(): if Bird.lives == 2: imageMode(CENTER,CENTER) background(bg['red'],150,250) fill(bg['birdr'],0,bg['birdb']) ellipse(bird.x,bird.y,20,20) image(birdimage,bird.x,bird.y,20,20) fill(150) rect(50,200,150,100) rect(300,200,150,100) fill(0) textAlign(CENTER,CENTER) textSize(30) text("Normal",125,250) text("Endless",375,250) text("Click to fly",250,60) text("Any key to slow time",250,100) if Bird.lives == 1: background(bg['red'],150,250) if Leaf.timer == 0: Leaf.timer = 7 direction = randint(1,4) if direction == 1: Leaf(randint(0,500),-10,0,3) elif direction == 2: Leaf(-10,randint(0,500),3,0) elif direction == 3: Leaf(randint(0,500),510,0,-3) elif direction == 4: Leaf(510,randint(0,500),-3,0) else: Leaf.timer -= 1 if bg['keydown'] == 1 and bg['meter'] and bg['cooldown'] == 0: if bg['red'] < 200: bg['red'] += 5 if bg['meter'] > 0: bg['meter'] -= .5 bg['targetslow'] = (0.5/25)*(bg['meter']/4) if bg['meter'] < 0: bg['meter'] = 0 else: bg['targetslow'] = 1 if bg['meter'] < 100: bg['meter'] += .25 if bg['red'] > 150: bg['red'] -= 5 if bg['meter'] <= 0: bg['countdown'] = 200 bg['cooldown'] = 1 bg['targetr'] = 0 bg['targetb'] = 200 elif bg['countdown'] > 0: bg['countdown'] -= 1 if bg['countdown'] == 0: bg['cooldown'] = 0 bg['targetr'] = 200 bg['targetb'] = 0 bg['slowdown'] = lag(bg['slowdown'], bg['targetslow'], 0.1) fill(150) rect(130,450,240,50) fill(60) rect(150,465,200,25) if bg['mode'] == 'normal': bg['score'] -= bg['slowdown'] if bg['score'] <= 0: Bird.lives = 0 if bg['mode'] == 'endless': bg['score'] += bg['slowdown'] textAlign(LEFT,CENTER) textSize(30) text(bg['score'],225,10) bg['birdr'] = lag(bg['birdr'],bg['targetr'],10) bg['birdb'] = lag(bg['birdb'],bg['targetb'],10) fill(bg['birdr'],0,bg['birdb']) rect(150,465,bg['meter']*2,25) imageMode(CENTER,CENTER) bird.move() ellipse(bird.x,bird.y,20,20) image(birdimage,bird.x,bird.y,20,20) fill(0,255,0) for i in Leaf.instance: i.move() image(leafimage,i.x,i.y,20,20) if i.x > 550 or i.x < -50 or i.y > 550 or i.y < -50: Leaf.instance.remove(i) if ((i.x-bird.x)**2+(i.y-bird.y)**2)**.5 <= 20: Bird.lives = 0 if bird.x > 510 or bird.x < -10 or bird.y > 510 or bird.y < -10: Bird.lives = 0 if Bird.lives == 0: textAlign(CENTER,CENTER) textSize(40) fill(150) rectMode(CENTER,CENTER) rect(250,250,300,100) fill(0) if bg['mode'] == 'normal': if bg['score'] <= 0: text('Congratulations',250,250) else: text('Ouch',250,250) if bg['mode'] == 'endless': if len(str(bg['score'])) > 8: textSize(30) text('Score:'+str(bg['score']),250,250) run()
flying2bird.py
( around 205 lines python code )
Published By:
luyfru
Published on
2023-09-21T19:25:21Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here