Skip to content

Commit b8babe2

Browse files
Merge pull request #229 from ash-heinz/feature/flappy-game
Feature/flappy game
2 parents a54ac82 + ce660f8 commit b8babe2

5 files changed

Lines changed: 425 additions & 458 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,20 @@ Find all the hidden differences between two interactive canvases!
301301
- ⏱️ Built-in timer and hint system
302302
- 🌐 *Web App Exclusive Project*
303303

304+
</td>
305+
</tr>
306+
<tr>
307+
<td width="50%">
308+
309+
#### 🐦 Flappy Game
310+
Fly through pipes and survive as long as possible!
311+
- 🦅 Interactive jump mechanics
312+
- 💥 Collision detection
313+
- 🏆 High score tracking
314+
```bash
315+
python games/Flappy-Game/Flappy-Game.py
316+
```
317+
304318
</td>
305319
</tr>
306320
</table>

games/Flappy-Game/Flappy-Game.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from random import randrange
2+
from turtle import *
3+
import math
4+
5+
class Vector:
6+
def __init__(self, x, y):
7+
self.x = x
8+
self.y = y
9+
10+
def move(self, other):
11+
"""move this vector by adding another vector to it"""
12+
self.x += other.x
13+
self.y += other.y
14+
15+
def __sub__(self, other):
16+
"""subtract two vectors to find the difference"""
17+
return Vector(self.x - other.x, self.y - other.y)
18+
19+
def __abs__(self):
20+
return math.hypot(self.x, self.y)
21+
22+
bird = Vector(0, 0)
23+
balls = []
24+
score = 0
25+
game_over = False
26+
27+
def tap(x, y):
28+
"""move bird up in response to screen tap or reset if dead"""
29+
global game_over
30+
31+
if game_over:
32+
reset_game()
33+
else:
34+
up = Vector(0, 30)
35+
bird.move(up)
36+
37+
def reset_game():
38+
"""resets the game state and starts the loop again"""
39+
global game_over, score
40+
game_over = False
41+
score = 0
42+
bird.x, bird.y = 0, 0
43+
balls.clear()
44+
move()
45+
46+
def inside(point):
47+
"""return True if point on screen"""
48+
return -200 < point.x < 200 and -200 < point.y < 200
49+
50+
def draw(alive):
51+
clear()
52+
53+
goto(bird.x, bird.y)
54+
if alive:
55+
dot(10, '#06b6d4')
56+
else:
57+
dot(10, '#ef4444')
58+
59+
for ball in balls:
60+
goto(ball.x, ball.y)
61+
dot(20, '#8b5cf6')
62+
63+
goto(-190, 180)
64+
color('white')
65+
write(f"Score: {score}", font=("Arial", 14, "bold"))
66+
67+
if not alive:
68+
goto(0, 20)
69+
write("💥 GAME OVER 💥", align="center", font=("Arial", 24, "bold"))
70+
goto(0, -20)
71+
write("🔄 Click anywhere to Play Again", align="center", font=("Arial", 14, "normal"))
72+
73+
update()
74+
75+
def move():
76+
"""update object positions"""
77+
global score, game_over
78+
79+
if game_over:
80+
return
81+
82+
bird.y -= 5
83+
84+
for ball in balls:
85+
ball.x -= 3
86+
87+
if randrange(10) == 0:
88+
y = randrange(-199, 199)
89+
ball = Vector(199, y)
90+
balls.append(ball)
91+
92+
while len(balls) > 0 and not inside(balls[0]):
93+
balls.pop(0)
94+
score += 1
95+
96+
if not inside(bird):
97+
game_over = True
98+
draw(False)
99+
return
100+
101+
for ball in balls:
102+
if abs(ball - bird) < 15:
103+
game_over = True
104+
draw(False)
105+
return
106+
107+
draw(True)
108+
ontimer(move, 50)
109+
110+
setup(420, 420, 370, 0)
111+
bgcolor('#0f172a')
112+
hideturtle()
113+
up()
114+
tracer(False)
115+
onscreenclick(tap)
116+
move()
117+
done()

web-app/index.html

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ <h1>🎮 Python Mini Projects</h1>
320320
</nav>
321321

322322
<main id="main-content" tabindex="-1">
323-
324323
<!-- Search & Category Tabs -->
325324
<section class="tabs-section" aria-label="Search and Project categories">
326325
<div class="container">
@@ -405,31 +404,31 @@ <h3>Rock Paper Scissors</h3>
405404
</div>
406405

407406
<div class="project-card" data-category="games" data-project="dice-rolling"
408-
data-tags="game,random,animation,probability">
407+
data-tags="game,random,animation,probability">
409408
<div class="card-icon">🎲</div>
410409
<h3>Dice Rolling</h3>
411410
<p>Roll the dice with 3D animation!</p>
412411
<button class="btn-play">Try It</button>
413412
</div>
414413

415414
<div class="project-card" data-category="games" data-project="coin-flip"
416-
data-tags="game,random,animation,probability,heads,tails">
415+
data-tags="game,random,animation,probability,heads,tails">
417416
<div class="card-icon">🪙</div>
418417
<h3>Coin Flip</h3>
419418
<p>Heads or tails with spinning animation!</p>
420419
<button class="btn-play">Try It</button>
421420
</div>
422421

423422
<div class="project-card" data-category="games" data-project="number-guessing"
424-
data-tags="game,logic,loops,conditional">
423+
data-tags="game,logic,loops,conditional">
425424
<div class="card-icon">🎯</div>
426425
<h3>Number Guessing</h3>
427426
<p>Guess the secret number!</p>
428427
<button class="btn-play">Try It</button>
429428
</div>
430429

431430
<div class="project-card" data-category="games" data-project="hangman"
432-
data-tags="game,word,letters,logic,string">
431+
data-tags="game,word,letters,logic,string">
433432
<div class="card-icon">🎮</div>
434433
<h3>Hangman</h3>
435434
<p>Classic word-guessing game!</p>
@@ -492,6 +491,15 @@ <h3>Whack-a-Mole</h3>
492491
<button class="btn-play">Try It</button>
493492
</div>
494493

494+
<div class="project-card" data-category="games" data-project="flappy-game">
495+
<div class="card-icon">🐦</div>
496+
<h3>Flappy Game</h3>
497+
<p>Dodge the incoming balls and survive!</p>
498+
<button class="btn-play">Try It</button>
499+
</div>
500+
501+
<div class="project-card" data-category="math" data-project="fibonacci"
502+
data-tags="math,sequence,series,loops,recursion">
495503
<div class="project-card" data-category="games" data-project="game2048"
496504
data-tags="game,puzzle,2048,logic,tiles">
497505
<div class="card-icon">🟦</div>
@@ -510,88 +518,87 @@ <h3>Fibonacci Series</h3>
510518
</div>
511519

512520
<div class="project-card" data-category="math" data-project="progression-recognizer"
513-
data-tags="math,sequence,progression,arithmetic,geometric">
521+
data-tags="math,sequence,progression,arithmetic,geometric">
514522
<div class="card-icon">📐</div>
515523
<h3>AP/GP/AGP/HP Recognizer</h3>
516524
<p>Identify progression types from any sequence.</p>
517525
<button class="btn-play">Try It</button>
518526
</div>
519527

520528
<div class="project-card" data-category="math" data-project="pascal-triangle"
521-
data-tags="math,triangle,visualization,recursion">
529+
data-tags="math,triangle,visualization,recursion">
522530
<div class="card-icon">🔺</div>
523531
<h3>Pascal's Triangle</h3>
524532
<p>Beautiful hexagon visualization!</p>
525533
<button class="btn-play">Try It</button>
526534
</div>
527535

528536
<div class="project-card" data-category="math" data-project="armstrong"
529-
data-tags="math,number,digits,power">
537+
data-tags="math,number,digits,power">
530538
<div class="card-icon">💎</div>
531539
<h3>Armstrong Numbers</h3>
532540
<p>Check special number properties!</p>
533541
<button class="btn-play">Try It</button>
534542
</div>
535543

536544
<div class="project-card" data-category="math" data-project="calculator"
537-
data-tags="math,calculator,arithmetic,operations">
545+
data-tags="math,calculator,arithmetic,operations">
538546
<div class="card-icon">🧮</div>
539547
<h3>Calculator</h3>
540548
<p>Your mathematical companion!</p>
541549
<button class="btn-play">Try It</button>
542550
</div>
543551

544552
<div class="project-card" data-category="math" data-project="collatz"
545-
data-tags="math,conjecture,algorithm,sequence">
553+
data-tags="math,conjecture,algorithm,sequence">
546554
<div class="card-icon">🔢</div>
547555
<h3>Collatz Conjecture</h3>
548556
<p>Explore the 3n+1 problem!</p>
549557
<button class="btn-play">Try It</button>
550558
</div>
551559

552560
<div class="project-card" data-category="math" data-project="prime-analyzer"
553-
data-tags="math,prime,factors,algorithm">
561+
data-tags="math,prime,factors,algorithm">
554562
<div class="card-icon">🔱</div>
555563
<h3>Prime Analyzer</h3>
556564
<p>All-in-one prime number toolkit!</p>
557565
<button class="btn-play">Try It</button>
558566
</div>
559567

560568
<div class="project-card" data-category="math" data-project="projectile-motion"
561-
data-tags="math,physics,motion,calculation">
569+
data-tags="math,physics,motion,calculation">
562570
<div class="card-icon">🚀</div>
563571
<h3>Projectile Motion</h3>
564572
<p>Calculate TOF, Hmax, and Range with physics!</p>
565573
<button class="btn-play">Try It</button>
566574
</div>
567575

568576
<div class="project-card" data-category="math" data-project="coordinate-polar-transform"
569-
data-tags="math,coordinate,transformation,geometry">
577+
data-tags="math,coordinate,transformation,geometry">
570578
<div class="card-icon">🧭</div>
571579
<h3>Coordinate to Polar</h3>
572580
<p>Transform Cartesian (x, y) into polar (r, theta).</p>
573581
<button class="btn-play">Try It</button>
574582
</div>
575583

576584
<div class="project-card" data-category="math" data-project="derivative-calculator"
577-
data-tags="math,calculus,derivative,polynomial">
585+
data-tags="math,calculus,derivative,polynomial">
578586
<div class="card-icon"></div>
579587
<h3>Derivative Calculator</h3>
580588
<p>Compute 1st/nth polynomial derivatives and evaluate them.</p>
581589
<button class="btn-play">Try It</button>
582590
</div>
583591

584-
<!-- UTILITIES -->
585592
<div class="project-card" data-category="utilities" data-project="morse-code"
586-
data-tags="utility,morse,translation,communication">
593+
data-tags="utility,morse,translation,communication">
587594
<div class="card-icon">📻</div>
588595
<h3>Morse Code</h3>
589596
<p>Translate with lights &amp; sound!</p>
590597
<button class="btn-play">Try It</button>
591598
</div>
592599

593600
<div class="project-card" data-category="utilities" data-project="tower-of-hanoi"
594-
data-tags="utility,puzzle,recursion,algorithm">
601+
data-tags="utility,puzzle,recursion,algorithm">
595602
<div class="card-icon">🗼</div>
596603
<h3>Tower of Hanoi</h3>
597604
<p>Solve the classic puzzle!</p>

0 commit comments

Comments
 (0)