Skip to content

Commit 161c14f

Browse files
authored
Add files via upload
1 parent 101f428 commit 161c14f

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

Stack_Tower/Stacks.py

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
# -----------------------------------------------------------------------------
2+
#
3+
# Stack Tower
4+
# Language - Python
5+
# Modules - pygame, sys, random
6+
#
7+
# Controls - Mouse Click
8+
#
9+
# By - Jatin Kumar Mandav
10+
#
11+
# Website - https://jatinmandav.wordpress.com
12+
#
13+
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
14+
# Twitter - @jatinmandav
15+
#
16+
# -----------------------------------------------------------------------------
17+
18+
import pygame
19+
import sys
20+
import random
21+
22+
pygame.init()
23+
24+
width = 400
25+
height = 500
26+
display = pygame.display.set_mode((width, height))
27+
clock = pygame.time.Clock()
28+
29+
background = (23, 32, 42)
30+
31+
white = (236, 240, 241)
32+
33+
# Color Codes
34+
color = [(120, 40, 31), (148, 49, 38), (176, 58, 46), (203, 67, 53), (231, 76, 60), (236, 112, 99), (241, 148, 138), (245, 183, 177), (250, 219, 216), (253, 237, 236),
35+
(254, 249, 231), (252, 243, 207), (249, 231, 159), (247, 220, 111), (244, 208, 63), (241, 196, 15), (212, 172, 13), (183, 149, 11), (154, 125, 10), (125, 102, 8),
36+
(126, 81, 9), (156, 100, 12), (185, 119, 14), (202, 111, 30), (214, 137, 16), (243, 156, 18), (245, 176, 65), (248, 196, 113),(250, 215, 160), (253, 235, 208), (254, 245, 231),
37+
(232, 246, 243), (162, 217, 206), (162, 217, 206),
38+
(115, 198, 182), (69, 179, 157), (22, 160, 133),
39+
(19, 141, 117), (17, 122, 101), (14, 102, 85),
40+
(11, 83, 69),
41+
(21, 67, 96), (26, 82, 118), (31, 97, 141),
42+
(36, 113, 163), (41, 128, 185), (84, 153, 199),
43+
(127, 179, 213), (169, 204, 227), (212, 230, 241),
44+
(234, 242, 248),
45+
(251, 238, 230), (246, 221, 204), (237, 187, 153),
46+
(229, 152, 102), (220, 118, 51), (211, 84, 0),
47+
(186, 74, 0), (160, 64, 0), (135, 54, 0),
48+
(110, 44, 0)
49+
]
50+
51+
colorIndex = 0
52+
53+
brickH = 10
54+
brickW = 100
55+
56+
score = 0
57+
speed = 3
58+
59+
60+
# Single Brick Class
61+
class Brick:
62+
def __init__(self, x, y, color, speed):
63+
self.x = x
64+
self.y = y
65+
self.w = brickW
66+
self.h = brickH
67+
self.color = color
68+
self.speed = speed
69+
70+
def draw(self):
71+
pygame.draw.rect(display, self.color, (self.x, self.y, self.w, self.h))
72+
73+
def move(self):
74+
self.x += self.speed
75+
if self.x > width:
76+
self.speed *= -1
77+
if self.x + self.w < 1:
78+
self.speed *= -1
79+
80+
81+
# Complete Stack
82+
class Stack:
83+
def __init__(self):
84+
global colorIndex
85+
self.stack = []
86+
self.initSize = 25
87+
for i in range(self.initSize):
88+
newBrick = Brick(width/2 - brickW/2, height - (i + 1)*brickH, color[colorIndex], 0)
89+
colorIndex += 1
90+
self.stack.append(newBrick)
91+
92+
def show(self):
93+
for i in range(self.initSize):
94+
self.stack[i].draw()
95+
96+
def move(self):
97+
for i in range(self.initSize):
98+
self.stack[i].move()
99+
100+
def addNewBrick(self):
101+
global colorIndex, speed
102+
103+
if colorIndex >= len(color):
104+
colorIndex = 0
105+
106+
y = self.peek().y
107+
if score > 50:
108+
speed += 0
109+
elif score%5 == 0:
110+
speed += 1
111+
112+
newBrick = Brick(width, y - brickH, color[colorIndex], speed)
113+
colorIndex += 1
114+
self.initSize += 1
115+
self.stack.append(newBrick)
116+
117+
def peek(self):
118+
return self.stack[self.initSize - 1]
119+
120+
def pushToStack(self):
121+
global brickW, score
122+
b = self.stack[self.initSize - 2]
123+
b2 = self.stack[self.initSize - 1]
124+
if b2.x <= b.x and not (b2.x + b2.w < b.x):
125+
self.stack[self.initSize - 1].w = self.stack[self.initSize - 1].x + self.stack[self.initSize - 1].w - b.x
126+
self.stack[self.initSize - 1].x = b.x
127+
if self.stack[self.initSize - 1].w > b.w:
128+
self.stack[self.initSize - 1].w = b.w
129+
self.stack[self.initSize - 1].speed = 0
130+
score += 1
131+
elif b.x <= b2.x <= b.x + b.w:
132+
self.stack[self.initSize - 1].w = b.x + b.w - b2.x
133+
self.stack[self.initSize - 1].speed = 0
134+
score += 1
135+
else:
136+
gameOver()
137+
for i in range(self.initSize):
138+
self.stack[i].y += brickH
139+
140+
brickW = self.stack[self.initSize - 1].w
141+
142+
# Game Over
143+
def gameOver():
144+
loop = True
145+
146+
font = pygame.font.SysFont("Agency FB", 60)
147+
text = font.render("Game Over!", True, white)
148+
149+
textRect = text.get_rect()
150+
textRect.center = (width/2, height/2 - 80)
151+
152+
while loop:
153+
for event in pygame.event.get():
154+
if event.type == pygame.QUIT:
155+
close()
156+
if event.type == pygame.KEYDOWN:
157+
if event.key == pygame.K_q:
158+
close()
159+
if event.key == pygame.K_r:
160+
gameLoop()
161+
if event.type == pygame.MOUSEBUTTONDOWN:
162+
gameLoop()
163+
display.blit(text, textRect)
164+
165+
pygame.display.update()
166+
clock.tick()
167+
168+
# Displaying the Score on Screen
169+
def showScore():
170+
font = pygame.font.SysFont("Forte", 30)
171+
text = font.render("Score: " + str(score), True, white)
172+
display.blit(text, (10, 10))
173+
174+
175+
# Close the Window
176+
def close():
177+
pygame.quit()
178+
sys.exit()
179+
180+
181+
# The Main Game Loop
182+
def gameLoop():
183+
global brickW, brickH, score, colorIndex, speed
184+
loop = True
185+
186+
brickH = 10
187+
brickW = 100
188+
colorIndex = 0
189+
speed = 3
190+
191+
score = 0
192+
193+
stack = Stack()
194+
stack.addNewBrick()
195+
196+
while loop:
197+
for event in pygame.event.get():
198+
if event.type == pygame.QUIT:
199+
close()
200+
if event.type == pygame.KEYDOWN:
201+
if event.key == pygame.K_q:
202+
close()
203+
if event.key == pygame.K_r:
204+
gameLoop()
205+
if event.type == pygame.MOUSEBUTTONDOWN:
206+
stack.pushToStack()
207+
stack.addNewBrick()
208+
209+
210+
display.fill(background)
211+
212+
stack.move()
213+
stack.show()
214+
215+
showScore()
216+
217+
pygame.display.update()
218+
clock.tick(60)
219+
220+
gameLoop()

0 commit comments

Comments
 (0)