├── .vs └── slnx.sqlite ├── 8_Ball_Pool └── 8BallPool.py ├── Angry_Birds ├── Fonts │ ├── Comic_Kings.ttf │ └── arfmoochikncheez.ttf ├── Images │ ├── bird.png │ ├── block1.png │ ├── block_destroyed1.png │ ├── game_play1.png │ ├── game_play2.png │ ├── pig1.png │ ├── pig3.png │ ├── pig_damaged.png │ ├── wall_horizontal.png │ └── wall_vertical.png ├── README.md ├── __pycache__ │ ├── interface.cpython-35.pyc │ ├── maps.cpython-35.pyc │ ├── objects.cpython-35.pyc │ └── physics_engine.cpython-35.pyc ├── interface.py ├── main.py ├── maps.py ├── objects.py └── physics_engine.py ├── BalloonShooter └── BalloonShooter.py ├── Battles ├── Battles - Game.rar └── Code │ ├── Battles.py │ ├── Data │ ├── customMaps │ ├── mapReached │ └── maps │ ├── GUI.py │ ├── Images │ ├── 1-stars.png │ ├── 2-stars.png │ ├── 3-stars.png │ ├── Helicopter.png │ └── Tank.png │ ├── Maps.py │ ├── Structures.py │ └── Troops.py ├── Chain_Reaction └── Chain-Reaction.py ├── Dodge_The_Ball └── DodgeTheBall.py ├── Hangman └── hangman.py ├── LICENSE ├── Minesweeper └── Minesweeper.py ├── Pong └── Pong.py ├── README.md ├── Snake_2d └── snake.py ├── Space_Invaders └── space invaders.py ├── Stack_Tower └── Stacks.py ├── Stopwatch └── Stopwatch.py ├── Tic Tac Toe .py └── Tron └── Tron.py /.vs/slnx.sqlite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/.vs/slnx.sqlite -------------------------------------------------------------------------------- /8_Ball_Pool/8BallPool.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # 8 Ball Pool 4 | # Language - Python 5 | # Modules - pygame, sys, random, math 6 | # 7 | # Controls - Mouse, the length of Stick is propotional to Force applied 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 | from math import * 21 | import random 22 | 23 | pygame.init() 24 | width = 660 25 | height = 360 26 | outerHeight = 400 27 | margin = 30 28 | display = pygame.display.set_mode((width, outerHeight)) 29 | pygame.display.set_caption("8 Ball Pool") 30 | clock = pygame.time.Clock() 31 | 32 | background = (51, 51, 51) 33 | white = (236, 240, 241) 34 | 35 | gray = (123, 125, 125) 36 | 37 | black = (23, 32, 42) 38 | yellow = (244, 208, 63) 39 | blue = (52, 152, 219) 40 | red = (203, 67, 53) 41 | purple = (136, 78, 160) 42 | orange = (230, 126, 34) 43 | green = (40, 180, 99) 44 | brown = (100, 30, 22) 45 | stickColor = (249, 231, 159) 46 | 47 | colors = [yellow, blue, red, purple, orange, green, brown, black, yellow, blue, red, purple, orange, green, brown] 48 | 49 | balls = [] 50 | noBalls = 15 51 | radius = 10 52 | friction = 0.005 53 | 54 | # Ball Class 55 | class Ball: 56 | def __init__(self, x, y, speed, color, angle, ballNum): 57 | self.x = x + radius 58 | self.y = y + radius 59 | self.color = color 60 | self.angle = angle 61 | self.speed = speed 62 | self.ballNum = ballNum 63 | self.font = pygame.font.SysFont("Agency FB", 10) 64 | 65 | # Draws Balls on Display Window 66 | def draw(self, x, y): 67 | pygame.draw.ellipse(display, self.color, (x - radius, y - radius, radius*2, radius*2)) 68 | if self.color == black or self.ballNum == "cue": 69 | ballNo = self.font.render(str(self.ballNum), True, white) 70 | display.blit(ballNo, (x - 5, y - 5)) 71 | else: 72 | ballNo = self.font.render(str(self.ballNum), True, black) 73 | if self.ballNum > 9: 74 | display.blit(ballNo, (x - 6, y - 5)) 75 | else: 76 | display.blit(ballNo, (x - 5, y - 5)) 77 | 78 | # Moves the Ball around the Screen 79 | def move(self): 80 | self.speed -= friction 81 | if self.speed <= 0: 82 | self.speed = 0 83 | self.x = self.x + self.speed*cos(radians(self.angle)) 84 | self.y = self.y + self.speed*sin(radians(self.angle)) 85 | 86 | if not (self.x < width - radius - margin): 87 | self.x = width - radius - margin 88 | self.angle = 180 - self.angle 89 | if not(radius + margin < self.x): 90 | self.x = radius + margin 91 | self.angle = 180 - self.angle 92 | if not (self.y < height - radius - margin): 93 | self.y = height - radius - margin 94 | self.angle = 360 - self.angle 95 | if not(radius + margin < self.y): 96 | self.y = radius + margin 97 | self.angle = 360 - self.angle 98 | 99 | # Pocket Class 100 | class Pockets: 101 | def __init__(self, x, y, color): 102 | self.r = margin/2 103 | self.x = x + self.r + 10 104 | self.y = y + self.r + 10 105 | self.color = color 106 | 107 | # Draws the Pockets on Pygame Window 108 | def draw(self): 109 | pygame.draw.ellipse(display, self.color, (self.x - self.r, self.y - self.r, self.r*2, self.r*2)) 110 | 111 | # Checks if ball has entered the Hole 112 | def checkPut(self): 113 | global balls 114 | ballsCopy = balls[:] 115 | for i in range(len(balls)): 116 | dist = ((self.x - balls[i].x)**2 + (self.y - balls[i].y)**2)**0.5 117 | if dist < self.r + radius: 118 | if balls[i] in ballsCopy: 119 | if balls[i].ballNum == 8: 120 | gameOver() 121 | else: 122 | ballsCopy.remove(balls[i]) 123 | 124 | balls = ballsCopy[:] 125 | 126 | # Cue Stick Class 127 | class CueStick: 128 | def __init__(self, x, y, length, color): 129 | self.x = x 130 | self.y = y 131 | self.length = length 132 | self.color = color 133 | self.tangent = 0 134 | 135 | # Applies force to Cue Ball 136 | def applyForce(self, cueBall, force): 137 | cueBall.angle = self.tangent 138 | cueBall.speed = force 139 | 140 | # Draws Cue Stick on Pygame Window 141 | def draw(self, cuex, cuey): 142 | self.x, self.y = pygame.mouse.get_pos() 143 | self.tangent = (degrees(atan2((cuey - self.y), (cuex - self.x)))) 144 | pygame.draw.line(display, white, (cuex + self.length*cos(radians(self.tangent)), cuey + self.length*sin(radians(self.tangent))), (cuex, cuey), 1) 145 | pygame.draw.line(display, self.color, (self.x, self.y), (cuex, cuey), 3) 146 | 147 | 148 | # Checks Collision 149 | def collision(ball1, ball2): 150 | dist = ((ball1.x - ball2.x)**2 + (ball1.y - ball2.y)**2)**0.5 151 | if dist <= radius*2: 152 | return True 153 | else: 154 | return False 155 | 156 | # Checks if Cue Ball hits any Ball 157 | def checkCueCollision(cueBall): 158 | for i in range(len(balls)): 159 | if collision(cueBall, balls[i]): 160 | if balls[i].x == cueBall.x: 161 | angleIncline = 2*90 162 | else: 163 | u1 = balls[i].speed 164 | u2 = cueBall.speed 165 | 166 | balls[i].speed = ((u1*cos(radians(balls[i].angle)))**2 + (u2*sin(radians(cueBall.angle)))**2)**0.5 167 | cueBall.speed = ((u2*cos(radians(cueBall.angle)))**2 + (u1*sin(radians(balls[i].angle)))**2)**0.5 168 | 169 | tangent = degrees((atan((balls[i].y - cueBall.y)/(balls[i].x - cueBall.x)))) + 90 170 | angle = tangent + 90 171 | 172 | balls[i].angle = (2*tangent - balls[i].angle) 173 | cueBall.angle = (2*tangent - cueBall.angle) 174 | 175 | balls[i].x += (balls[i].speed)*sin(radians(angle)) 176 | balls[i].y -= (balls[i].speed)*cos(radians(angle)) 177 | cueBall.x -= (cueBall.speed)*sin(radians(angle)) 178 | cueBall.y += (cueBall.speed)*cos(radians(angle)) 179 | 180 | 181 | # Checks Collision Between Balls 182 | def checkCollision(): 183 | for i in range(len(balls)): 184 | for j in range(len(balls) - 1, i, -1): 185 | if collision(balls[i], balls[j]): 186 | if balls[i].x == balls[j].x: 187 | angleIncline = 2*90 188 | else: 189 | u1 = balls[i].speed 190 | u2 = balls[j].speed 191 | 192 | balls[i].speed = ((u1*cos(radians(balls[i].angle)))**2 + (u2*sin(radians(balls[j].angle)))**2)**0.5 193 | balls[j].speed = ((u2*cos(radians(balls[j].angle)))**2 + (u1*sin(radians(balls[i].angle)))**2)**0.5 194 | 195 | tangent = degrees((atan((balls[i].y - balls[j].y)/(balls[i].x - balls[j].x)))) + 90 196 | angle = tangent + 90 197 | 198 | balls[i].angle = (2*tangent - balls[i].angle) 199 | balls[j].angle = (2*tangent - balls[j].angle) 200 | 201 | balls[i].x += (balls[i].speed)*sin(radians(angle)) 202 | balls[i].y -= (balls[i].speed)*cos(radians(angle)) 203 | balls[j].x -= (balls[j].speed)*sin(radians(angle)) 204 | balls[j].y += (balls[j].speed)*cos(radians(angle)) 205 | 206 | def border(): 207 | pygame.draw.rect(display, gray, (0, 0, width, 30)) 208 | pygame.draw.rect(display, gray, (0, 0, 30, height)) 209 | pygame.draw.rect(display, gray, (width - 30, 0, width, height)) 210 | pygame.draw.rect(display, gray, (0, height - 30, width, height)) 211 | 212 | def score(): 213 | font = pygame.font.SysFont("Agency FB", 30) 214 | 215 | pygame.draw.rect(display, (51, 51, 51), (0, height, width, outerHeight)) 216 | for i in range(len(balls)): 217 | balls[i].draw((i + 1)*2*(radius + 1), height + radius + 10) 218 | 219 | text = font.render("Remaining Balls: " + str(len(balls)), True, stickColor) 220 | display.blit(text, (width/2 + 50, height + radius/2)) 221 | 222 | 223 | def reset(): 224 | global balls, noBalls 225 | noBalls = 15 226 | balls = [] 227 | 228 | s = 70 229 | 230 | b1 = Ball(s, height/2 - 4*radius, 0, colors[0], 0, 1) 231 | b2 = Ball(s + 2*radius, height/2 - 3*radius, 0, colors[1], 0, 2) 232 | b3 = Ball(s, height/2 - 2*radius, 0, colors[2], 0, 3) 233 | b4 = Ball(s + 4*radius, height/2 - 2*radius, 0, colors[3], 0, 4) 234 | b5 = Ball(s + 2*radius, height/2 - 1*radius, 0, colors[4], 0, 5) 235 | b6 = Ball(s, height/2, 0, colors[5], 0, 6) 236 | b7 = Ball(s + 6*radius, height/2 - 1*radius, 0, colors[6], 0, 7) 237 | b8 = Ball(s + 4*radius, height/2, 0, colors[7], 0, 8) 238 | b9 = Ball(s + 8*radius, height/2, 0, colors[8], 0, 9) 239 | b10 = Ball(s + 6*radius, height/2 + 1*radius, 0, colors[9], 0, 10) 240 | b11 = Ball(s + 2*radius, height/2 + 1*radius, 0, colors[10], 0, 11) 241 | b12 = Ball(s, height/2 + 2*radius, 0, colors[11], 0, 12) 242 | b13 = Ball(s + 4*radius, height/2 + 2*radius, 0, colors[12], 0, 13) 243 | b14 = Ball(s + 2*radius, height/2 + 3*radius, 0, colors[13], 0, 14) 244 | b15 = Ball(s, height/2 + 4*radius, 0, colors[14], 0, 15) 245 | 246 | balls.append(b1) 247 | balls.append(b2) 248 | balls.append(b3) 249 | balls.append(b4) 250 | balls.append(b5) 251 | balls.append(b6) 252 | balls.append(b7) 253 | balls.append(b8) 254 | balls.append(b9) 255 | balls.append(b10) 256 | balls.append(b11) 257 | balls.append(b12) 258 | balls.append(b13) 259 | balls.append(b14) 260 | balls.append(b15) 261 | 262 | 263 | 264 | def gameOver(): 265 | font = pygame.font.SysFont("Agency FB", 75) 266 | if len(balls) == 0: 267 | text = font.render("You Won!", True, (133, 193, 233)) 268 | else: 269 | text = font.render("You Lost! Black in Hole!", True, (241, 148, 138)) 270 | 271 | while True: 272 | for event in pygame.event.get(): 273 | if event.type == pygame.QUIT: 274 | close() 275 | if event.type == pygame.KEYDOWN: 276 | if event.key == pygame.K_q: 277 | close() 278 | 279 | if event.key == pygame.K_r: 280 | poolTable() 281 | display.blit(text, (50, height/2)) 282 | 283 | pygame.display.update() 284 | clock.tick() 285 | 286 | def close(): 287 | pygame.quit() 288 | sys.exit() 289 | 290 | # Main Function 291 | def poolTable(): 292 | loop = True 293 | 294 | reset() 295 | 296 | noPockets = 6 297 | pockets = [] 298 | 299 | p1 = Pockets(0, 0, black) 300 | p2 = Pockets(width/2 - p1.r*2, 0, black) 301 | p3 = Pockets(width - p1.r - margin - 5, 0, black) 302 | p4 = Pockets(0, height - margin - 5 - p1.r, black) 303 | p5 = Pockets(width/2 - p1.r*2, height - margin - 5 - p1.r, black) 304 | p6 = Pockets(width - p1.r - margin - 5, height - margin - 5 - p1.r, black) 305 | 306 | pockets.append(p1) 307 | pockets.append(p2) 308 | pockets.append(p3) 309 | pockets.append(p4) 310 | pockets.append(p5) 311 | pockets.append(p6) 312 | 313 | cueBall = Ball(width/2, height/2, 0, white, 0, "cue") 314 | cueStick = CueStick(0, 0, 100, stickColor) 315 | 316 | 317 | start = 0 318 | end = 0 319 | 320 | while loop: 321 | for event in pygame.event.get(): 322 | if event.type == pygame.QUIT: 323 | close() 324 | if event.type == pygame.KEYDOWN: 325 | if event.key == pygame.K_q: 326 | close() 327 | 328 | if event.key == pygame.K_r: 329 | poolTable() 330 | 331 | if event.type == pygame.MOUSEBUTTONDOWN: 332 | start = [cueBall.x, cueBall.y] 333 | x, y = pygame.mouse.get_pos() 334 | end = [x ,y] 335 | dist = ((start[0] - end[0])**2 + (start[1] - end[1])**2)**0.5 336 | force = dist/10.0 337 | if force > 10: 338 | force = 10 339 | 340 | cueStick.applyForce(cueBall, force) 341 | 342 | 343 | display.fill(background) 344 | 345 | cueBall.draw(cueBall.x, cueBall.y) 346 | cueBall.move() 347 | 348 | if not (cueBall.speed > 0): 349 | 350 | cueStick.draw(cueBall.x, cueBall.y) 351 | 352 | for i in range(len(balls)): 353 | balls[i].draw(balls[i].x, balls[i].y) 354 | 355 | for i in range(len(balls)): 356 | balls[i].move() 357 | 358 | checkCollision() 359 | checkCueCollision(cueBall) 360 | border() 361 | 362 | for i in range(noPockets): 363 | pockets[i].draw() 364 | 365 | for i in range(noPockets): 366 | pockets[i].checkPut() 367 | 368 | if len(balls) == 1 and balls[0].ballNum == 8: 369 | gameOver() 370 | 371 | score() 372 | 373 | pygame.display.update() 374 | clock.tick(60) 375 | 376 | poolTable() 377 | -------------------------------------------------------------------------------- /Angry_Birds/Fonts/Comic_Kings.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Fonts/Comic_Kings.ttf -------------------------------------------------------------------------------- /Angry_Birds/Fonts/arfmoochikncheez.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Fonts/arfmoochikncheez.ttf -------------------------------------------------------------------------------- /Angry_Birds/Images/bird.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/bird.png -------------------------------------------------------------------------------- /Angry_Birds/Images/block1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/block1.png -------------------------------------------------------------------------------- /Angry_Birds/Images/block_destroyed1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/block_destroyed1.png -------------------------------------------------------------------------------- /Angry_Birds/Images/game_play1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/game_play1.png -------------------------------------------------------------------------------- /Angry_Birds/Images/game_play2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/game_play2.png -------------------------------------------------------------------------------- /Angry_Birds/Images/pig1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/pig1.png -------------------------------------------------------------------------------- /Angry_Birds/Images/pig3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/pig3.png -------------------------------------------------------------------------------- /Angry_Birds/Images/pig_damaged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/pig_damaged.png -------------------------------------------------------------------------------- /Angry_Birds/Images/wall_horizontal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/wall_horizontal.png -------------------------------------------------------------------------------- /Angry_Birds/Images/wall_vertical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/Images/wall_vertical.png -------------------------------------------------------------------------------- /Angry_Birds/README.md: -------------------------------------------------------------------------------- 1 | # Angry Birds 2 | 3 | Here is a Small Attempt to Recreate One of the popular Games, Angry Birds in Python using Pygame 4 | 5 | Link to 14 Hour Time Lapse of coding the game from Scratch: [Angry Birds - 14 Hour Time Lapse](https://youtu.be/6in-mdiumcA) 6 | 7 | Link to Blog: [Angry Birds in Python Using PyGame](https://jatinmandav.wordpress.com/2018/05/25/angry-birds-in-python-using-pygame/) 8 | 9 | ### Requirements: 10 | 11 | [Pygame Module](https://www.pygame.org) 12 | 13 | pip install pygame 14 | 15 | 16 | ## Usage: 17 | 18 | wget https://github.com/jatinmandav/Gaming-in-Python/tree/master/Angry_Birds 19 | 20 | From Angry_Birds/ Directory: 21 | 22 | $ python3 main.py or 23 | 24 | $ python main.py 25 | 26 | 27 | ### Game Play Screenshot: 28 | 29 |

30 | 31 | 32 |

33 | -------------------------------------------------------------------------------- /Angry_Birds/__pycache__/interface.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/__pycache__/interface.cpython-35.pyc -------------------------------------------------------------------------------- /Angry_Birds/__pycache__/maps.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/__pycache__/maps.cpython-35.pyc -------------------------------------------------------------------------------- /Angry_Birds/__pycache__/objects.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/__pycache__/objects.cpython-35.pyc -------------------------------------------------------------------------------- /Angry_Birds/__pycache__/physics_engine.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Angry_Birds/__pycache__/physics_engine.cpython-35.pyc -------------------------------------------------------------------------------- /Angry_Birds/interface.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | Game: Angry Birds 4 | File: interface.py 5 | 6 | Contents: Class Buttons and Class Labels for User Interface 7 | 8 | Requirements: Pygame, sys 9 | 10 | By: Jatin Kumar Mandav 11 | 12 | Blog: https://www.jatinmandav.wordpress.com 13 | Twitter: @jatinmandav 14 | YouTube: https://www.youtube.com/mandav 15 | 16 | ''' 17 | import pygame 18 | import sys 19 | 20 | pygame.init() 21 | display = None 22 | 23 | def init(screen): 24 | global display 25 | display = screen 26 | 27 | class Button: 28 | def __init__(self, x, y, w, h, action=None, colorNotActive=(189, 195, 199), colorActive=None): 29 | self.x = x 30 | self.y = y 31 | self.w = w 32 | self.h = h 33 | 34 | self.colorActive = colorActive 35 | self.colorNotActive = colorNotActive 36 | 37 | self.action = action 38 | 39 | self.font = None 40 | self.text = None 41 | self.text_pos = None 42 | 43 | def add_text(self, text, size=20, font="Times New Roman", text_color=(0, 0, 0)): 44 | self.font = pygame.font.Font(font, size) 45 | self.text = self.font.render(text, True, text_color) 46 | self.text_pos = self.text.get_rect() 47 | 48 | self.text_pos.center = (self.x + self.w/2, self.y + self.h/2) 49 | 50 | def draw(self): 51 | if self.isActive(): 52 | if not self.colorActive == None: 53 | pygame.draw.rect(display, self.colorActive, (self.x, self.y, self.w, self.h)) 54 | else: 55 | pygame.draw.rect(display, self.colorNotActive, (self.x, self.y, self.w, self.h)) 56 | 57 | if self.text: 58 | display.blit(self.text, self.text_pos) 59 | 60 | def isActive(self): 61 | pos = pygame.mouse.get_pos() 62 | 63 | if (self.x < pos[0] < self.x + self.w) and (self.y < pos[1] < self.y + self.h): 64 | return True 65 | else: 66 | return False 67 | 68 | class Label(Button): 69 | def draw(self): 70 | if self.text: 71 | display.blit(self.text, self.text_pos) 72 | -------------------------------------------------------------------------------- /Angry_Birds/main.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | Game: Angry Birds 4 | File: main.py 5 | 6 | Contents: The Main file to Start the Game! 7 | 8 | Requirements: Pygame, sys, random, math 9 | Supporting Modules: interface.py, physics_engine.py, maps.py, objects.py 10 | 11 | Usage: 12 | Angry_Birds/$ python3 main.py or 13 | Angry_Birds/$ python main.py 14 | 15 | By: Jatin Kumar Mandav 16 | 17 | Blog: https://www.jatinmandav.wordpress.com 18 | Twitter: @jatinmandav 19 | YouTube: https://www.youtube.com/mandav 20 | 21 | ''' 22 | import pygame 23 | import sys 24 | import random 25 | from math import * 26 | 27 | import physics_engine 28 | import objects 29 | import maps 30 | import interface 31 | 32 | pygame.init() 33 | width = 1800 34 | height = 700 35 | display = pygame.display.set_mode((width, height)) 36 | clock = pygame.time.Clock() 37 | 38 | physics_engine.init(display) 39 | objects.init(display) 40 | maps.init(display) 41 | interface.init(display) 42 | 43 | background = (51, 51, 51) 44 | 45 | def close(): 46 | pygame.quit() 47 | sys.exit() 48 | 49 | def start_game(map): 50 | map.draw_map() 51 | 52 | def GAME(): 53 | map = maps.Maps() 54 | 55 | welcome = interface.Label(700, 100, 400, 200, None, background) 56 | welcome.add_text("ANGRY BIRDS", 80, "Fonts/arfmoochikncheez.ttf", (236, 240, 241)) 57 | 58 | start = interface.Button(500, 400, 300, 100, start_game, (244, 208, 63), (247, 220, 111)) 59 | start.add_text("START GAME", 60, "Fonts/arfmoochikncheez.ttf", background) 60 | 61 | exit = interface.Button(1000, 400, 300, 100, close, (241, 148, 138), (245, 183, 177)) 62 | exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", background) 63 | 64 | mandav = interface.Button(width - 300, height - 80, 300, 100, None, background) 65 | mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", (41, 41, 41)) 66 | 67 | while True: 68 | for event in pygame.event.get(): 69 | if event.type == pygame.QUIT: 70 | close() 71 | if event.type == pygame.KEYDOWN: 72 | if event.key == pygame.K_q: 73 | close() 74 | 75 | if event.type == pygame.MOUSEBUTTONDOWN: 76 | if exit.isActive(): 77 | exit.action() 78 | if start.isActive(): 79 | start_game(map) 80 | 81 | display.fill(background) 82 | 83 | start.draw() 84 | exit.draw() 85 | welcome.draw() 86 | mandav.draw() 87 | 88 | pygame.display.update() 89 | clock.tick(60) 90 | 91 | GAME() 92 | -------------------------------------------------------------------------------- /Angry_Birds/maps.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | Game: Angry Birds 4 | File: maps.py 5 | 6 | Contents: Class MAPS, that puts everything in action! 7 | 8 | Requirements: Pygame, sys 9 | Supporting Modules: physics_engine, interface, objects 10 | 11 | By: Jatin Kumar Mandav 12 | 13 | Blog: https://www.jatinmandav.wordpress.com 14 | Twitter: @jatinmandav 15 | YouTube: https://www.youtube.com/mandav 16 | 17 | ''' 18 | import pygame 19 | import sys 20 | 21 | import physics_engine 22 | import objects 23 | import interface 24 | 25 | pygame.init() 26 | width = None 27 | height = None 28 | display = None 29 | clock = pygame.time.Clock() 30 | 31 | ground = 50 32 | 33 | d_velocity = 2.0 34 | 35 | def init(screen): 36 | global width, height, display 37 | display = screen 38 | (width, height) = display.get_rect().size 39 | height -= ground 40 | interface.init(display) 41 | 42 | def all_rest(pigs, birds, blocks): 43 | threshold = 0.15 44 | for pig in pigs: 45 | if pig.velocity.magnitude >= threshold: 46 | return False 47 | 48 | for bird in birds: 49 | if bird.velocity.magnitude >= threshold: 50 | return False 51 | 52 | for block in blocks: 53 | if block.velocity.magnitude >= threshold: 54 | return False 55 | 56 | return True 57 | 58 | def close(): 59 | pygame.quit() 60 | sys.exit() 61 | 62 | class Maps: 63 | def __init__(self): 64 | self.level = 1 65 | self.max_level = 15 66 | self.color = {'background': (51, 51, 51)} 67 | self.score = 0 68 | 69 | def wait_level(self): 70 | time = 0 71 | while time < 3: 72 | for event in pygame.event.get(): 73 | if event.type == pygame.QUIT: 74 | close() 75 | if event.type == pygame.KEYDOWN: 76 | if event.key == pygame.K_q: 77 | close() 78 | time += 1 79 | clock.tick(1) 80 | 81 | return 82 | 83 | def check_win(self, pigs, birds): 84 | if pigs == []: 85 | print("WON!") 86 | return True 87 | if (not pigs == []) and birds == []: 88 | print("LOST!") 89 | return False 90 | 91 | def pause(self): 92 | pause_text = interface.Label(700, 200, 400, 200, None, self.color['background']) 93 | pause_text.add_text("GAME PAUSED", 70, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 94 | 95 | replay = interface.Button(350, 500, 300, 100, self.draw_map, (244, 208, 63), (247, 220, 111)) 96 | replay.add_text("RESTART", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 97 | 98 | resume = interface.Button(750, 500, 300, 100, None, (88, 214, 141), (171, 235, 198)) 99 | resume.add_text("RESUME", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 100 | 101 | exit = interface.Button(1150, 500, 300, 100, close, (241, 148, 138), (245, 183, 177)) 102 | exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 103 | 104 | mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background']) 105 | mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 )) 106 | 107 | while True: 108 | for event in pygame.event.get(): 109 | if event.type == pygame.QUIT: 110 | close() 111 | if event.type == pygame.KEYDOWN: 112 | if event.key == pygame.K_q: 113 | close() 114 | if event.key == pygame.K_p: 115 | return 116 | if event.key == pygame.K_ESCAPE: 117 | return 118 | 119 | if event.type == pygame.MOUSEBUTTONDOWN: 120 | if replay.isActive(): 121 | replay.action() 122 | if resume.isActive(): 123 | return 124 | if exit.isActive(): 125 | exit.action() 126 | 127 | replay.draw() 128 | resume.draw() 129 | exit.draw() 130 | pause_text.draw() 131 | mandav.draw() 132 | 133 | pygame.display.update() 134 | clock.tick(60) 135 | 136 | def draw_map(self): 137 | birds = [] 138 | pigs = [] 139 | blocks = [] 140 | walls = [] 141 | self.score = 0 142 | 143 | if self.level == 1: 144 | for i in range(3): 145 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 146 | birds.append(new_bird) 147 | 148 | pigs.append(physics_engine.Pig(1100, height - 40, 20)) 149 | pigs.append(physics_engine.Pig(1500, height - 40, 20)) 150 | 151 | blocks.append(physics_engine.Block(1300, height - 60, 60)) 152 | 153 | elif self.level == 2: 154 | for i in range(3): 155 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 156 | birds.append(new_bird) 157 | 158 | pigs.append(physics_engine.Pig(1000, height - 40, 20)) 159 | pigs.append(physics_engine.Pig(1400, height - 40, 20)) 160 | 161 | blocks.append(physics_engine.Block(1200, height - 60, 60)) 162 | blocks.append(physics_engine.Block(1200, height - 2*35, 60)) 163 | blocks.append(physics_engine.Block(1500, height - 60, 60)) 164 | 165 | elif self.level == 3: 166 | for i in range(3): 167 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 168 | birds.append(new_bird) 169 | 170 | pigs.append(physics_engine.Pig(1200, height - 60, 30)) 171 | pigs.append(physics_engine.Pig(1300, height - 60, 30)) 172 | 173 | blocks.append(physics_engine.Block(1000, height - 100, 100)) 174 | blocks.append(physics_engine.Block(1000, height - 2*60, 100)) 175 | blocks.append(physics_engine.Block(1500, height - 100, 100)) 176 | blocks.append(physics_engine.Block(1500, height - 2*60, 100)) 177 | 178 | elif self.level == 4: 179 | for i in range(3): 180 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 181 | birds.append(new_bird) 182 | 183 | pigs.append(physics_engine.Pig(1200, 500 - 60, 30)) 184 | pigs.append(physics_engine.Pig(1300, height - 60, 30)) 185 | 186 | walls.append(objects.Slab(1000, 450, 500, 20)) 187 | 188 | blocks.append(physics_engine.Block(1100, height - 100, 100)) 189 | 190 | elif self.level == 5: 191 | for i in range(3): 192 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 193 | birds.append(new_bird) 194 | 195 | pigs.append(physics_engine.Pig(1300, 500 - 60, 25)) 196 | pigs.append(physics_engine.Pig(1300, height - 60, 25)) 197 | 198 | walls.append(objects.Slab(500, 400, 100, height - 400)) 199 | walls.append(objects.Slab(1000, 450, 500, 30)) 200 | 201 | blocks.append(physics_engine.Block(1150, 500 - 100, 100)) 202 | blocks.append(physics_engine.Block(1100, height - 100, 100)) 203 | 204 | elif self.level == 6: 205 | for i in range(3): 206 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 207 | birds.append(new_bird) 208 | 209 | pigs.append(physics_engine.Pig(1300, 500 - 60, 25)) 210 | pigs.append(physics_engine.Pig(1300, height - 60, 25)) 211 | 212 | walls.append(objects.Slab(1000, 0, 30, 450)) 213 | walls.append(objects.Slab(1000, 450, 500, 30)) 214 | 215 | blocks.append(physics_engine.Block(1150, 500 - 100, 100)) 216 | blocks.append(physics_engine.Block(1100, height - 100, 100)) 217 | 218 | elif self.level == 7: 219 | for i in range(4): 220 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 221 | birds.append(new_bird) 222 | 223 | pigs.append(physics_engine.Pig(1100, 500 - 60, 25)) 224 | pigs.append(physics_engine.Pig(1300, 500 - 60, 25)) 225 | pigs.append(physics_engine.Pig(1200, height - 60, 25)) 226 | 227 | walls.append(objects.Slab(1200, 250, 30, 200)) 228 | walls.append(objects.Slab(1000, 450, 500, 30)) 229 | 230 | elif self.level == 8: 231 | for i in range(3): 232 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 233 | birds.append(new_bird) 234 | 235 | pigs.append(physics_engine.Pig(1100, height - 60, 25)) 236 | pigs.append(physics_engine.Pig(1200, height - 60, 25)) 237 | 238 | walls.append(objects.Slab(700, 250, 30, height - 250)) 239 | 240 | elif self.level == 9: 241 | for i in range(3): 242 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 243 | birds.append(new_bird) 244 | 245 | pigs.append(physics_engine.Pig(1100, height - 60, 25)) 246 | pigs.append(physics_engine.Pig(1450, height - 60, 25)) 247 | 248 | 249 | blocks.append(physics_engine.Block(1250, height - 100, 100)) 250 | blocks.append(physics_engine.Block(1250, height - 2*60, 100)) 251 | 252 | walls.append(objects.Slab(700, 400, 30, height - 400)) 253 | 254 | elif self.level == 10: 255 | for i in range(3): 256 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 257 | birds.append(new_bird) 258 | 259 | pigs.append(physics_engine.Pig(1100, height - 60, 25)) 260 | pigs.append(physics_engine.Pig(1450, height - 60, 25)) 261 | 262 | blocks.append(physics_engine.Block(1250, height - 100, 100)) 263 | blocks.append(physics_engine.Block(1250, height - 2*60, 100)) 264 | blocks.append(physics_engine.Block(900, height - 100, 100)) 265 | 266 | walls.append(objects.Slab(900, 400, 500, 30)) 267 | 268 | elif self.level == 11: 269 | for i in range(3): 270 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 271 | birds.append(new_bird) 272 | 273 | pigs.append(physics_engine.Pig(1100, height - 60, 25)) 274 | pigs.append(physics_engine.Pig(1450, height - 60, 25)) 275 | 276 | blocks.append(physics_engine.Block(1250, height - 100, 100)) 277 | blocks.append(physics_engine.Block(1250, height - 2*60, 100)) 278 | 279 | walls.append(objects.Slab(900, 400, 500, 30)) 280 | walls.append(objects.Slab(900, 400, 30, height - 400)) 281 | 282 | elif self.level == 12: 283 | for i in range(3): 284 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 285 | birds.append(new_bird) 286 | 287 | pigs.append(physics_engine.Pig(1100, height - 60, 25)) 288 | pigs.append(physics_engine.Pig(1450, height - 60, 25)) 289 | 290 | walls.append(objects.Slab(900, 400, 500, 30)) 291 | walls.append(objects.Slab(1200, 500, 30, height - 500)) 292 | 293 | elif self.level == 13: 294 | for i in range(4): 295 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 296 | birds.append(new_bird) 297 | 298 | pigs.append(physics_engine.Pig(1100, height - 60, 25)) 299 | pigs.append(physics_engine.Pig(1200, 400 - 60, 25)) 300 | pigs.append(physics_engine.Pig(1450, height - 60, 25)) 301 | 302 | blocks.append(physics_engine.Block(900, height - 100, 100)) 303 | blocks.append(physics_engine.Block(900, height - 2*60, 100)) 304 | 305 | walls.append(objects.Slab(900, 400, 500, 40)) 306 | walls.append(objects.Slab(1200, 500, 30, height - 500)) 307 | 308 | elif self.level == 14: 309 | for i in range(4): 310 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 311 | birds.append(new_bird) 312 | 313 | pigs.append(physics_engine.Pig(1100, height - 60, 25)) 314 | pigs.append(physics_engine.Pig(1100, 400 - 60, 25)) 315 | pigs.append(physics_engine.Pig(1450, height - 60, 25)) 316 | 317 | blocks.append(physics_engine.Block(900, height - 100, 100)) 318 | 319 | blocks.append(physics_engine.Block(1300, 400 - 100, 100)) 320 | 321 | walls.append(objects.Slab(900, 400, 500, 40)) 322 | walls.append(objects.Slab(900, 0, 30, 400)) 323 | 324 | elif self.level == 15: 325 | for i in range(5): 326 | new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD") 327 | birds.append(new_bird) 328 | 329 | pigs.append(physics_engine.Pig(900, height - 60, 25)) 330 | pigs.append(physics_engine.Pig(width - 400, 400 - 60, 25)) 331 | pigs.append(physics_engine.Pig(1700, height - 60, 25)) 332 | 333 | 334 | walls.append(objects.Slab(800, 400, 30, height - 400)) 335 | walls.append(objects.Slab(1000, 500, 30, height - 500)) 336 | 337 | walls.append(objects.Slab(width - 500, 400, 500, 40)) 338 | walls.append(objects.Slab(width - 500, 150, 60, 400 - 150)) 339 | 340 | 341 | self.start_level(birds, pigs, blocks, walls) 342 | 343 | def replay_level(self): 344 | self.level -= 1 345 | self.draw_map() 346 | 347 | def start_again(self): 348 | self.level = 1 349 | self.draw_map() 350 | 351 | def level_cleared(self): 352 | self.level += 1 353 | 354 | level_cleared_text = interface.Label(700, 100, 400, 200, None, self.color['background']) 355 | if self.level <= self.max_level: 356 | level_cleared_text.add_text("LEVEL " + str(self.level - 1) + " CLEARED!", 80, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 357 | else: 358 | level_cleared_text.add_text("ALL LEVEL CLEARED!", 80, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 359 | 360 | score_text = interface.Label(750, 300, 300, 100, None, self.color['background']) 361 | score_text.add_text("SCORE: " + str(self.score), 55, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 362 | 363 | replay = interface.Button(350, 500, 300, 100, self.replay_level, (244, 208, 63), (247, 220, 111)) 364 | replay.add_text("PLAY AGAIN", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 365 | 366 | if self.level <= self.max_level: 367 | next = interface.Button(750, 500, 300, 100, self.draw_map, (88, 214, 141), (171, 235, 198)) 368 | next.add_text("CONTINUE", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 369 | else: 370 | next = interface.Button(750, 500, 300, 100, self.start_again, (88, 214, 141), (171, 235, 198)) 371 | next.add_text("START AGAIN", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 372 | 373 | exit = interface.Button(1150, 500, 300, 100, close, (241, 148, 138), (245, 183, 177)) 374 | exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 375 | 376 | mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background']) 377 | mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 )) 378 | 379 | while True: 380 | for event in pygame.event.get(): 381 | if event.type == pygame.QUIT: 382 | close() 383 | if event.type == pygame.KEYDOWN: 384 | if event.key == pygame.K_q: 385 | close() 386 | 387 | if event.type == pygame.MOUSEBUTTONDOWN: 388 | if replay.isActive(): 389 | replay.action() 390 | if next.isActive(): 391 | next.action() 392 | if exit.isActive(): 393 | exit.action() 394 | 395 | replay.draw() 396 | next.draw() 397 | exit.draw() 398 | level_cleared_text.draw() 399 | score_text.draw() 400 | mandav.draw() 401 | 402 | pygame.display.update() 403 | clock.tick(60) 404 | 405 | def level_failed(self): 406 | level_failed_text = interface.Label(700, 100, 400, 200, None, self.color['background']) 407 | level_failed_text.add_text("LEVEL FAILED!", 80, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 408 | 409 | score_text = interface.Label(750, 300, 300, 100, None, self.color['background']) 410 | score_text.add_text("SCORE: " + str(self.score), 55, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 411 | 412 | replay = interface.Button(500, 500, 300, 100, self.draw_map, (244, 208, 63), (247, 220, 111)) 413 | replay.add_text("TRY AGAIN", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 414 | 415 | exit = interface.Button(1000, 500, 300, 100, close, (241, 148, 138), (245, 183, 177)) 416 | exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", self.color['background']) 417 | 418 | mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background']) 419 | mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 )) 420 | 421 | while True: 422 | for event in pygame.event.get(): 423 | if event.type == pygame.QUIT: 424 | close() 425 | if event.type == pygame.KEYDOWN: 426 | if event.key == pygame.K_q: 427 | close() 428 | 429 | if event.type == pygame.MOUSEBUTTONDOWN: 430 | if replay.isActive(): 431 | replay.action() 432 | if exit.isActive(): 433 | exit.action() 434 | 435 | replay.draw() 436 | exit.draw() 437 | level_failed_text.draw() 438 | score_text.draw() 439 | mandav.draw() 440 | 441 | pygame.display.update() 442 | clock.tick(60) 443 | 444 | def start_level(self, birds, pigs, blocks, walls): 445 | loop = True 446 | 447 | slingshot = physics_engine.Slingshot(200, height - 200, 30, 200) 448 | 449 | birds[0].load(slingshot) 450 | 451 | mouse_click = False 452 | flag = 1 453 | 454 | pigs_to_remove = [] 455 | blocks_to_remove = [] 456 | 457 | score_text = interface.Label(50, 10, 100, 50, None, self.color['background']) 458 | score_text.add_text("SCORE: " + str(self.score), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 459 | 460 | birds_remaining = interface.Label(120, 50, 100, 50, None, self.color['background']) 461 | birds_remaining.add_text("BIRDS REMAINING: " + str(len(birds)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 462 | 463 | pigs_remaining = interface.Label(110, 90, 100, 50, None, self.color['background']) 464 | pigs_remaining.add_text("PIGS REMAINING: " + str(len(pigs)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 465 | 466 | mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background']) 467 | mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 )) 468 | 469 | while loop: 470 | for event in pygame.event.get(): 471 | if event.type == pygame.QUIT: 472 | close() 473 | if event.type == pygame.KEYDOWN: 474 | if event.key == pygame.K_q: 475 | close() 476 | if event.key == pygame.K_r: 477 | self.draw_map() 478 | if event.key == pygame.K_p: 479 | self.pause() 480 | if event.key == pygame.K_ESCAPE: 481 | self.pause() 482 | 483 | if event.type == pygame.MOUSEBUTTONDOWN: 484 | if birds[0].mouse_selected(): 485 | mouse_click = True 486 | if event.type == pygame.MOUSEBUTTONUP: 487 | mouse_click = False 488 | if birds[0].mouse_selected(): 489 | flag = 0 490 | 491 | if (not birds[0].loaded) and all_rest(pigs, birds, blocks): 492 | print("LOADED!") 493 | birds.pop(0) 494 | if self.check_win(pigs, birds) == 1: 495 | self.score += len(birds)*100 496 | self.level_cleared() 497 | elif self.check_win(pigs,birds) == 0: 498 | self.level_failed() 499 | 500 | if not birds == []: 501 | birds[0].load(slingshot) 502 | flag = 1 503 | 504 | if mouse_click: 505 | birds[0].reposition(slingshot, mouse_click) 506 | 507 | if not flag: 508 | birds[0].unload() 509 | 510 | #display.fill(self.color['background']) 511 | color = self.color['background'] 512 | for i in range(3): 513 | color = (color[0] + 5, color[1] + 5, color[2] + 5) 514 | pygame.draw.rect(display, color, (0, i*300, width, 300)) 515 | 516 | pygame.draw.rect(display, (77, 86, 86), (0, height, width, 50)) 517 | 518 | 519 | slingshot.draw(birds[0]) 520 | 521 | for i in range(len(pigs)): 522 | for j in range(len(blocks)): 523 | pig_v, block_v = pigs[i].velocity.magnitude, blocks[j].velocity.magnitude 524 | pigs[i], blocks[j], result_block_pig = physics_engine.collision_handler(pigs[i], blocks[j], "BALL_N_BLOCK") 525 | pig_v1, block_v1 = pigs[i].velocity.magnitude, blocks[j].velocity.magnitude 526 | 527 | if result_block_pig: 528 | if abs(pig_v - pig_v1) > d_velocity: 529 | blocks_to_remove.append(blocks[j]) 530 | blocks[j].destroy() 531 | if abs(block_v - block_v1) > d_velocity: 532 | pigs_to_remove.append(pigs[i]) 533 | pigs[i].dead() 534 | 535 | for i in range(len(birds)): 536 | if not (birds[i].loaded or birds[i].velocity.magnitude == 0): 537 | for j in range(len(blocks)): 538 | birds_v, block_v = birds[i].velocity.magnitude, blocks[j].velocity.magnitude 539 | birds[i], blocks[j], result_bird_block = physics_engine.collision_handler(birds[i], blocks[j], "BALL_N_BLOCK") 540 | birds_v1, block_v1 = birds[i].velocity.magnitude, blocks[j].velocity.magnitude 541 | 542 | if result_bird_block: 543 | if abs(birds_v - birds_v1) > d_velocity: 544 | if not blocks[j] in blocks_to_remove: 545 | blocks_to_remove.append(blocks[j]) 546 | blocks[j].destroy() 547 | 548 | for i in range(len(pigs)): 549 | pigs[i].move() 550 | for j in range(i+1, len(pigs)): 551 | pig1_v, pig2_v = pigs[i].velocity.magnitude, pigs[j].velocity.magnitude 552 | pigs[i], pigs[j], result = physics_engine.collision_handler(pigs[i], pigs[j], "BALL") 553 | pig1_v1, pig2_v1 = pigs[i].velocity.magnitude, pigs[j].velocity.magnitude 554 | result = True 555 | if result: 556 | if abs(pig1_v - pig1_v1) > d_velocity: 557 | if not pigs[j] in pigs_to_remove: 558 | pigs_to_remove.append(pigs[j]) 559 | pigs[j].dead() 560 | if abs(pig2_v - pig2_v1) > d_velocity: 561 | if not pigs[i] in pigs_to_remove: 562 | pigs_to_remove.append(pigs[i]) 563 | pigs[i].dead() 564 | 565 | for wall in walls: 566 | pigs[i] = wall.collision_manager(pigs[i]) 567 | 568 | pigs[i].draw() 569 | 570 | for i in range(len(birds)): 571 | if (not birds[i].loaded) and birds[i].velocity.magnitude: 572 | birds[0].move() 573 | for j in range(len(pigs)): 574 | bird_v, pig_v = birds[i].velocity.magnitude, pigs[j].velocity.magnitude 575 | birds[i], pigs[j], result_bird_pig = physics_engine.collision_handler(birds[i], pigs[j], "BALL") 576 | bird_v1, pig_v1 = birds[i].velocity.magnitude, pigs[j].velocity.magnitude 577 | result = True 578 | if result_bird_pig: 579 | if abs(bird_v - bird_v1) > d_velocity: 580 | if not pigs[j] in pigs_to_remove: 581 | pigs_to_remove.append(pigs[j]) 582 | pigs[j].dead() 583 | 584 | if birds[i].loaded: 585 | birds[i].project_path() 586 | 587 | for wall in walls: 588 | birds[i] = wall.collision_manager(birds[i]) 589 | 590 | birds[i].draw() 591 | 592 | for i in range(len(blocks)): 593 | for j in range(i + 1, len(blocks)): 594 | block1_v, block2_v = blocks[i].velocity.magnitude, blocks[j].velocity.magnitude 595 | blocks[i], blocks[j], result_block = physics_engine.block_collision_handler(blocks[i], blocks[j]) 596 | block1_v1, block2_v1 = blocks[i].velocity.magnitude, blocks[j].velocity.magnitude 597 | 598 | if result_block: 599 | if abs(block1_v - block1_v1) > d_velocity: 600 | if not blocks[j] in blocks_to_remove: 601 | blocks_to_remove.append(blocks[j]) 602 | blocks[j].destroy() 603 | if abs(block2_v - block2_v1) > d_velocity: 604 | if not blocks[i] in blocks_to_remove: 605 | blocks_to_remove.append(blocks[i]) 606 | blocks[i].destroy() 607 | 608 | blocks[i].move() 609 | 610 | for wall in walls: 611 | blocks[i] = wall.collision_manager(blocks[i], "BLOCK") 612 | 613 | blocks[i].draw() 614 | 615 | for wall in walls: 616 | wall.draw() 617 | 618 | score_text.add_text("SCORE: " + str(self.score), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 619 | score_text.draw() 620 | 621 | birds_remaining.add_text("BIRDS REMAINING: " + str(len(birds)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 622 | birds_remaining.draw() 623 | 624 | pigs_remaining.add_text("PIGS REMAINING: " + str(len(pigs)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241)) 625 | pigs_remaining.draw() 626 | 627 | mandav.draw() 628 | 629 | pygame.display.update() 630 | 631 | if all_rest(pigs, birds, blocks): 632 | for pig in pigs_to_remove: 633 | if pig in pigs: 634 | pigs.remove(pig) 635 | self.score += 100 636 | 637 | for block in blocks_to_remove: 638 | if block in blocks: 639 | blocks.remove(block) 640 | self.score += 50 641 | 642 | pigs_to_remove = [] 643 | blocks_to_remove = [] 644 | 645 | clock.tick(60) 646 | -------------------------------------------------------------------------------- /Angry_Birds/objects.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | Game: Angry Birds 4 | File: objects.py 5 | 6 | Contents: Class SLAB or WALL 7 | 8 | Requirements: Pygame, sys, math 9 | Supporting Modules: physics_engine 10 | 11 | By: Jatin Kumar Mandav 12 | 13 | Blog: https://www.jatinmandav.wordpress.com 14 | Twitter: @jatinmandav 15 | YouTube: https://www.youtube.com/mandav 16 | 17 | ''' 18 | import pygame 19 | import sys 20 | from math import * 21 | 22 | import physics_engine 23 | 24 | pygame.init() 25 | display = None 26 | width = None 27 | height = None 28 | clock = pygame.time.Clock() 29 | ground = 50 30 | 31 | def init(screen): 32 | global width, height, display 33 | display = screen 34 | (width, height) = display.get_rect().size 35 | height -= ground 36 | 37 | class Slab: 38 | def __init__(self, x, y, w, h, color=(255, 255, 255)): 39 | self.x = x 40 | self.y = y 41 | self.w = w 42 | self.h = h 43 | 44 | if self.w > self.h: 45 | self.image = pygame.image.load("Images/wall_horizontal.png") 46 | else: 47 | self.image = pygame.image.load("Images/wall_vertical.png") 48 | 49 | self.image = pygame.transform.scale(self.image, (self.w, self.h)) 50 | 51 | self.color = color 52 | 53 | def draw(self): 54 | display.blit(self.image, (self.x, self.y)) 55 | 56 | def collision_manager(self, ball, type="BALL"): 57 | if type == "BALL": 58 | if (ball.y + ball.r > self.y) and (ball.y < self.y + self.h): 59 | if (ball.x < self.x + self.w) and (ball.x + ball.r > self.x + self.w): 60 | ball.x = 2*(self.x + self.w) - ball.x 61 | ball.velocity.angle = - ball.velocity.angle 62 | ball.velocity.magnitude *= physics_engine.elasticity 63 | elif ball.x + ball.r > self.x and (ball.x < self.x): 64 | ball.x = 2*(self.x - ball.r) - ball.x 65 | ball.velocity.angle = - ball.velocity.angle 66 | ball.velocity.magnitude *= physics_engine.elasticity 67 | if (ball.x + ball.r > self.x) and (ball.x < self.x + self.w): 68 | if ball.y + ball.r > self.y and ball.y < self.y: 69 | ball.y = 2*(self.y - ball.r) - ball.y 70 | ball.velocity.angle = pi - ball.velocity.angle 71 | ball.velocity.magnitude *= physics_engine.elasticity 72 | elif (ball.y < self.y + self.h) and (ball.y + ball.r > self.y + self.h): 73 | ball.y = 2*(self.y + self.h) - ball.y 74 | ball.velocity.angle = pi - ball.velocity.angle 75 | ball.velocity.magnitude *= physics_engine.elasticity 76 | 77 | return ball 78 | else: 79 | block = ball 80 | if (block.y + block.h > self.y) and (block.y < self.y + self.h): 81 | if (block.x < self.x + self.w) and (block.x + block.w > self.x + self.w): 82 | block.x = 2*(self.x + self.w) - block.x 83 | block.velocity.angle = - block.velocity.angle 84 | block.rotateAngle = - block.velocity.angle 85 | block.velocity.magnitude *= physics_engine.elasticity 86 | elif block.x + block.w > self.x and (block.x < self.x): 87 | block.x = 2*(self.x - block.w) - block.x 88 | block.velocity.angle = - block.velocity.angle 89 | block.rotateAngle = - block.velocity.angle 90 | block.velocity.magnitude *= physics_engine.elasticity 91 | if (block.x + block.w > self.x) and (block.x < self.x + self.w): 92 | if block.y + block.h > self.y and block.y < self.y: 93 | block.y = 2*(self.y - block.h) - block.y 94 | block.velocity.angle = pi - block.velocity.angle 95 | block.rotateAngle = pi - block.velocity.angle 96 | block.velocity.magnitude *= physics_engine.elasticity 97 | elif (block.y < self.y + self.h) and (block.y + block.h > self.y + self.h): 98 | block.y = 2*(self.y + self.h) - block.y 99 | block.velocity.angle = pi - block.velocity.angle 100 | block.rotateAngle = pi - block.velocity.angle 101 | block.velocity.magnitude *= physics_engine.elasticity 102 | 103 | return block 104 | -------------------------------------------------------------------------------- /Angry_Birds/physics_engine.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | Game: Angry Birds 4 | File: physics_engine.py 5 | 6 | Contents: Class Vector 7 | Class PIG 8 | Class BIRD 9 | Class BLOCK 10 | Class SLINGSHOT 11 | func collision_handler, block_collision_handler 12 | 13 | Requirements: Pygame, sys, math, random 14 | 15 | By: Jatin Kumar Mandav 16 | 17 | Blog: https://www.jatinmandav.wordpress.com 18 | Twitter: @jatinmandav 19 | YouTube: https://www.youtube.com/mandav 20 | 21 | ''' 22 | import pygame 23 | import sys 24 | from math import * 25 | import random 26 | 27 | pygame.init() 28 | width = None 29 | height = None 30 | display = None 31 | ground = 50 32 | clock = pygame.time.Clock() 33 | 34 | def init(screen): 35 | global width, height, display 36 | display = screen 37 | (width, height) = display.get_rect().size 38 | height -= ground 39 | 40 | class Vector: 41 | def __init__(self, magnitude=0, angle=radians(0)): 42 | self.magnitude = magnitude 43 | self.angle = angle 44 | 45 | def add_vectors(vector1, vector2): 46 | x = sin(vector1.angle)*vector1.magnitude + sin(vector2.angle)*vector2.magnitude 47 | y = cos(vector1.angle)*vector1.magnitude + cos(vector2.angle)*vector2.magnitude 48 | 49 | new_angle = 0.5*pi - atan2(y, x) 50 | new_magnitude = hypot(x, y) 51 | 52 | new_vector = Vector(new_magnitude, new_angle) 53 | return new_vector 54 | 55 | gravity = Vector(0.2, pi) 56 | inverse_friction = 0.99 57 | elasticity = 0.8 58 | block_elasticity = 0.7 59 | 60 | class Pig: 61 | def __init__(self, x, y, r, v=None, type="PIG", loaded = False, color=(255, 255, 255)): 62 | self.x = x 63 | self.y = y 64 | self.r = r 65 | if v == None: 66 | self.velocity = Vector() 67 | else: 68 | self.velocity = v 69 | 70 | self.pig1_image = pygame.image.load("Images/pig1.png") 71 | self.pig2_image = pygame.image.load("Images/pig3.png") 72 | 73 | self.pig_dead = pygame.image.load("Images/pig_damaged.png") 74 | 75 | self.bird_image = pygame.image.load("Images/bird.png") 76 | 77 | if type == "PIG": 78 | self.image = random.choice([self.pig1_image, self.pig2_image]) 79 | else: 80 | self.image = self.bird_image 81 | 82 | self.type = type 83 | self.color = color 84 | self.loaded = loaded 85 | self.path = [] 86 | self.count = 0 87 | self.animate_count = 0 88 | self.isDead = False 89 | 90 | def draw(self): 91 | self.animate_count += 1 92 | 93 | if self.type == "BIRD" and not self.loaded: 94 | for point in self.path: 95 | pygame.draw.ellipse(display, self.color, (point[0], point[1], 3, 3), 1) 96 | 97 | if (self.type == "PIG") and (not self.animate_count%20) and (not self.isDead): 98 | self.image = random.choice([self.pig1_image, self.pig2_image]) 99 | 100 | display.blit(self.image, (self.x - self.r, self.y - self.r)) 101 | 102 | 103 | def dead(self): 104 | self.isDead = True 105 | self.image = self.pig_dead 106 | 107 | def move(self): 108 | self.velocity = add_vectors(self.velocity, gravity) 109 | 110 | self.x += self.velocity.magnitude*sin(self.velocity.angle) 111 | self.y -= self.velocity.magnitude*cos(self.velocity.angle) 112 | 113 | self.velocity.magnitude *= inverse_friction 114 | 115 | if self.x > width - self.r: 116 | self.x = 2*(width - self.r) - self.x 117 | self.velocity.angle *= -1 118 | self.velocity.magnitude *= elasticity 119 | elif self.x < self.r: 120 | self.x = 2*self.r - self.x 121 | self.velocity.angle *= -1 122 | self.velocity.magnitude *= elasticity 123 | 124 | if self.y > height - self.r: 125 | self.y = 2*(height - self.r) - self.y 126 | self.velocity.angle = pi - self.velocity.angle 127 | self.velocity.magnitude *= elasticity 128 | elif self.y < self.r: 129 | self.y = 2*self.r - self.y 130 | self.velocity.angle = pi - self.velocity.angle 131 | self.velocity.magnitude *= elasticity 132 | 133 | self.count += 1 134 | if self.count%1 == 0: 135 | self.path.append((self.x, self.y)) 136 | 137 | class Bird(Pig): 138 | def load(self, slingshot): 139 | self.x = slingshot.x 140 | self.y = slingshot.y 141 | self.loaded = True 142 | 143 | def mouse_selected(self): 144 | pos = pygame.mouse.get_pos() 145 | dx = pos[0] - self.x 146 | dy = pos[1] - self.y 147 | dist = hypot(dy, dx) 148 | if dist < self.r: 149 | return True 150 | 151 | return False 152 | 153 | def reposition(self, slingshot, mouse_click): 154 | pos = pygame.mouse.get_pos() 155 | if self.mouse_selected(): 156 | self.x = pos[0] 157 | self.y = pos[1] 158 | 159 | dx = slingshot.x - self.x 160 | dy = slingshot.y - self.y 161 | self.velocity.magnitude = int(hypot(dx, dy)/2) 162 | if self.velocity.magnitude > 80: 163 | self.velocity.magnitude = 80 164 | self.velocity.angle = pi/2 + atan2(dy, dx) 165 | 166 | def unload(self): 167 | self.loaded = False 168 | 169 | def project_path(self): 170 | if self.loaded: 171 | path = [] 172 | ball = Pig(self.x, self.y, self.r, self.velocity, self.type) 173 | for i in range(30): 174 | ball.move() 175 | if i%5 == 0: 176 | path.append((ball.x, ball.y)) 177 | 178 | for point in path: 179 | pygame.draw.ellipse(display, self.color, (point[0], point[1], 2, 2)) 180 | 181 | 182 | class Block: 183 | def __init__(self, x, y, r, v=None, color=( 120, 40, 31 ), colorBoundary = ( 28, 40, 51 )): 184 | self.r = 50 185 | self.w = 100 186 | self.h = 100 187 | 188 | self.x = x 189 | self.y = y 190 | 191 | self.block_image = pygame.image.load("Images/block1.png") 192 | self.block_destroyed_image = pygame.image.load("Images/block_destroyed1.png") 193 | 194 | self.image = self.block_image 195 | 196 | if v == None: 197 | self.velocity = Vector() 198 | else: 199 | self.velocity = v 200 | 201 | self.color = color 202 | self.colorDestroyed = ( 100, 30, 22 ) 203 | self.colorBoundary = colorBoundary 204 | self.rotateAngle = radians(0) 205 | self.anchor = (self.r/2, self.r/2) 206 | 207 | self.isDestroyed = False 208 | 209 | def rotate(self, coord, angle, anchor=(0, 0)): 210 | corr = 0 211 | return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)), 212 | (coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr))) 213 | 214 | def translate(self, coord): 215 | return [coord[0] + self.x, coord[1] + self.y] 216 | 217 | def draw(self): 218 | pygame.transform.rotate(self.image, self.rotateAngle) 219 | display.blit(self.image, (self.x - self.w/2, self.y)) 220 | 221 | def destroy(self): 222 | self.isDestroyed = True 223 | self.image = self.block_destroyed_image 224 | 225 | def move(self): 226 | self.velocity = add_vectors(self.velocity, gravity) 227 | 228 | self.x += self.velocity.magnitude*sin(self.velocity.angle) 229 | self.y -= self.velocity.magnitude*cos(self.velocity.angle) 230 | 231 | self.velocity.magnitude *= inverse_friction 232 | 233 | if self.x > width - self.w: 234 | self.x = 2*(width - self.w) - self.x 235 | self.velocity.angle *= -1 236 | self.rotateAngle = - self.velocity.angle 237 | self.velocity.magnitude *= block_elasticity 238 | elif self.x < self.w: 239 | self.x = 2*self.w - self.x 240 | self.velocity.angle *= -1 241 | self.rotateAngle = - self.velocity.angle 242 | self.velocity.magnitude *= block_elasticity 243 | 244 | if self.y > height - self.h: 245 | self.y = 2*(height - self.h) - self.y 246 | self.velocity.angle = pi - self.velocity.angle 247 | self.rotateAngle = pi - self.velocity.angle 248 | self.velocity.magnitude *= block_elasticity 249 | elif self.y < self.h: 250 | self.y = 2*self.h - self.y 251 | self.velocity.angle = pi - self.velocity.angle 252 | self.rotateAngle = pi - self.velocity.angle 253 | self.velocity.magnitude *= block_elasticity 254 | 255 | 256 | class Slingshot: 257 | def __init__(self, x, y, w, h, color=( 66, 73, 73 )): 258 | self.x = x 259 | self.y = y 260 | self.w = w 261 | self.h = h 262 | self.color = color 263 | 264 | def rotate(self, coord, angle, anchor=(0, 0)): 265 | corr = 0 266 | return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)), 267 | (coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr))) 268 | 269 | def translate(self, coord): 270 | return [coord[0] + self.x, coord[1] + self.y] 271 | 272 | def draw(self, loaded=None): 273 | pygame.draw.rect(display, self.color, (self.x, self.y + self.h*1/3, self.w, self.h*2/3)) 274 | 275 | if (not loaded == None) and loaded.loaded: 276 | pygame.draw.line(display, ( 100, 30, 22 ), (self.x - self.w/4 + self.w/4, self.y + self.h/6), (loaded.x, loaded.y + loaded.r/2), 10) 277 | pygame.draw.line(display, ( 100, 30, 22 ), (self.x + self.w, self.y + self.h/6), (loaded.x + loaded.r, loaded.y + loaded.r/2), 10) 278 | 279 | pygame.draw.rect(display, self.color, (self.x - self.w/4, self.y, self.w/2, self.h/3), 5) 280 | pygame.draw.rect(display, self.color, (self.x + self.w - self.w/4, self.y, self.w/2, self.h/3), 5) 281 | 282 | def collision_handler(b_1, b_2, type): 283 | collision = False 284 | if type == "BALL": 285 | dx = b_1.x - b_2.x 286 | dy = b_1.y - b_2.y 287 | 288 | dist = hypot(dx, dy) 289 | if dist < b_1.r + b_2.r: 290 | tangent = atan2(dy, dx) 291 | angle = 0.5*pi + tangent 292 | 293 | angle1 = 2*tangent - b_1.velocity.angle 294 | angle2 = 2*tangent - b_2.velocity.angle 295 | 296 | magnitude1 = b_2.velocity.magnitude 297 | magnitude2 = b_1.velocity.magnitude 298 | 299 | b_1.velocity = Vector(magnitude1, angle1) 300 | b_2.velocity = Vector(magnitude2, angle2) 301 | 302 | b_1.velocity.magnitude *= elasticity 303 | b_2.velocity.magnitude *= elasticity 304 | 305 | overlap = 0.5*(b_1.r + b_2.r - dist + 1) 306 | b_1.x += sin(angle)*overlap 307 | b_1.y -= cos(angle)*overlap 308 | b_2.x -= sin(angle)*overlap 309 | b_2.y += cos(angle)*overlap 310 | collision = True 311 | #print(collision) 312 | 313 | #print(collision) 314 | return b_1, b_2, collision 315 | elif type == "BALL_N_BLOCK": 316 | dx = b_1.x - b_2.x 317 | dy = b_1.y - b_2.y 318 | 319 | dist = hypot(dx, dy) 320 | if dist < b_1.r + b_2.w: 321 | tangent = atan2(dy, dx) 322 | angle = 0.5*pi + tangent 323 | 324 | angle1 = 2*tangent - b_1.velocity.angle 325 | angle2 = 2*tangent - b_2.velocity.angle 326 | 327 | magnitude1 = b_2.velocity.magnitude 328 | magnitude2 = b_1.velocity.magnitude 329 | 330 | b_1.velocity = Vector(magnitude1, angle1) 331 | b_2.velocity = Vector(magnitude2, angle2) 332 | 333 | b_1.velocity.magnitude *= elasticity 334 | b_2.velocity.magnitude *= block_elasticity 335 | 336 | overlap = 0.5*(b_1.r + b_2.w - dist + 1) 337 | b_1.x += sin(angle)*overlap 338 | b_1.y -= cos(angle)*overlap 339 | b_2.x -= sin(angle)*overlap 340 | b_2.y += cos(angle)*overlap 341 | collision = True 342 | 343 | return b_1, b_2, collision 344 | 345 | def block_collision_handler(block, block2): 346 | collision = False 347 | if (block.y + block.h > block2.y) and (block.y < block2.y + block2.h): 348 | if (block.x < block2.x + block2.w) and (block.x + block.w > block2.x + block2.w): 349 | block.x = 2*(block2.x + block2.w) - block.x 350 | block.velocity.angle = - block.velocity.angle 351 | block.rotateAngle = - block.velocity.angle 352 | block.velocity.magnitude *= block_elasticity 353 | 354 | block2.velocity.angle = - block2.velocity.angle 355 | block2.rotateAngle = - block2.velocity.angle 356 | block2.velocity.magnitude *= block_elasticity 357 | collision = True 358 | 359 | elif block.x + block.w > block2.x and (block.x < block2.x): 360 | block.x = 2*(block2.x - block.w) - block.x 361 | block.velocity.angle = - block.velocity.angle 362 | block.rotateAngle = - block.velocity.angle 363 | block.velocity.magnitude *= block_elasticity 364 | 365 | block2.velocity.angle = - block2.velocity.angle 366 | block2.rotateAngle = - block2.velocity.angle 367 | block2.velocity.magnitude *= block_elasticity 368 | collision = True 369 | 370 | if (block.x + block.w > block2.x) and (block.x < block2.x + block2.w): 371 | if block.y + block.h > block2.y and block.y < block2.y: 372 | block.y = 2*(block2.y - block.h) - block.y 373 | block.velocity.angle = pi - block.velocity.angle 374 | block.rotateAngle = pi - block.velocity.angle 375 | block.velocity.magnitude *= block_elasticity 376 | 377 | block2.velocity.angle = pi - block2.velocity.angle 378 | block2.rotateAngle = pi - block2.velocity.angle 379 | block2.velocity.magnitude *= block_elasticity 380 | collision = True 381 | 382 | elif (block.y < block2.y + block2.h) and (block.y + block.h > block2.y + block2.h): 383 | block.y = 2*(block2.y + block2.h) - block.y 384 | block.velocity.angle = pi - block.velocity.angle 385 | block.rotateAngle = pi - block.velocity.angle 386 | block.velocity.magnitude *= block_elasticity 387 | 388 | block2.velocity.angle = pi - block2.velocity.angle 389 | block2.rotateAngle = pi - block2.velocity.angle 390 | block2.velocity.magnitude *= block_elasticity 391 | collision = True 392 | 393 | return block, block2, collision 394 | -------------------------------------------------------------------------------- /BalloonShooter/BalloonShooter.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Balloon Shooter 4 | # Language - Python 5 | # Modules - pygame, sys, random, math 6 | # 7 | # Controls - Mouse 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 | from math import * 22 | 23 | pygame.init() 24 | 25 | width = 500 26 | height = 500 27 | 28 | display = pygame.display.set_mode((width, height)) 29 | pygame.display.set_caption("Balloon Shooter") 30 | clock = pygame.time.Clock() 31 | 32 | margin = 100 33 | lowerBound = 100 34 | 35 | score = 0 36 | 37 | # Colors 38 | white = (230, 230, 230) 39 | lightBlue = (174, 214, 241) 40 | red = (231, 76, 60) 41 | lightGreen = (25, 111, 61) 42 | darkGray = (40, 55, 71) 43 | darkBlue = (21, 67, 96) 44 | green = (35, 155, 86) 45 | yellow = (244, 208, 63) 46 | blue = (46, 134, 193) 47 | purple = (155, 89, 182) 48 | orange = (243, 156, 18) 49 | 50 | font = pygame.font.SysFont("Snap ITC", 25) 51 | 52 | # Balloon Class 53 | class Balloon: 54 | def __init__(self, speed): 55 | self.a = random.randint(30, 40) 56 | self.b = self.a + random.randint(0, 10) 57 | self.x = random.randrange(margin, width - self.a - margin) 58 | self.y = height - lowerBound 59 | self.angle = 90 60 | self.speed = -speed 61 | self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1] 62 | self.length = random.randint(50, 100) 63 | self.color = random.choice([red, green, purple, orange, yellow, blue]) 64 | 65 | # Move balloon around the Screen 66 | def move(self): 67 | direct = random.choice(self.probPool) 68 | 69 | if direct == -1: 70 | self.angle += -10 71 | elif direct == 0: 72 | self.angle += 0 73 | else: 74 | self.angle += 10 75 | 76 | self.y += self.speed*sin(radians(self.angle)) 77 | self.x += self.speed*cos(radians(self.angle)) 78 | 79 | if (self.x + self.a > width) or (self.x < 0): 80 | if self.y > height/5: 81 | self.x -= self.speed*cos(radians(self.angle)) 82 | else: 83 | self.reset() 84 | if self.y + self.b < 0 or self.y > height + 30: 85 | self.reset() 86 | 87 | # Show/Draw the balloon 88 | def show(self): 89 | pygame.draw.line(display, darkBlue, (self.x + self.a/2, self.y + self.b), (self.x + self.a/2, self.y + self.b + self.length)) 90 | pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b)) 91 | pygame.draw.ellipse(display, self.color, (self.x + self.a/2 - 5, self.y + self.b - 3, 10, 10)) 92 | 93 | # Check if Balloon is bursted 94 | def burst(self): 95 | global score 96 | pos = pygame.mouse.get_pos() 97 | 98 | if onBalloon(self.x, self.y, self.a, self.b, pos): 99 | score += 1 100 | self.reset() 101 | 102 | # Reset the Balloon 103 | def reset(self): 104 | self.a = random.randint(30, 40) 105 | self.b = self.a + random.randint(0, 10) 106 | self.x = random.randrange(margin, width - self.a - margin) 107 | self.y = height - lowerBound 108 | self.angle = 90 109 | self.speed -= 0.002 110 | self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1] 111 | self.length = random.randint(50, 100) 112 | self.color = random.choice([red, green, purple, orange, yellow, blue]) 113 | 114 | balloons = [] 115 | noBalloon = 10 116 | for i in range(noBalloon): 117 | obj = Balloon(random.choice([1, 1, 2, 2, 2, 2, 3, 3, 3, 4])) 118 | balloons.append(obj) 119 | 120 | def onBalloon(x, y, a, b, pos): 121 | if (x < pos[0] < x + a) and (y < pos[1] < y + b): 122 | return True 123 | else: 124 | return False 125 | 126 | # show the location of Mouse 127 | def pointer(): 128 | pos = pygame.mouse.get_pos() 129 | r = 25 130 | l = 20 131 | color = lightGreen 132 | for i in range(noBalloon): 133 | if onBalloon(balloons[i].x, balloons[i].y, balloons[i].a, balloons[i].b, pos): 134 | color = red 135 | pygame.draw.ellipse(display, color, (pos[0] - r/2, pos[1] - r/2, r, r), 4) 136 | pygame.draw.line(display, color, (pos[0], pos[1] - l/2), (pos[0], pos[1] - l), 4) 137 | pygame.draw.line(display, color, (pos[0] + l/2, pos[1]), (pos[0] + l, pos[1]), 4) 138 | pygame.draw.line(display, color, (pos[0], pos[1] + l/2), (pos[0], pos[1] + l), 4) 139 | pygame.draw.line(display, color, (pos[0] - l/2, pos[1]), (pos[0] - l, pos[1]), 4) 140 | 141 | def lowerPlatform(): 142 | pygame.draw.rect(display, darkGray, (0, height - lowerBound, width, lowerBound)) 143 | 144 | def showScore(): 145 | scoreText = font.render("Balloons Bursted : " + str(score), True, white) 146 | display.blit(scoreText, (150, height - lowerBound + 50)) 147 | 148 | 149 | def close(): 150 | pygame.quit() 151 | sys.exit() 152 | 153 | def game(): 154 | global score 155 | loop = True 156 | 157 | while loop: 158 | for event in pygame.event.get(): 159 | if event.type == pygame.QUIT: 160 | close() 161 | if event.type == pygame.KEYDOWN: 162 | if event.key == pygame.K_q: 163 | close() 164 | if event.key == pygame.K_r: 165 | score = 0 166 | game() 167 | 168 | if event.type == pygame.MOUSEBUTTONDOWN: 169 | for i in range(noBalloon): 170 | balloons[i].burst() 171 | 172 | display.fill(lightBlue) 173 | 174 | for i in range(noBalloon): 175 | balloons[i].show() 176 | 177 | pointer() 178 | 179 | for i in range(noBalloon): 180 | balloons[i].move() 181 | 182 | 183 | lowerPlatform() 184 | showScore() 185 | pygame.display.update() 186 | clock.tick(60) 187 | 188 | 189 | game() 190 | -------------------------------------------------------------------------------- /Battles/Battles - Game.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Battles/Battles - Game.rar -------------------------------------------------------------------------------- /Battles/Code/Battles.py: -------------------------------------------------------------------------------- 1 | """ 2 | ~~~~~~~~~~~~~~ 3 | Battles.py 4 | ~~~~~~~~~~~~~~ 5 | 6 | By - JATIN KUMAR MANDAV 7 | 8 | BATTLES is inspired by mobile strategy video game Clash of Clans 9 | which is developed and published by Finnish game developer Supercell. 10 | 11 | This game is developed using PYGAME library of PYTHON 2.7 12 | 13 | Lines - 127 14 | 15 | """ 16 | 17 | import pygame 18 | import sys 19 | from math import * 20 | from GUI import * 21 | from Maps import * 22 | from Structures import * 23 | 24 | pygame.init() 25 | 26 | width = 1260 27 | height = 720 28 | 29 | margin = 25 30 | 31 | groundW = width - 150 - 2*margin 32 | groundH = height - 2*margin 33 | 34 | display = pygame.display.set_mode((width, height), pygame.FULLSCREEN) 35 | clock = pygame.time.Clock() 36 | 37 | initGUI(display, (width, height)) 38 | initStruct(display, (groundW, groundH), margin) 39 | initMaps(display, (width, height), margin, 100) 40 | 41 | 42 | white = (255, 255, 255) 43 | black = (0, 0, 0) 44 | yellow = (241, 196, 15) 45 | red = (203, 67, 53) 46 | green = (30, 132, 73) 47 | blue = (36, 113, 163) 48 | gray = (113, 125, 126) 49 | 50 | activeButton = 0 51 | 52 | 53 | def drawBorder(): 54 | pygame.draw.rect(display, black, (margin, margin, groundW, groundH), 2) 55 | 56 | def createMaps(arg=0): 57 | maps = Maps() 58 | maps.createMap() 59 | 60 | def adventure(arg=0): 61 | maps = Maps() 62 | maps.openMap([0, "adventure"]) 63 | 64 | def customMap(arg=0): 65 | maps = Maps() 66 | maps.openMap([0, "custom"]) 67 | 68 | def close(arg=0): 69 | pygame.quit() 70 | sys.exit() 71 | 72 | def game(): 73 | loop = True 74 | font = pygame.font.SysFont("Agency FB", 50) 75 | font.set_bold(True) 76 | mandav = font.render("MANDAV", True, (71, 71, 71)) 77 | 78 | font2 = pygame.font.SysFont("Agency FB", 200) 79 | font2.set_bold(True) 80 | title = font2.render("BATTLES", True, (200, 200, 200)) 81 | titlePos = title.get_rect() 82 | titlePos.center = [width/2, height/2 - 200] 83 | 84 | tank = pygame.image.load("Images/Tank.png") 85 | tankPos = tank.get_rect() 86 | tankPos.center = [width/2, height/2 + 40] 87 | 88 | heli = pygame.image.load("Images/Helicopter.png") 89 | heliPos = heli.get_rect() 90 | heliPos.center = [width/2 - 70, height/2 - 100] 91 | 92 | quit = Button(width - 300 - 10, height/2 + 200, 300, 100, blue, blue, close) 93 | quit.addText("QUIT", (0, 0), 30, "Showcard Gothic", (150, 150, 150)) 94 | 95 | create = Button(width - 600 - 20, height/2 + 200, 300, 100, blue, blue, createMaps) 96 | create.addText("CREATE MAPS", (0, 0), 30, "Showcard Gothic", (150, 150, 150)) 97 | 98 | adven = Button(width - 900 - 30, height/2 + 200, 300, 100, blue, blue, adventure) 99 | adven.addText("ADVENTURE", (0, 0), 30, "Showcard Gothic", (150, 150, 150)) 100 | 101 | custom = Button(width - 1200 - 40, height/2 + 200, 300, 100, blue, blue, customMap) 102 | custom.addText("CUSTOM BASES", (0, 0), 30, "Showcard Gothic", (150, 150, 150)) 103 | 104 | while loop: 105 | for event in pygame.event.get(): 106 | if event.type == pygame.QUIT: 107 | close() 108 | if event.type == pygame.KEYDOWN: 109 | if event.key == pygame.K_q: 110 | close() 111 | if event.type == pygame.MOUSEBUTTONDOWN: 112 | quit.select() 113 | create.select() 114 | adven.select() 115 | custom.select() 116 | 117 | display.fill((51,51,51)) 118 | 119 | display.blit(title, titlePos) 120 | display.blit(heli, heliPos) 121 | display.blit(tank, tankPos) 122 | 123 | display.blit(mandav, (width - 200, height - 60)) 124 | 125 | quit.draw() 126 | create.draw() 127 | adven.draw() 128 | custom.draw() 129 | 130 | pygame.display.update() 131 | clock.tick(60) 132 | 133 | game() 134 | -------------------------------------------------------------------------------- /Battles/Code/Data/customMaps: -------------------------------------------------------------------------------- 1 | 364.0,341.0,4/747.0,360.0,4/541.0,237.0,4/542.0,443.0,4/|648.0,349.0,3/441.0,345.0,3/|473.0,281.0,4/615.0,280.0,4/608.0,411.0,4/463.0,413.0,4/|557,354,5/|431,135/281,218/269,358/327,483/433,533/589,543/723,503/851,379/806,250/678,144/ 2 | 391.0,229.0,4/493.0,229.0,4/579.0,230.0,4/667.0,231.0,4/|429.0,340.0,3/608.0,343.0,3/|382.0,433.0,4/498.0,431.0,4/589.0,433.0,4/673.0,433.0,4/|529,343,5/|293,325/771,331/297,148/465,129/602,129/738,143/287,499/423,507/607,517/767,529/ 3 | -------------------------------------------------------------------------------- /Battles/Code/Data/mapReached: -------------------------------------------------------------------------------- 1 | 8 -------------------------------------------------------------------------------- /Battles/Code/Data/maps: -------------------------------------------------------------------------------- 1 | 465.0,271.0,1/|||565,352,1/|637,425/ 2 | 451.0,270.0,1/613.0,452.0,1/|||525,361,1/|659,309/448,421/ 3 | 386.0,327.0,1/517.0,453.0,1/||605.0,297.0,1/|513,351,1/|676,387/463,229/394,441/ 4 | 434.0,265.0,1/633.0,454.0,1/||647.0,261.0,1/|538,346,1/|413,361/505,476/770,355/531,165/ 5 | 453.0,253.0,1/657.0,255.0,1/||553.0,451.0,1/|553,339,2/|710,389/437,381/ 6 | 437.0,453.0,1/635.0,458.0,1/||533.0,531.0,1/|541,359,2/|429,306/542,241/655,336/ 7 | 671.0,447.0,1/445.0,448.0,1/||450.0,255.0,1/654.0,260.0,1/|543,348,2/|833,335/397,339/566,164/558,472/ 8 | 429.0,456.0,1/649.0,257.0,1/||619.0,410.0,1/453.0,273.0,1/|536,343,2/|708,327/569,448/353,350/557,206/803,516/285,200/259,497/884,198/ 9 | 429.0,281.0,2/656.0,276.0,1/600.0,432.0,2/|429.0,434.0,1/|544.0,236.0,1/524.0,493.0,1/|519,363,3/|755,377/715,464/569,529/335,483/309,327/365,153/947,162/ 10 | 455.0,273.0,2/613.0,452.0,2/699.0,335.0,1/|617.0,305.0,1/|423.0,376.0,1/556.0,225.0,2/|551,365,3/| 11 | 421.0,261.0,2/603.0,245.0,2/627.0,375.0,2/|437.0,429.0,1/|345.0,375.0,2/534.0,489.0,2/337.0,535.0,1/|520,337,3/|749,542/809,364/284,163/209,310/195,489/531,581/ 12 | 458.0,281.0,2/612.0,256.0,2/623.0,380.0,2/|475.0,383.0,1/|378.0,345.0,2/392.0,452.0,2/546.0,467.0,2/|545,348,3/|710,479/807,361/731,205/488,165/328,215/277,429/499,569/892,543/ 13 | 434.0,245.0,3/645.0,362.0,3/701.0,261.0,2/432.0,466.0,1/|601.0,241.0,1/|422.0,371.0,2/523.0,431.0,2/529.0,179.0,3/|536,329,4/|775,397/703,510/552,525/295,479/307,312/338,168/709,156/838,271/ 14 | 465.0,234.0,3/615.0,221.0,3/675.0,336.0,3/559.0,432.0,2/|457.0,367.0,2/|379.0,279.0,2/693.0,436.0,2/391.0,432.0,3/|545,337,4/|879,478/649,526/461,525/267,466/268,345/290,161/753,187/822,346/ 15 | 434.0,257.0,3/663.0,261.0,3/663.0,433.0,3/435.0,419.0,2/|698.0,343.0,2/|549.0,211.0,2/544.0,527.0,3/347.0,326.0,3/|536,354,4/|775,514/875,359/759,207/293,135/131,168/167,412/188,547/367,534/ 16 | 446.0,253.0,3/630.0,243.0,3/629.0,427.0,3/451.0,422.0,3/|691.0,333.0,2/|536.0,170.0,3/543.0,489.0,3/377.0,331.0,3/|547,343,4/|707,509/859,357/769,219/401,133/207,275/245,431/347,515/534,578/675,127/ 17 | 478.0,255.0,4/695.0,367.0,4/467.0,437.0,3/666.0,230.0,3/|574.0,475.0,3/579.0,259.0,1/|672.0,473.0,3/380.0,329.0,3/473.0,533.0,4/569.0,180.0,2/|560,363,5/|790,545/891,371/807,195/329,136/257,415/347,537/218,254/ 18 | 405.0,213.0,4/662.0,513.0,4/719.0,217.0,4/399.0,495.0,2/|559.0,221.0,2/651.0,356.0,3/|357.0,359.0,3/529.0,519.0,4/753.0,407.0,4/775.0,271.0,4/|529,361,5/|824,542/919,398/921,209/265,160/215,287/211,441/282,550/953,515/ 19 | 228.0,195.0,4/878.0,183.0,4/877.0,539.0,4/230.0,519.0,4/|672.0,362.0,3/449.0,358.0,3/|546.0,231.0,4/545.0,483.0,4/355.0,354.0,3/785.0,369.0,3/|548,366,5/|705,192/411,181/195,325/389,523/684,526/919,362/ 20 | 535.0,193.0,4/537.0,503.0,4/762.0,349.0,4/298.0,347.0,4/|637.0,355.0,3/413.0,349.0,3/|231.0,184.0,4/842.0,186.0,4/847.0,542.0,4/217.0,531.0,4/|532,357,5/|681,179/402,197/193,282/175,383/357,541/697,559/899,455/896,290/ 21 | 543.0,221.0,4/324.0,346.0,4/749.0,348.0,4/553.0,499.0,4/|431.0,366.0,3/661.0,367.0,3/|363.0,227.0,4/356.0,465.0,4/721.0,477.0,4/705.0,218.0,4/|542,372,5/|887,543/925,417/907,261/815,146/545,133/269,131/191,237/169,397/245,541/561,583/ 22 | 351.0,430.0,4/461.0,493.0,4/628.0,492.0,4/719.0,403.0,4/|480.0,377.0,3/587.0,375.0,3/|362.0,275.0,4/423.0,177.0,4/551.0,159.0,4/679.0,247.0,4/|540,295,5/|735,149/817,249/858,383/835,490/753,579/301,135/203,229/207,349/202,471/311,545/ 23 | 364.0,341.0,4/747.0,360.0,4/541.0,237.0,4/542.0,443.0,4/|648.0,349.0,3/441.0,345.0,3/|473.0,281.0,4/615.0,280.0,4/608.0,411.0,4/463.0,413.0,4/|557,354,5/|431,135/281,218/269,358/327,483/433,533/589,543/723,503/851,379/806,250/678,144/ 24 | 391.0,229.0,4/493.0,229.0,4/579.0,230.0,4/667.0,231.0,4/|429.0,340.0,3/608.0,343.0,3/|382.0,433.0,4/498.0,431.0,4/589.0,433.0,4/673.0,433.0,4/|529,343,5/|293,325/771,331/297,148/465,129/602,129/738,143/287,499/423,507/607,517/767,529/ 25 | -------------------------------------------------------------------------------- /Battles/Code/GUI.py: -------------------------------------------------------------------------------- 1 | """ 2 | ~~~~~~ 3 | GUI.py 4 | ~~~~~~ 5 | 6 | By - JATIN KUMAR MANDAV 7 | 8 | 9 | A small library for Button widget for the game 10 | This is completely coded using PYGAME library of PYTHON 2.7 11 | 12 | Lines - 136 13 | 14 | """ 15 | 16 | # Imports 17 | import pygame 18 | import sys 19 | 20 | # Initialize Pygame 21 | pygame.init() 22 | 23 | width = 0 24 | height = 0 25 | display = None 26 | 27 | # Initialize display, width, margin, and height of pygame window 28 | def initGUI(screen, size): 29 | global display, width, height 30 | display = screen 31 | width = size[0] 32 | height = size[1] 33 | 34 | # Button Class 35 | class Button: 36 | def __init__(self, x, y, w, h, color, activeColor, action=None, border=None, borderWidth = 2): 37 | self.x = x 38 | self.y = y 39 | self.w = w 40 | self.h = h 41 | self.color = color 42 | self.activeColor = activeColor 43 | self.arg = 0 44 | self.borderWidth = borderWidth 45 | self.action = action 46 | 47 | if border: 48 | self.border = border 49 | else: 50 | self.border = color 51 | 52 | self.image = None 53 | self.imagePos = None 54 | 55 | self.text = None 56 | self.textPos = None 57 | self.align = "center" 58 | self.font = "Times New Roman" 59 | self.fontSize = 11 60 | self.fontColor = (0, 0, 0) 61 | 62 | self.image = None 63 | self.imagePos = None 64 | 65 | def select(self): 66 | if isActive(self.x, self.y, self.w, self.h) and self.action != None: 67 | self.action(self.arg) 68 | 69 | def draw(self, index=0): 70 | if index == self.arg: 71 | color = self.activeColor 72 | else: 73 | color = self.color 74 | pygame.draw.rect(display, color, (self.x, self.y, self.w, self.h)) 75 | pygame.draw.rect(display, self.border, (self.x, self.y, self.w, self.h), self.borderWidth) 76 | if (self.text): 77 | display.blit(self.text, self.textPos) 78 | if not (self.image == None): 79 | display.blit(self.image, self.imagePos) 80 | 81 | def addImage(self, image, pos): 82 | size = image.get_size() 83 | self.image = image 84 | self.imagePos = (self.x + pos[0] - size[0]/2, self.y + pos[1] - size[1]/2) 85 | 86 | def addText(self, text, pos, size=25, font="Times New Roman", color=(0, 0 ,0), align="center"): 87 | self.font = font 88 | self.fontSize = size 89 | self.fontColor = color 90 | self.align = align 91 | font = pygame.font.SysFont(font, size) 92 | 93 | self.text = font.render(text, True, color) 94 | self.textPos = getRect(self.text) 95 | if align == "center": 96 | self.textPos.center = (self.x + self.w/2 + pos[0], self.y + self.h/2 + pos[1]) 97 | else: 98 | self.textPos = (self.x + 10 + pos[0], self.y + pos[1]) 99 | 100 | def updateText(self, text, pos): 101 | font = pygame.font.SysFont(self.font, self.fontSize) 102 | self.text = font.render(text, True, self.fontColor) 103 | self.textPos = getRect(self.text) 104 | if self.align == "center": 105 | self.textPos.center = (self.x + self.w/2 + pos[0], self.y + self.h/2 + pos[1]) 106 | else: 107 | self.textPos = (self.x + 10 + pos[0], self.y + pos[1]) 108 | 109 | 110 | def getRect(font): 111 | return font.get_rect() 112 | 113 | # Check if mouse is over the button 114 | def isActive(x, y, w, h): 115 | pos = pygame.mouse.get_pos() 116 | if (x < pos[0] < x + w) and (y < pos[1] < y + h): 117 | return True 118 | return False 119 | 120 | 121 | if __name__ == "__main__": 122 | screen = pygame.display.set_mode((200, 100)) 123 | initGUI(screen, (200, 100)) 124 | newButton = Button(50, 25, 100, 50, (255, 255, 255)) 125 | newButton.addText("Text", (0, 0)) 126 | 127 | while True: 128 | for event in pygame.event.get(): 129 | if event.type == pygame.QUIT: 130 | pygame.quit() 131 | sys.exit() 132 | 133 | display.fill(0) 134 | newButton.draw() 135 | pygame.display.update() 136 | -------------------------------------------------------------------------------- /Battles/Code/Images/1-stars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Battles/Code/Images/1-stars.png -------------------------------------------------------------------------------- /Battles/Code/Images/2-stars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Battles/Code/Images/2-stars.png -------------------------------------------------------------------------------- /Battles/Code/Images/3-stars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Battles/Code/Images/3-stars.png -------------------------------------------------------------------------------- /Battles/Code/Images/Helicopter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Battles/Code/Images/Helicopter.png -------------------------------------------------------------------------------- /Battles/Code/Images/Tank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jatinmandav/Gaming-in-Python/f51b9e1e0f8a2d5ac03285de7b37a66340216657/Battles/Code/Images/Tank.png -------------------------------------------------------------------------------- /Battles/Code/Structures.py: -------------------------------------------------------------------------------- 1 | """ 2 | ~~~~~~~~~~~~~ 3 | Structures.py 4 | ~~~~~~~~~~~~~ 5 | 6 | By - JATIN KUMAR MANDAV 7 | 8 | This is a library for all structures used in Game 9 | Structures: 1. CANNONS 10 | 2. MORTARS 11 | 3. TOWERS 12 | 4. HEADQUARTERS 13 | 5. RESOURCE BUILDINGS 14 | 15 | This is completely coded using PYGAME library of PYTHON 2.7 16 | 17 | Lines - 717 18 | 19 | """ 20 | 21 | # Imports 22 | import pygame 23 | import sys 24 | from math import * 25 | import random 26 | 27 | pygame.init() 28 | 29 | display = None 30 | width = 0 31 | height = 0 32 | margin = 0 33 | 34 | clock = pygame.time.Clock() 35 | 36 | def initStruct(screen, size, border=0): 37 | global display, width, height, margin 38 | display = screen 39 | width = size[0] 40 | height = size[1] 41 | margin = border 42 | 43 | def getCoordPoly(x, y, r, n, rotate=0, anchor=(0, 0)): 44 | coords = [] 45 | for i in range(n): 46 | coords.append([x - anchor[0] + r*cos(2*pi*i/n + rotate), y - anchor[1] + r*sin(2*pi*i/n + rotate)]) 47 | 48 | return coords[:] 49 | 50 | class Shots: 51 | def __init__(self, x, y, r1, speed, angle, color=(0, 0, 0), type="circle", r2=0): 52 | self.x = x 53 | self.y = y 54 | self.r1 = r1 55 | if r2 == 0: 56 | self.r2 = r1 57 | else: 58 | self.r2 = r2 59 | 60 | self.type = type 61 | 62 | self.speed = speed 63 | self.angle = angle 64 | self.color = color 65 | 66 | def draw(self): 67 | if self.type == "circle": 68 | pygame.draw.ellipse(display, self.color, (self.x - self.r1, self.y - self.r2, self.r1*2, self.r2*2)) 69 | elif self.type == "line": 70 | x2 = self.x + self.r1*cos(self.angle) 71 | y2 = self.y + self.r1*sin(self.angle) 72 | pygame.draw.line(display, self.color, (self.x, self.y), (x2, y2), 2) 73 | 74 | def move(self): 75 | self.x += self.speed*cos(self.angle) 76 | self.y += self.speed*sin(self.angle) 77 | 78 | class Cannon: 79 | def __init__(self, x, y, w, h, size, range, baseR, level, radius, speed, colorBarrel=(0, 0, 0), colorHead=(0, 0, 0), colorBase=(255, 255, 255)): 80 | self.x = x 81 | self.y = y 82 | 83 | self.xOld = self.x 84 | self.yOld = self.y 85 | self.angle = 0 86 | 87 | self.r = size 88 | self.w = w 89 | self.h = h 90 | self.type = "CANNON" 91 | self.attackType = "GROUND" 92 | self.baseR = baseR 93 | 94 | self.radius = radius 95 | self.speed = speed 96 | 97 | self.range = range 98 | self.coord = getCoordPoly(self.x, self.y, self.r, 4) 99 | self.rectCoord = [] 100 | 101 | self.shots = [] 102 | 103 | self.recoil = 3 104 | self.rebound = 0.1 105 | 106 | self.level = level 107 | 108 | if self.level == 1: 109 | self.colorBarrel = (23, 32, 42) 110 | self.colorHead = (39, 55, 70) 111 | self.colorBase = (123, 125, 125) 112 | self.hitPoint = 420 113 | self.damage = 8 114 | elif self.level == 2: 115 | self.colorBarrel = (11, 83, 69) 116 | self.colorHead = (30, 132, 73) 117 | self.colorBase = (23, 32, 42) 118 | self.hitPoint = 640 119 | self.damage = 33 120 | self.headCover = (69, 179, 157) 121 | elif self.level == 3: 122 | self.colorBarrel = (21, 67, 96) 123 | self.colorHead = (40, 116, 166) 124 | self.colorBase = (144, 148, 151) 125 | self.hitPoint = 830 126 | self.damage = 32 127 | self.rectCoord2 = [] 128 | elif self.level == 4: 129 | self.colorBarrel = (23, 32, 42) 130 | self.colorHead = (46, 64, 83) 131 | self.colorBase = (144, 148, 151) 132 | self.hitPoint = 1200 133 | self.damage = 54 134 | self.rectCoord2 = [] 135 | self.headCover = (133, 146, 158) 136 | 137 | self.health = self.hitPoint 138 | 139 | self.nearestPos = pygame.mouse.get_pos() 140 | 141 | self.font = pygame.font.SysFont("Agency FB", 20) 142 | 143 | def updateHealth(self, troops): 144 | error = self.baseR 145 | for troop in troops: 146 | if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error): 147 | for shot in troop.shots: 148 | if troop.isHit((shot.x, shot.y)): 149 | self.health -= troop.damage 150 | 151 | def isHit(self, coord): 152 | error = self.baseR 153 | dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5 154 | if dist < error: 155 | return True 156 | return False 157 | 158 | def removeHit(self): 159 | tempList = self.shots[:] 160 | for shot in self.shots: 161 | if self.isHit((shot.x, shot.y)): 162 | tempList.remove(shot) 163 | 164 | self.shots = tempList[:] 165 | 166 | def rotate(self, coord, angle, anchor=(0, 0)): 167 | corr = 270 168 | return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)), 169 | (coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr))) 170 | 171 | def translate(self, coord): 172 | return [coord[0] + self.x, coord[1] + self.y] 173 | 174 | def shoot(self): 175 | error = 0.05 176 | 177 | if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)): 178 | self.x += self.rebound*cos(self.angle) 179 | self.y += self.rebound*sin(self.angle) 180 | else: 181 | pos = self.nearestPos 182 | #pos = pygame.mouse.get_pos() 183 | dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5 184 | self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld) 185 | 186 | if dist < self.range/2: 187 | shot = Shots((self.rectCoord[1][0] + self.rectCoord[2][0])/2, (self.rectCoord[1][1] + self.rectCoord[2][1])/2, self.radius, self.speed, self.angle, (0, 0, 0), "line") 188 | self.shots.append(shot) 189 | if self.level >= 3: 190 | shot = Shots((self.rectCoord2[1][0] + self.rectCoord2[2][0])/2, (self.rectCoord2[1][1] + self.rectCoord2[2][1])/2, self.radius, self.speed, self.angle, (0, 0, 0), "line") 191 | self.shots.append(shot) 192 | self.x -= self.recoil*cos(self.angle) 193 | self.y -= self.recoil*sin(self.angle) 194 | 195 | tempList = self.shots[:] 196 | 197 | for shot in self.shots: 198 | shot.move() 199 | shot.draw() 200 | if not ((margin < shot.x < width) and (margin < shot.y < height)): 201 | tempList.remove(shot) 202 | 203 | self.shots = tempList[:] 204 | 205 | def draw(self, troops): 206 | if len(troops): 207 | nearestPos = 0 208 | lowestDist = float("inf") 209 | for struct in troops: 210 | if struct.type == "GROUND": 211 | dist = (self.x - struct.x)**2 + (self.y - struct.y)**2 212 | if lowestDist > dist: 213 | self.nearestPos = [struct.x, struct.y] 214 | lowestDist = dist 215 | else: 216 | self.nearestPos = pygame.mouse.get_pos() 217 | 218 | pos = self.nearestPos 219 | #pos = pygame.mouse.get_pos() 220 | rotate = atan2(pos[1] - self.y, pos[0] - self.x) 221 | 222 | points = [(0, 0), (0, self.h), (self.w, self.h), (self.w, 0)] 223 | 224 | self.coord = getCoordPoly(self.x, self.y, self.r/2, 6, rotate) 225 | 226 | if self.level <= 3: 227 | self.rectCoord = [] 228 | 229 | for point in points: 230 | self.rectCoord.append(self.translate(self.rotate(point, rotate, (self.w/2, 0)))) 231 | if self.level >= 3: 232 | w = self.w/2 233 | h = self.h 234 | self.rectCoord = [] 235 | self.rectCoord2 = [] 236 | 237 | points = [(0, 0), (0, h), (w, h), (w, 0)] 238 | 239 | for point in points: 240 | self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2 + w, 0)))) 241 | self.rectCoord2.append(self.translate(self.rotate(point, rotate, (w/2 - w, 0)))) 242 | 243 | 244 | baseCoord = getCoordPoly(self.xOld, self.yOld, self.baseR, 4, radians(45)) 245 | 246 | pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.baseR*2, 40, 8)) 247 | pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.baseR*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2)) 248 | 249 | pygame.draw.polygon(display, self.colorBase, baseCoord) 250 | pygame.draw.polygon(display, self.colorBarrel, self.rectCoord) 251 | if self.level >= 3: 252 | pygame.draw.polygon(display, self.colorBarrel, self.rectCoord2) 253 | pygame.draw.polygon(display, self.colorHead, self.coord) 254 | 255 | if self.level == 2 or self.level == 4: 256 | pygame.draw.ellipse(display, self.headCover, (self.x - self.baseR/3, self.y - self.baseR/3, self.baseR*2/3, self.baseR*2/3)) 257 | 258 | #pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1) 259 | 260 | class Tower: 261 | def __init__(self, x, y, w, h, size, range, baseR, level, radius, speed, colorBarrel=(0, 0, 0), colorHead=(0, 0, 0), colorBase=(255, 0, 0)): 262 | self.x = x 263 | self.y = y 264 | 265 | self.xOld = self.x 266 | self.yOld = self.y 267 | self.angle = 0 268 | 269 | self.r = size 270 | self.w = w 271 | self.h = h 272 | self.type = "TOWER" 273 | self.baseR = baseR 274 | 275 | self.radius = radius 276 | self.speed = speed 277 | 278 | self.range = range 279 | self.coord = getCoordPoly(self.x, self.y, self.r, 4) 280 | self.rectCoord = [] 281 | 282 | self.attackType = "GROUND AND AIR" 283 | 284 | self.shots = [] 285 | 286 | self.recoil = 5 287 | self.rebound = 0.25 288 | 289 | self.level = level 290 | 291 | if self.level == 1: 292 | self.colorBarrel = (100, 30, 22) 293 | self.colorHead = (100, 30, 22) 294 | self.colorBase = (26, 82, 118) 295 | self.hitPoint = 380 296 | self.damage = 10 297 | elif self.level == 2: 298 | self.colorBarrel = (100, 30, 22) 299 | self.colorHead = (100, 30, 22) 300 | self.colorBase = (125, 102, 8) 301 | self.hitPoint = 580 302 | self.damage = 30 303 | elif self.level == 3: 304 | self.colorBarrel = (183, 149, 11) 305 | self.colorHead = (185, 119, 14) 306 | self.colorBase = (11, 83, 69) 307 | self.colorBase2 = (39, 174, 96) 308 | self.hitPoint = 810 309 | self.damage = 60 310 | elif self.level == 4: 311 | self.colorBarrel = (23, 32, 42) 312 | self.colorHead = (52, 73, 94) 313 | self.colorBase = (21, 67, 96) 314 | self.colorBase2 = (98, 101, 103) 315 | self.hitPoint = 1230 316 | self.damage = 90 317 | 318 | self.health = self.hitPoint 319 | 320 | self.nearestPos = pygame.mouse.get_pos() 321 | 322 | self.font = pygame.font.SysFont("Agency FB", 20) 323 | 324 | def updateHealth(self, troops): 325 | error = self.r 326 | for troop in troops: 327 | if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error): 328 | for shot in troop.shots: 329 | if troop.isHit((shot.x, shot.y)): 330 | self.health -= troop.damage 331 | 332 | def isHit(self, coord): 333 | error = self.r 334 | dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5 335 | if dist < error: 336 | return True 337 | return False 338 | 339 | def removeHit(self): 340 | tempList = self.shots[:] 341 | for shot in self.shots: 342 | if self.isHit((shot.x, shot.y)): 343 | tempList.remove(shot) 344 | 345 | self.shots = tempList[:] 346 | 347 | def rotate(self, coord, angle, anchor=(0, 0)): 348 | corr = 270 349 | return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)), 350 | (coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr))) 351 | 352 | def translate(self, coord): 353 | return [coord[0] + self.x, coord[1] + self.y] 354 | 355 | def shoot(self): 356 | error = 0.1 357 | 358 | if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)): 359 | self.x += self.rebound*cos(self.angle) 360 | self.y += self.rebound*sin(self.angle) 361 | else: 362 | #pos = pygame.mouse.get_pos() 363 | pos = self.nearestPos 364 | dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5 365 | self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld) 366 | 367 | if dist < self.range/2: 368 | shot = Shots((self.rectCoord[1][0] + self.rectCoord[2][0])/2, (self.rectCoord[1][1] + self.rectCoord[2][1])/2, self.radius, self.speed, self.angle, (0, 0, 0), "line") 369 | self.shots.append(shot) 370 | self.x -= self.recoil*cos(self.angle) 371 | self.y -= self.recoil*sin(self.angle) 372 | 373 | tempList = self.shots[:] 374 | 375 | for shot in self.shots: 376 | shot.move() 377 | shot.draw() 378 | if not ((margin < shot.x < width) and (margin < shot.y < height)): 379 | tempList.remove(shot) 380 | 381 | self.shots = tempList[:] 382 | 383 | def draw(self, troops): 384 | if len(troops): 385 | nearestPos = 0 386 | lowestDist = float("inf") 387 | for struct in troops: 388 | if struct.type == "AIR" or struct.type == "GROUND": 389 | dist = (self.x - struct.x)**2 + (self.y - struct.y)**2 390 | if lowestDist > dist: 391 | self.nearestPos = [struct.x, struct.y] 392 | lowestDist = dist 393 | else: 394 | self.nearestPos = pygame.mouse.get_pos() 395 | else: 396 | self.nearestPos = pygame.mouse.get_pos() 397 | 398 | #pos = pygame.mouse.get_pos() 399 | pos = self.nearestPos 400 | rotate = atan2(pos[1] - self.y, pos[0] - self.x) 401 | 402 | if self.level == 4: 403 | w = self.w*2 404 | h = self.h 405 | points = [(0, 0), (0, h), (w, h), (w, 0)] 406 | self.rectCoord = [] 407 | 408 | for point in points: 409 | self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0)))) 410 | else: 411 | w = self.w 412 | h = self.h 413 | points = [(0, 0), (0, h), (w, h), (w, 0)] 414 | self.rectCoord = [] 415 | 416 | for point in points: 417 | self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0)))) 418 | 419 | baseCoord = getCoordPoly(self.xOld, self.yOld, self.baseR, 6) 420 | 421 | pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.baseR*2, 40, 8)) 422 | pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.baseR*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2)) 423 | 424 | pygame.draw.polygon(display, self.colorBase, baseCoord) 425 | 426 | if self.level >= 3: 427 | baseCoord2 = getCoordPoly(self.xOld, self.yOld, self.baseR*3/4, 6) 428 | pygame.draw.polygon(display, self.colorBase2, baseCoord2) 429 | 430 | pygame.draw.polygon(display, self.colorBarrel, self.rectCoord) 431 | #pygame.draw.polygon(display, self.colorHead, self.coord) 432 | pygame.draw.ellipse(display, self.colorHead, (self.x - self.r/2, self.y - self.r/2, self.r, self.r )) 433 | 434 | #pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1) 435 | 436 | class Mortar: 437 | def __init__(self, x, y, w, h, r, range, level, radius, speed, colorBarrel=(0, 0, 0), colorHead=(0, 0, 0), colorBase=(255, 0, 0)): 438 | self.x = x 439 | self.y = y 440 | 441 | self.xOld = self.x 442 | self.yOld = self.y 443 | 444 | self.w = w 445 | self.h = h 446 | self.r = r 447 | 448 | self.level = level 449 | 450 | if self.level == 1: 451 | self.colorBarrel = (95, 106, 106) 452 | self.colorHead = (46, 64, 83) 453 | self.colorBase = (146, 43, 33) 454 | self.range = range 455 | self.hitPoint = 400 456 | self.damage = 37 457 | elif self.level == 2: 458 | self.colorBarrel = (125, 102, 8) 459 | self.colorHead = (11, 83, 69) 460 | self.colorBase = (166, 172, 175) 461 | self.colorHeadCover = (34, 153, 84) 462 | self.range = range 463 | self.hitPoint = 650 464 | self.damage = 70 465 | elif self.level == 3: 466 | self.colorBarrel = (21, 67, 96) 467 | self.colorHead = (36, 113, 163) 468 | self.colorBase = (113, 125, 126) 469 | self.colorBarrelHead = (46, 64, 83) 470 | self.range = range 471 | self.hitPoint = 850 472 | self.damage = 130 473 | 474 | self.radius = radius 475 | self.speed = speed 476 | 477 | self.type = "MORTAR" 478 | self.attackType = "GROUND" 479 | 480 | self.coord = [] 481 | self.rectCoord = [] 482 | 483 | self.shots = [] 484 | 485 | self.angle = 0 486 | self.recoil = 7 487 | self.rebound = 0.1 488 | 489 | self.health = self.hitPoint 490 | 491 | self.font = pygame.font.SysFont("Agency FB", 20) 492 | 493 | self.nearestPos = pygame.mouse.get_pos() 494 | 495 | def updateHealth(self, troops): 496 | error = 15 497 | for troop in troops: 498 | if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error): 499 | for shot in troop.shots: 500 | if troop.isHit((shot.x, shot.y)): 501 | self.health -= troop.damage 502 | 503 | def isHit(self, coord): 504 | error = 50 505 | dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5 506 | if dist < self.r: 507 | return True 508 | return False 509 | 510 | def removeHit(self): 511 | tempList = self.shots[:] 512 | for shot in self.shots: 513 | if self.isHit((shot.x, shot.y)): 514 | tempList.remove(shot) 515 | 516 | self.shots = tempList[:] 517 | 518 | def shoot(self): 519 | error = 0.05 520 | 521 | if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)): 522 | self.x += self.rebound*cos(self.angle) 523 | self.y += self.rebound*sin(self.angle) 524 | else: 525 | #pos = pygame.mouse.get_pos() 526 | pos = self.nearestPos 527 | dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5 528 | self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld) 529 | 530 | if dist < self.range/2: 531 | self.x -= self.recoil*cos(self.angle) 532 | self.y -= self.recoil*sin(self.angle) 533 | 534 | shot = Shots((self.rectCoord[1][0] + self.rectCoord[2][0])/2, (self.rectCoord[1][1] + self.rectCoord[2][1])/2, self.radius, self.speed, self.angle) 535 | self.shots.append(shot) 536 | 537 | tempList = self.shots[:] 538 | 539 | for shot in self.shots: 540 | shot.move() 541 | shot.draw() 542 | if not ((margin < shot.x < width) and (margin < shot.y < height)): 543 | tempList.remove(shot) 544 | 545 | self.shots = tempList[:] 546 | 547 | def rotate(self, coord, angle, anchor=(0, 0)): 548 | corr = 270 549 | return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)), 550 | (coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr))) 551 | 552 | def translate(self, coord): 553 | return [coord[0] + self.x, coord[1] + self.y] 554 | 555 | def draw(self, troops): 556 | if len(troops): 557 | nearestPos = 0 558 | lowestDist = float("inf") 559 | for struct in troops: 560 | if struct.type == "GROUND": 561 | dist = (self.x - struct.x)**2 + (self.y - struct.y)**2 562 | if lowestDist > dist: 563 | self.nearestPos = [struct.x, struct.y] 564 | lowestDist = dist 565 | else: 566 | self.neasestPos = pygame.mouse.get_pos() 567 | else: 568 | self.nearestPos = pygame.mouse.get_pos() 569 | 570 | pos = self.nearestPos 571 | #pos = pygame.mouse.get_pos() 572 | rotate = atan2(pos[1] - self.y, pos[0] - self.x) 573 | 574 | points = [(0, 0), (0, self.h), (self.w, self.h), (self.w, 0)] 575 | 576 | self.coord = getCoordPoly(self.x, self.y, self.w*5/8, 8, rotate) 577 | 578 | self.rectCoord = [] 579 | 580 | for point in points: 581 | self.rectCoord.append(self.translate(self.rotate(point, rotate, (self.w/2, 0)))) 582 | 583 | baseCoord = getCoordPoly(self.xOld, self.yOld, self.r*2/5, 10) 584 | 585 | pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r, 40, 8)) 586 | pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2)) 587 | 588 | pygame.draw.polygon(display, self.colorBase, baseCoord) 589 | pygame.draw.polygon(display, self.colorBarrel, self.rectCoord) 590 | pygame.draw.polygon(display, self.colorHead, self.coord) 591 | if self.level == 2: 592 | coord = getCoordPoly(self.x, self.y, self.w*3/8, 8, rotate) 593 | pygame.draw.polygon(display, self.colorHeadCover, coord) 594 | if self.level == 3: 595 | coord = [] 596 | w = self.w*1.2 597 | h = self.h/5 598 | points = [(0, 0), (0, h), (w, h), (w, 0)] 599 | 600 | for point in points: 601 | coord.append(self.translate(self.rotate(point, rotate, (w/2, h/2 - self.h)))) 602 | pygame.draw.polygon(display, self.colorBarrelHead, coord) 603 | #pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1) 604 | 605 | 606 | 607 | class HeadQuarters: 608 | def __init__(self, x, y, r, level, color1=(0, 0, 0), color2=(255, 255, 0)): 609 | self.x = x 610 | self.y = y 611 | self.r = r 612 | self.type = "HEADQUARTERS" 613 | self.color1 = (23, 32, 42) 614 | self.color2 = (113, 125, 126) 615 | 616 | self.level = level + 1 617 | if self.level == 2: 618 | self.hitPoint = 570 619 | elif self.level == 3: 620 | self.hitPoint = 1500 621 | elif self.level == 4: 622 | self.hitPoint = 3000 623 | elif self.level == 5: 624 | self.hitPoint = 4500 625 | elif self.level == 6: 626 | self.hitPoint = 6000 627 | 628 | self.health = self.hitPoint 629 | self.font = pygame.font.SysFont("Agency FB", 20) 630 | 631 | def updateHealth(self, troops): 632 | error = 15 633 | for troop in troops: 634 | if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error): 635 | for shot in troop.shots: 636 | if troop.isHit((shot.x, shot.y)): 637 | self.health -= troop.damage 638 | 639 | def draw(self): 640 | pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*1.5, 40, 8)) 641 | pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*1.5 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2)) 642 | 643 | 644 | outer = [] 645 | inner = [] 646 | 647 | for i in range(self.level): 648 | coord = getCoordPoly(self.x, self.y, self.r - (2.0/3)*self.r*(i)/(self.level), 5) 649 | if i == 0: 650 | outer = coord 651 | elif i == self.level - 1: 652 | inner = coord 653 | if i%2 == 0: 654 | pygame.draw.polygon(display, self.color1, coord) 655 | else: 656 | pygame.draw.polygon(display, self.color2, coord) 657 | 658 | for i in range(5): 659 | pygame.draw.line(display, self.color2, outer[i], inner[i], 3) 660 | 661 | gray = (121, 125, 127) 662 | darkGray = (28, 40, 51) 663 | yellow = (212, 172, 13) 664 | red = (203, 67, 53) 665 | orange = (202, 111, 30) 666 | blue = (36, 113, 163) 667 | green = (17, 122, 101) 668 | violet = (125, 60, 152) 669 | 670 | class Resource: 671 | def __init__(self, x, y): 672 | self.x = x 673 | self.y = y 674 | self.r = random.randrange(20, 40) 675 | self.type = "RESOURCE" 676 | colors = [gray, darkGray, yellow, red, orange, blue, green, violet] 677 | self.color1 = random.choice(colors) 678 | self.color2 = random.choice(colors) 679 | 680 | self.sides = random.randrange(4, 10) 681 | 682 | self.hitPoint = random.randrange(250, 700) 683 | self.health = self.hitPoint 684 | 685 | self.font = pygame.font.SysFont("Agency FB", 20) 686 | 687 | def updateHealth(self, troops): 688 | error = 15 689 | for troop in troops: 690 | if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error): 691 | for shot in troop.shots: 692 | if troop.isHit((shot.x, shot.y)): 693 | self.health -= troop.damage 694 | 695 | def draw(self): 696 | 697 | pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*1.5, 40, 8)) 698 | pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*1.5 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2)) 699 | 700 | outer = [] 701 | inner = [] 702 | 703 | level = self.sides 704 | for i in range(level): 705 | coord = getCoordPoly(self.x, self.y, self.r - (2.0/3)*self.r*(i)/(self.sides), self.sides) 706 | if i == 0: 707 | outer = coord 708 | elif i == level - 1: 709 | inner = coord 710 | if i%2 == 0: 711 | pygame.draw.polygon(display, self.color1, coord) 712 | else: 713 | pygame.draw.polygon(display, self.color2, coord) 714 | 715 | for i in range(level): 716 | pygame.draw.line(display, self.color2, outer[i], inner[i], 3) 717 | -------------------------------------------------------------------------------- /Chain_Reaction/Chain-Reaction.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Chain Reaction 4 | # Language - Python 5 | # Modules - pygame, sys, math 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 | import pygame 18 | import sys 19 | from math import * 20 | 21 | # Initialization of Pygame 22 | pygame.init() 23 | 24 | width = 400 25 | height = 400 26 | display = pygame.display.set_mode((width, height)) 27 | clock = pygame.time.Clock() 28 | 29 | # Colors 30 | background = (21, 67, 96) 31 | border = (208, 211, 212) 32 | red = (231, 76, 60) 33 | white = (244, 246, 247) 34 | violet = (136, 78, 160) 35 | yellow = (244, 208, 63) 36 | green = (88, 214, 141) 37 | 38 | playerColor = [red, green, violet, yellow] 39 | 40 | font = pygame.font.SysFont("Times New Roman", 30) 41 | 42 | blocks = 40 43 | noPlayers = 4 44 | 45 | pygame.display.set_caption("Chain Reaction %d Player" % noPlayers) 46 | 47 | score = [] 48 | for i in range(noPlayers): 49 | score.append(0) 50 | 51 | players = [] 52 | for i in range(noPlayers): 53 | players.append(playerColor[i]) 54 | 55 | d = blocks//2 - 2 56 | 57 | cols = int(width//blocks) 58 | rows = int(height//blocks) 59 | 60 | grid = [] 61 | 62 | # Quit or Close the Game Window 63 | def close(): 64 | pygame.quit() 65 | sys.exit() 66 | 67 | # Class for Each Spot in Grid 68 | class Spot(): 69 | def __init__(self): 70 | self.color = border 71 | self.neighbors = [] 72 | self.noAtoms = 0 73 | 74 | def addNeighbors(self, i, j): 75 | if i > 0: 76 | self.neighbors.append(grid[i - 1][j]) 77 | if i < rows - 1: 78 | self.neighbors.append(grid[i + 1][j]) 79 | if j < cols - 1: 80 | self.neighbors.append(grid[i][j + 1]) 81 | if j > 0: 82 | self.neighbors.append(grid[i][j - 1]) 83 | 84 | # Initializing the Grid with "Empty or 0" 85 | def initializeGrid(): 86 | global grid, score, players 87 | score = [] 88 | for i in range(noPlayers): 89 | score.append(0) 90 | 91 | players = [] 92 | for i in range(noPlayers): 93 | players.append(playerColor[i]) 94 | 95 | grid = [[]for _ in range(cols)] 96 | for i in range(cols): 97 | for j in range(rows): 98 | newObj = Spot() 99 | grid[i].append(newObj) 100 | for i in range(cols): 101 | for j in range(rows): 102 | grid[i][j].addNeighbors(i, j) 103 | 104 | # Draw the Grid in Pygame Window 105 | def drawGrid(currentIndex): 106 | r = 0 107 | c = 0 108 | for i in range(width//blocks): 109 | r += blocks 110 | c += blocks 111 | pygame.draw.line(display, players[currentIndex], (c, 0), (c, height)) 112 | pygame.draw.line(display, players[currentIndex], (0, r), (width, r)) 113 | 114 | # Draw the Present Situation of Grid 115 | def showPresentGrid(vibrate = 1): 116 | r = -blocks 117 | c = -blocks 118 | padding = 2 119 | for i in range(cols): 120 | r += blocks 121 | c = -blocks 122 | for j in range(rows): 123 | c += blocks 124 | if grid[i][j].noAtoms == 0: 125 | grid[i][j].color = border 126 | elif grid[i][j].noAtoms == 1: 127 | pygame.draw.ellipse(display, grid[i][j].color, (r + blocks/2 - d/2 + vibrate, c + blocks/2 - d/2, d, d)) 128 | elif grid[i][j].noAtoms == 2: 129 | pygame.draw.ellipse(display, grid[i][j].color, (r + 5, c + blocks/2 - d/2 - vibrate, d, d)) 130 | pygame.draw.ellipse(display, grid[i][j].color, (r + d/2 + blocks/2 - d/2 + vibrate, c + blocks/2 - d/2, d, d)) 131 | elif grid[i][j].noAtoms == 3: 132 | angle = 90 133 | x = r + (d/2)*cos(radians(angle)) + blocks/2 - d/2 134 | y = c + (d/2)*sin(radians(angle)) + blocks/2 - d/2 135 | pygame.draw.ellipse(display, grid[i][j].color, (x - vibrate, y, d, d)) 136 | x = r + (d/2)*cos(radians(angle + 90)) + blocks/2 - d/2 137 | y = c + (d/2)*sin(radians(angle + 90)) + 5 138 | pygame.draw.ellipse(display, grid[i][j].color, (x + vibrate, y, d, d)) 139 | x = r + (d/2)*cos(radians(angle - 90)) + blocks/2 - d/2 140 | y = c + (d/2)*sin(radians(angle - 90)) + 5 141 | pygame.draw.ellipse(display, grid[i][j].color, (x - vibrate, y, d, d)) 142 | 143 | pygame.display.update() 144 | 145 | # Increase the Atom when Clicked 146 | def addAtom(i, j, color): 147 | grid[i][j].noAtoms += 1 148 | grid[i][j].color = color 149 | if grid[i][j].noAtoms >= len(grid[i][j].neighbors): 150 | overFlow(grid[i][j], color) 151 | 152 | # Split the Atom when it Increases the "LIMIT" 153 | def overFlow(cell, color): 154 | showPresentGrid() 155 | cell.noAtoms = 0 156 | for m in range(len(cell.neighbors)): 157 | cell.neighbors[m].noAtoms += 1 158 | cell.neighbors[m].color = color 159 | if cell.neighbors[m].noAtoms >= len(cell.neighbors[m].neighbors): 160 | overFlow(cell.neighbors[m], color) 161 | 162 | # Checking if Any Player has WON! 163 | def isPlayerInGame(): 164 | global score 165 | playerScore = [] 166 | for i in range(noPlayers): 167 | playerScore.append(0) 168 | for i in range(cols): 169 | for j in range(rows): 170 | for k in range(noPlayers): 171 | if grid[i][j].color == players[k]: 172 | playerScore[k] += grid[i][j].noAtoms 173 | score = playerScore[:] 174 | 175 | # GAME OVER 176 | def gameOver(playerIndex): 177 | while True: 178 | for event in pygame.event.get(): 179 | if event.type == pygame.QUIT: 180 | close() 181 | if event.type == pygame.KEYDOWN: 182 | if event.key == pygame.K_q: 183 | close() 184 | if event.key == pygame.K_r: 185 | gameLoop() 186 | 187 | text = font.render("Player %d Won!" % (playerIndex + 1), True, white) 188 | text2 = font.render("Press \'r\' to Reset!", True, white) 189 | 190 | display.blit(text, (width/3, height/3)) 191 | display.blit(text2, (width/3, height/2 )) 192 | 193 | pygame.display.update() 194 | clock.tick(60) 195 | 196 | 197 | def checkWon(): 198 | num = 0 199 | for i in range(noPlayers): 200 | if score[i] == 0: 201 | num += 1 202 | if num == noPlayers - 1: 203 | for i in range(noPlayers): 204 | if score[i]: 205 | return i 206 | 207 | return 9999 208 | 209 | # Main Loop 210 | def gameLoop(): 211 | initializeGrid() 212 | loop = True 213 | 214 | turns = 0 215 | 216 | currentPlayer = 0 217 | 218 | vibrate = .5 219 | 220 | while loop: 221 | for event in pygame.event.get(): 222 | if event.type == pygame.QUIT: 223 | close() 224 | if event.type == pygame.KEYDOWN: 225 | if event.key == pygame.K_q: 226 | close() 227 | if event.type == pygame.MOUSEBUTTONDOWN: 228 | x, y = pygame.mouse.get_pos() 229 | i = x/blocks 230 | j = y/blocks 231 | if grid[i][j].color == players[currentPlayer] or grid[i][j].color == border: 232 | turns += 1 233 | addAtom(i, j, players[currentPlayer]) 234 | currentPlayer += 1 235 | if currentPlayer >= noPlayers: 236 | currentPlayer = 0 237 | if turns >= noPlayers: 238 | isPlayerInGame() 239 | 240 | 241 | display.fill(background) 242 | # Vibrate the Atoms in their Cells 243 | vibrate *= -1 244 | 245 | drawGrid(currentPlayer) 246 | showPresentGrid(vibrate) 247 | 248 | pygame.display.update() 249 | 250 | res = checkWon() 251 | if res < 9999: 252 | gameOver(res) 253 | 254 | clock.tick(20) 255 | 256 | gameLoop() 257 | -------------------------------------------------------------------------------- /Dodge_The_Ball/DodgeTheBall.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Dodge The Ball! 4 | # Language - Python 5 | # Modules - pygame, sys, random, math 6 | # 7 | # Controls - Mouse Movement 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 | from math import * 22 | 23 | pygame.init() 24 | 25 | width = 400 26 | height = 500 27 | display = pygame.display.set_mode((width, height)) 28 | pygame.display.set_caption("Dodge The Ball!") 29 | clock = pygame.time.Clock() 30 | 31 | background = (51, 51, 51) 32 | playerColor = (249, 231, 159) 33 | 34 | red = (203, 67, 53) 35 | yellow = (241, 196, 15) 36 | blue = (46, 134, 193) 37 | green = (34, 153, 84) 38 | purple = (136, 78, 160) 39 | orange = (214, 137, 16) 40 | 41 | colors = [red, yellow, blue, green, purple, orange] 42 | 43 | score = 0 44 | 45 | 46 | class Ball: 47 | def __init__(self, radius, speed): 48 | self.x = 0 49 | self.y = 0 50 | self.r = radius 51 | self.color = 0 52 | self.speed = speed 53 | self.angle = 0 54 | 55 | def createBall(self): 56 | self.x = width/2 - self.r 57 | self.y = height/2 - self.r 58 | self.color = random.choice(colors) 59 | self.angle = random.randint(-180, 180) 60 | 61 | def move(self): 62 | self.x += self.speed*cos(radians(self.angle)) 63 | self.y += self.speed*sin(radians(self.angle)) 64 | 65 | if self.x < self.r or self.x + self.r > width: 66 | self.angle = 180 - self.angle 67 | if self.y < self.r or self.y + self.r > height: 68 | self.angle *= -1 69 | 70 | def draw(self): 71 | pygame.draw.ellipse(display, self.color, (self.x - self.r, self.y - self.r, self.r*2, self.r*2)) 72 | 73 | def collision(self, radius): 74 | pos = pygame.mouse.get_pos() 75 | 76 | dist = ((pos[0] - self.x)**2 + (pos[1] - self.y)**2)**0.5 77 | 78 | if dist <= self.r + radius: 79 | gameOver() 80 | 81 | class Target: 82 | def __init__(self): 83 | self.x = 0 84 | self.y = 0 85 | self.w = 20 86 | self.h = self.w 87 | 88 | def generateNewCoord(self): 89 | self.x = random.randint(self.w, width - self.w) 90 | self.y = random.randint(self.h, height - self.h) 91 | 92 | def draw(self): 93 | color = random.choice(colors) 94 | 95 | pygame.draw.rect(display, color, (self.x, self.y, self.w, self.h)) 96 | 97 | 98 | def gameOver(): 99 | loop = True 100 | 101 | font = pygame.font.SysFont("Agency FB", 100) 102 | text = font.render("Game Over!", True, (230, 230, 230)) 103 | 104 | while loop: 105 | for event in pygame.event.get(): 106 | if event.type == pygame.QUIT: 107 | close() 108 | if event.type == pygame.KEYDOWN: 109 | if event.key == pygame.K_q: 110 | close() 111 | if event.key == pygame.K_r: 112 | gameLoop() 113 | 114 | display.fill(background) 115 | 116 | display.blit(text, (20, height/2 - 100)) 117 | displayScore() 118 | 119 | pygame.display.update() 120 | clock.tick() 121 | 122 | 123 | def checkCollision(target, d, objTarget): 124 | pos = pygame.mouse.get_pos() 125 | dist = ((pos[0] - target[0] - objTarget.w)**2 + (pos[1] - target[1] - objTarget.h)**2)**0.5 126 | 127 | if dist <= d + objTarget.w: 128 | return True 129 | return False 130 | 131 | 132 | def drawPlayerPointer(pos, r): 133 | pygame.draw.ellipse(display, playerColor, (pos[0] - r, pos[1] - r, 2*r, 2*r)) 134 | 135 | 136 | def close(): 137 | pygame.quit() 138 | sys.exit() 139 | 140 | def displayScore(): 141 | font = pygame.font.SysFont("Forte", 30) 142 | scoreText = font.render("Score: " + str(score), True, (230, 230, 230)) 143 | display.blit(scoreText, (10, 10)) 144 | 145 | 146 | def gameLoop(): 147 | global score 148 | score = 0 149 | 150 | loop = True 151 | 152 | pRadius = 10 153 | 154 | balls = [] 155 | 156 | for i in range(1): 157 | newBall = Ball(pRadius + 2, 5) 158 | newBall.createBall() 159 | balls.append(newBall) 160 | 161 | target = Target() 162 | target.generateNewCoord() 163 | 164 | while loop: 165 | for event in pygame.event.get(): 166 | if event.type == pygame.QUIT: 167 | close() 168 | if event.type == pygame.KEYDOWN: 169 | if event.key == pygame.K_q: 170 | close() 171 | if event.key == pygame.K_r: 172 | gameLoop() 173 | 174 | display.fill(background) 175 | 176 | for i in range(len(balls)): 177 | balls[i].move() 178 | 179 | for i in range(len(balls)): 180 | balls[i].draw() 181 | 182 | for i in range(len(balls)): 183 | balls[i].collision(pRadius) 184 | 185 | playerPos = pygame.mouse.get_pos() 186 | drawPlayerPointer((playerPos[0], playerPos[1]), pRadius) 187 | 188 | collide = checkCollision((target.x, target.y), pRadius, target) 189 | 190 | if collide: 191 | score += 1 192 | target.generateNewCoord() 193 | elif score == 2 and len(balls) == 1: 194 | newBall = Ball(pRadius + 2, 5) 195 | newBall.createBall() 196 | balls.append(newBall) 197 | target.generateNewCoord() 198 | elif score == 5 and len(balls) == 2: 199 | newBall = Ball(pRadius + 2, 6) 200 | newBall.createBall() 201 | balls.append(newBall) 202 | target.generateNewCoord() 203 | elif score == 10 and len(balls) == 3: 204 | newBall = Ball(pRadius + 2, 7) 205 | newBall.createBall() 206 | balls.append(newBall) 207 | target.generateNewCoord() 208 | elif score == 15 and len(balls) == 4: 209 | newBall = Ball(pRadius + 2, 8) 210 | newBall.createBall() 211 | balls.append(newBall) 212 | target.generateNewCoord() 213 | elif score == 20 and len(balls) == 5: 214 | newBall = Ball(pRadius + 2, 9) 215 | newBall.createBall() 216 | balls.append(newBall) 217 | target.generateNewCoord() 218 | 219 | target.draw() 220 | displayScore() 221 | 222 | pygame.display.update() 223 | clock.tick(60) 224 | 225 | gameLoop() 226 | -------------------------------------------------------------------------------- /Hangman/hangman.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | def game(): 5 | print_opening_message() 6 | secret_word = upload_secret_word() 7 | 8 | correct_letters = initializes_correct_letters(secret_word) 9 | print(correct_letters) 10 | 11 | hanged = False 12 | right = False 13 | failure = 0 14 | 15 | while(not hanged and not right): 16 | 17 | guess = ask_guess() 18 | 19 | if(guess in secret_word): 20 | correct_guess_mark(guess, correct_letters, secret_word) 21 | else: 22 | failure += 1 23 | draw_hang(failure) 24 | 25 | hanged = failure == 7 26 | right = "_" not in correct_letters 27 | 28 | print(correct_letters) 29 | 30 | if(right): 31 | prints_winner_message() 32 | else: 33 | prints_loser_message(secret_word) 34 | 35 | 36 | def draw_hang(failure): 37 | print(" _______ ") 38 | print(" |/ | ") 39 | 40 | if(failure == 1): 41 | print (" | (_) ") 42 | print (" | ") 43 | print (" | ") 44 | print (" | ") 45 | 46 | if(failure == 2): 47 | print (" | (_) ") 48 | print (" | \ ") 49 | print (" | ") 50 | print (" | ") 51 | 52 | if(failure == 3): 53 | print (" | (_) ") 54 | print (" | \| ") 55 | print (" | ") 56 | print (" | ") 57 | 58 | if(failure == 4): 59 | print (" | (_) ") 60 | print (" | \|/ ") 61 | print (" | ") 62 | print (" | ") 63 | 64 | if(failure == 5): 65 | print (" | (_) ") 66 | print (" | \|/ ") 67 | print (" | | ") 68 | print (" | ") 69 | 70 | if(failure == 6): 71 | print (" | (_) ") 72 | print (" | \|/ ") 73 | print (" | | ") 74 | print (" | / ") 75 | 76 | if (failure == 7): 77 | print (" | (_) ") 78 | print (" | \|/ ") 79 | print (" | | ") 80 | print (" | / \ ") 81 | 82 | print(" | ") 83 | print("_|___ ") 84 | print() 85 | 86 | 87 | 88 | def prints_winner_message(): 89 | print("Congratulations, you win!") 90 | print(" ___________ ") 91 | print(" '._==_==_=_.' ") 92 | print(" .-\\: /-. ") 93 | print(" | (|:. |) | ") 94 | print(" '-|:. |-' ") 95 | print(" \\::. / ") 96 | print(" '::. .' ") 97 | print(" ) ( ") 98 | print(" _.' '._ ") 99 | print(" '-------' ") 100 | 101 | 102 | def prints_loser_message(secret_word): 103 | print("Failed, you were hanged!") 104 | print("The word was {}".format(secret_word)) 105 | print(" _______________ ") 106 | print(" / \ ") 107 | print(" / \ ") 108 | print("// \/\ ") 109 | print("\| XXXX XXXX | / ") 110 | print(" | XXXX XXXX |/ ") 111 | print(" | XXX XXX | ") 112 | print(" | | ") 113 | print(" \__ XXX __/ ") 114 | print(" |\ XXX /| ") 115 | print(" | | | | ") 116 | print(" | I I I I I I I | ") 117 | print(" | I I I I I I | ") 118 | print(" \_ _/ ") 119 | print(" \_ _/ ") 120 | print(" \_______/ ") 121 | 122 | def correct_guess_mark(guess, correct_letters, secret_word): 123 | index = 0 124 | for word in secret_word: 125 | if (guess == word): 126 | correct_letters[index] = word 127 | index += 1 128 | 129 | def ask_guess(): 130 | guess = input("Which letter? ") 131 | guess = guess.strip().upper() 132 | return guess 133 | 134 | def initializes_correct_letters(word): 135 | return ["_" for word in word] 136 | 137 | def print_opening_message(): 138 | print("*********************************") 139 | print("**Welcome to the Hangman game!***") 140 | print("*********************************") 141 | 142 | def upload_secret_word(): 143 | archive = open("list.txt", "r") 144 | word = [] 145 | 146 | for line in archive: 147 | line = line.strip() 148 | word.append(line) 149 | 150 | archive.close() 151 | 152 | number = random.randrange(0, len(word)) 153 | secret_word = word[number].upper() 154 | return secret_word 155 | 156 | 157 | if(__name__ == "__main__"): 158 | game() 159 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Jatin Mandav 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Minesweeper/Minesweeper.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Minesweeper 4 | # Language - Python 5 | # Modules - pygame, sys, random 6 | # 7 | # Controls - Single Mouse Click on any box, r to reset the grid 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 = 510 25 | height = 510 26 | display = pygame.display.set_mode((width, height)) 27 | pygame.display.set_caption("Minesweeper") 28 | clock = pygame.time.Clock() 29 | 30 | background = (51, 51, 51) 31 | white = (236, 240, 241) 32 | darkWhite = (174, 182, 191) 33 | gray = (52, 73, 94) 34 | yellow = (244, 208, 63) 35 | lightred = (236, 112, 99) 36 | 37 | grid = [] 38 | size = 10 39 | mineProb = 30 40 | 41 | safeSpots = 0 42 | revealedSpots = 0 43 | 44 | numberFont = pygame.font.SysFont("Times New Roman", width/(size*2)) 45 | font = pygame.font.SysFont("Times New Roman", 35) 46 | 47 | # Property of Each Cell on Grid 48 | class Spot: 49 | def __init__(self, x, y, w, h, mineState): 50 | self.x = x 51 | self.y = y 52 | self.w = w 53 | self.h = h 54 | self.isMine = mineState 55 | self.neighbors = 0 56 | self.reveal = False 57 | 58 | # Draw Cells 59 | def draw(self): 60 | if not self.reveal: 61 | pygame.draw.rect(display, white, (self.x, self.y, self.w, self.h)) 62 | else: 63 | pygame.draw.rect(display, darkWhite, (self.x, self.y, self.w, self.h)) 64 | 65 | if self.reveal: 66 | if self.isMine: 67 | pygame.draw.ellipse(display, gray, (self.x + self.w/4, self.y + self.h/4, self.w/2, self.h/2)) 68 | else: 69 | if not self.neighbors == 0: 70 | num = numberFont.render(str(self.neighbors), True, gray) 71 | display.blit(num, (self.x + self.w/4, self.y + self.h/4)) 72 | 73 | # Check if the Cell is a Mine and reveal others if Block has no Mine Surrounding 74 | def checkForMine(self, i, j): 75 | global revealedSpots 76 | self.reveal = True 77 | revealedSpots += 1 78 | 79 | if self.isMine: 80 | for i in range(size): 81 | for j in range(size): 82 | grid[i][j].reveal = True 83 | 84 | drawGrid() 85 | pygame.display.update() 86 | gameLost() 87 | 88 | elif grid[i][j].neighbors == 0: 89 | if i > 0: 90 | if not grid[i-1][j].isMine: 91 | grid[i-1][j].reveal = True 92 | revealedSpots += 1 93 | if j > 0: 94 | if not grid[i][j-1].isMine: 95 | grid[i][j-1].reveal = True 96 | revealedSpots += 1 97 | if i < size - 1: 98 | if not grid[i+1][j].isMine: 99 | grid[i+1][j].reveal = True 100 | revealedSpots += 1 101 | if j < size - 1: 102 | if not grid[i][j+1].isMine: 103 | grid[i][j+1].reveal = True 104 | revealedSpots += 1 105 | if i > 0 and j > 0: 106 | if not grid[i-1][j-1].isMine: 107 | grid[i-1][j-1].reveal = True 108 | revealedSpots += 1 109 | if i > 0 and j < size - 1: 110 | if not grid[i-1][j+1].isMine: 111 | grid[i-1][j+1].reveal = True 112 | revealedSpots += 1 113 | if i < size - 1 and j > 0: 114 | if not grid[i+1][j-1].isMine: 115 | grid[i+1][j-1].reveal = True 116 | revealedSpots += 1 117 | if i < size - 1 and j < size - 1: 118 | if not grid[i+1][j+1].isMine: 119 | grid[i+1][j+1].reveal = True 120 | revealedSpots += 1 121 | 122 | # Count Neighboring Mines 123 | def countNeighborMines(self, i, j): 124 | if not self.isMine: 125 | if i > 0: 126 | if grid[i-1][j].isMine: 127 | self.neighbors += 1 128 | if j > 0: 129 | if grid[i][j-1].isMine: 130 | self.neighbors += 1 131 | if i < size - 1: 132 | if grid[i+1][j].isMine: 133 | self.neighbors += 1 134 | if j < size - 1: 135 | if grid[i][j+1].isMine: 136 | self.neighbors += 1 137 | if i > 0 and j > 0: 138 | if grid[i-1][j-1].isMine: 139 | self.neighbors += 1 140 | if i > 0 and j < size - 1: 141 | if grid[i-1][j+1].isMine: 142 | self.neighbors += 1 143 | if i < size - 1 and j > 0: 144 | if grid[i+1][j-1].isMine: 145 | self.neighbors += 1 146 | if i < size - 1 and j < size - 1: 147 | if grid[i+1][j+1].isMine: 148 | self.neighbors += 1 149 | 150 | 151 | # Initialize the Grid 152 | def generateGrid(): 153 | global grid, safeSpots 154 | grid = [] 155 | for i in range(size): 156 | grid.append([]) 157 | for j in range(size): 158 | prob = random.randint(1, 100) 159 | if prob < mineProb: 160 | newObj = Spot((width/size)*(j), (height/size)*(i), width/size - 3, height/size - 3, True) 161 | else: 162 | safeSpots += 1 163 | newObj = Spot((width/size)*(j), (height/size)*(i), width/size - 3, height/size - 3, False) 164 | 165 | grid[i].append(newObj) 166 | 167 | for i in range(size): 168 | for j in range(size): 169 | grid[i][j].countNeighborMines(i, j) 170 | 171 | # Check if Grid is solved 172 | def gameWon(): 173 | while True: 174 | for event in pygame.event.get(): 175 | if event.type == pygame.QUIT: 176 | close() 177 | if event.type == pygame.KEYDOWN: 178 | if event.key == pygame.K_q: 179 | close() 180 | if event.key == pygame.K_r: 181 | reset() 182 | 183 | font.set_bold(True) 184 | text = font.render("You Won the Minesweeper!", True, yellow) 185 | display.blit(text, (width/2 - 250, height/2)) 186 | pygame.display.update() 187 | clock.tick(60) 188 | 189 | # Check if Game is Lost 190 | def gameLost(): 191 | while True: 192 | for event in pygame.event.get(): 193 | if event.type == pygame.QUIT: 194 | close() 195 | if event.type == pygame.KEYDOWN: 196 | if event.key == pygame.K_q: 197 | close() 198 | if event.key == pygame.K_r: 199 | reset() 200 | 201 | font.set_bold(True) 202 | text = font.render("You Lost the Game!", True, (236, 112, 99)) 203 | display.blit(text, (width/2 - 250, height/2)) 204 | pygame.display.update() 205 | clock.tick(60) 206 | 207 | # Draw the Grid 208 | def drawGrid(): 209 | for i in range(size): 210 | for j in range(size): 211 | grid[i][j].draw() 212 | 213 | # Reset the Grid 214 | def reset(): 215 | minesweeper() 216 | 217 | # Close the Game 218 | def close(): 219 | pygame.quit() 220 | sys.exit() 221 | 222 | # The Game 223 | def minesweeper(): 224 | loop = True 225 | 226 | generateGrid() 227 | 228 | while loop: 229 | for event in pygame.event.get(): 230 | if event.type == pygame.QUIT: 231 | close() 232 | if event.type == pygame.KEYDOWN: 233 | if event.key == pygame.K_q: 234 | close() 235 | if event.key == pygame.K_r: 236 | reset() 237 | if event.type == pygame.MOUSEBUTTONDOWN: 238 | pos = pygame.mouse.get_pos() 239 | j = pos[0]/(width/size) 240 | i = pos[1]/(width/size) 241 | grid[i][j].checkForMine(i, j) 242 | 243 | display.fill(background) 244 | 245 | drawGrid() 246 | 247 | if revealedSpots == safeSpots: 248 | gameWon() 249 | 250 | pygame.display.update() 251 | clock.tick(60) 252 | 253 | minesweeper() 254 | -------------------------------------------------------------------------------- /Pong/Pong.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Pong 4 | # Language - Python 5 | # Modules - pygame, sys, random, math 6 | # 7 | # Controls - Arrow Keys for Right Paddle and WASD Keys for Left Paddle 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 | from math import * 22 | 23 | pygame.init() 24 | 25 | width = 600 26 | height = 400 27 | display = pygame.display.set_mode((width, height)) 28 | pygame.display.set_caption("Pong!") 29 | clock = pygame.time.Clock() 30 | 31 | background = (27, 38, 49) 32 | white = (236, 240, 241) 33 | red = (203, 67, 53) 34 | blue = (52, 152, 219) 35 | yellow = (244, 208, 63) 36 | 37 | top = white 38 | bottom = white 39 | left = white 40 | right = white 41 | 42 | margin = 4 43 | 44 | scoreLeft = 0 45 | scoreRight = 0 46 | maxScore = 20 47 | 48 | font = pygame.font.SysFont("Small Fonts", 30) 49 | largeFont = pygame.font.SysFont("Small Fonts", 60) 50 | 51 | # Draw the Boundary of Board 52 | def boundary(): 53 | global top, bottom, left, right 54 | pygame.draw.rect(display, left, (0, 0, margin, height)) 55 | pygame.draw.rect(display, top, (0, 0, width, margin)) 56 | pygame.draw.rect(display, right, (width-margin, 0, margin, height)) 57 | pygame.draw.rect(display, bottom, (0, height - margin, width, margin)) 58 | 59 | l = 25 60 | 61 | pygame.draw.rect(display, white, (width/2-margin/2, 10, margin, l)) 62 | pygame.draw.rect(display, white, (width/2-margin/2, 60, margin, l)) 63 | pygame.draw.rect(display, white, (width/2-margin/2, 110, margin, l)) 64 | pygame.draw.rect(display, white, (width/2-margin/2, 160, margin, l)) 65 | pygame.draw.rect(display, white, (width/2-margin/2, 210, margin, l)) 66 | pygame.draw.rect(display, white, (width/2-margin/2, 260, margin, l)) 67 | pygame.draw.rect(display, white, (width/2-margin/2, 310, margin, l)) 68 | pygame.draw.rect(display, white, (width/2-margin/2, 360, margin, l)) 69 | 70 | # Paddle Class 71 | class Paddle: 72 | def __init__(self, position): 73 | self.w = 10 74 | self.h = self.w*8 75 | self.paddleSpeed = 6 76 | 77 | if position == -1: 78 | self.x = 1.5*margin 79 | else: 80 | self.x = width - 1.5*margin - self.w 81 | 82 | self.y = height/2 - self.h/2 83 | 84 | # Show the Paddle 85 | def show(self): 86 | pygame.draw.rect(display, white, (self.x, self.y, self.w, self.h)) 87 | 88 | # Move the Paddle 89 | def move(self, ydir): 90 | self.y += self.paddleSpeed*ydir 91 | if self.y < 0: 92 | self.y -= self.paddleSpeed*ydir 93 | elif self.y + self.h> height: 94 | self.y -= self.paddleSpeed*ydir 95 | 96 | 97 | leftPaddle = Paddle(-1) 98 | rightPaddle = Paddle(1) 99 | 100 | # Ball Class 101 | class Ball: 102 | def __init__(self, color): 103 | self.r = 20 104 | self.x = width/2 - self.r/2 105 | self.y = height/2 -self.r/2 106 | self.color = color 107 | self.angle = random.randint(-75, 75) 108 | if random.randint(0, 1): 109 | self.angle += 180 110 | 111 | self.speed = 8 112 | 113 | # Show the Ball 114 | def show(self): 115 | pygame.draw.ellipse(display, self.color, (self.x, self.y, self.r, self.r)) 116 | 117 | # Move the Ball 118 | def move(self): 119 | global scoreLeft, scoreRight 120 | self.x += self.speed*cos(radians(self.angle)) 121 | self.y += self.speed*sin(radians(self.angle)) 122 | if self.x + self.r > width - margin: 123 | scoreLeft += 1 124 | self.angle = 180 - self.angle 125 | if self.x < margin: 126 | scoreRight += 1 127 | self.angle = 180 - self.angle 128 | if self.y < margin: 129 | self.angle = - self.angle 130 | if self.y + self.r >=height - margin: 131 | self.angle = - self.angle 132 | 133 | # Check and Reflect the Ball when it hits the padddle 134 | def checkForPaddle(self): 135 | if self.x < width/2: 136 | if leftPaddle.x < self.x < leftPaddle.x + leftPaddle.w: 137 | if leftPaddle.y < self.y < leftPaddle.y + 10 or leftPaddle.y < self.y + self.r< leftPaddle.y + 10: 138 | self.angle = -45 139 | if leftPaddle.y + 10 < self.y < leftPaddle.y + 20 or leftPaddle.y + 10 < self.y + self.r< leftPaddle.y + 20: 140 | self.angle = -30 141 | if leftPaddle.y + 20 < self.y < leftPaddle.y + 30 or leftPaddle.y + 20 < self.y + self.r< leftPaddle.y + 30: 142 | self.angle = -15 143 | if leftPaddle.y + 30 < self.y < leftPaddle.y + 40 or leftPaddle.y + 30 < self.y + self.r< leftPaddle.y + 40: 144 | self.angle = -10 145 | if leftPaddle.y + 40 < self.y < leftPaddle.y + 50 or leftPaddle.y + 40 < self.y + self.r< leftPaddle.y + 50: 146 | self.angle = 10 147 | if leftPaddle.y + 50 < self.y < leftPaddle.y + 60 or leftPaddle.y + 50 < self.y + self.r< leftPaddle.y + 60: 148 | self.angle = 15 149 | if leftPaddle.y + 60 < self.y < leftPaddle.y + 70 or leftPaddle.y + 60 < self.y + self.r< leftPaddle.y + 70: 150 | self.angle = 30 151 | if leftPaddle.y + 70 < self.y < leftPaddle.y + 80 or leftPaddle.y + 70 < self.y + self.r< leftPaddle.y + 80: 152 | self.angle = 45 153 | else: 154 | if rightPaddle.x + rightPaddle.w > self.x + self.r > rightPaddle.x: 155 | if rightPaddle.y < self.y < leftPaddle.y + 10 or leftPaddle.y < self.y + self.r< leftPaddle.y + 10: 156 | self.angle = -135 157 | if rightPaddle.y + 10 < self.y < rightPaddle.y + 20 or rightPaddle.y + 10 < self.y + self.r< rightPaddle.y + 20: 158 | self.angle = -150 159 | if rightPaddle.y + 20 < self.y < rightPaddle.y + 30 or rightPaddle.y + 20 < self.y + self.r< rightPaddle.y + 30: 160 | self.angle = -165 161 | if rightPaddle.y + 30 < self.y < rightPaddle.y + 40 or rightPaddle.y + 30 < self.y + self.r< rightPaddle.y + 40: 162 | self.angle = 170 163 | if rightPaddle.y + 40 < self.y < rightPaddle.y + 50 or rightPaddle.y + 40 < self.y + self.r< rightPaddle.y + 50: 164 | self.angle = 190 165 | if rightPaddle.y + 50 < self.y < rightPaddle.y + 60 or rightPaddle.y + 50 < self.y + self.r< rightPaddle.y + 60: 166 | self.angle = 165 167 | if rightPaddle.y + 60 < self.y < rightPaddle.y + 70 or rightPaddle.y + 60 < self.y + self.r< rightPaddle.y + 70: 168 | self.angle = 150 169 | if rightPaddle.y + 70 < self.y < rightPaddle.y + 80 or rightPaddle.y + 70 < self.y + self.r< rightPaddle.y + 80: 170 | self.angle = 135 171 | 172 | # Show the Score 173 | def showScore(): 174 | leftScoreText = font.render("Score : " + str(scoreLeft), True, red) 175 | rightScoreText = font.render("Score : " + str(scoreRight), True, blue) 176 | 177 | display.blit(leftScoreText, (3*margin, 3*margin)) 178 | display.blit(rightScoreText, (width/2 + 3*margin, 3*margin)) 179 | 180 | # Game Over 181 | def gameOver(): 182 | if scoreLeft == maxScore or scoreRight == maxScore: 183 | while True: 184 | for event in pygame.event.get(): 185 | if event.type == pygame.QUIT: 186 | close() 187 | if event.type == pygame.KEYDOWN: 188 | if event.key == pygame.K_q: 189 | close() 190 | if event.key == pygame.K_r: 191 | reset() 192 | if scoreLeft == maxScore: 193 | playerWins = largeFont.render("Left Player Wins!", True, red) 194 | elif scoreRight == maxScore: 195 | playerWins = largeFont.render("Right Player Wins!", True, blue) 196 | 197 | display.blit(playerWins, (width/2 - 100, height/2)) 198 | pygame.display.update() 199 | 200 | def reset(): 201 | global scoreLeft, scoreRight 202 | scoreLeft = 0 203 | scoreRight = 0 204 | board() 205 | 206 | 207 | def close(): 208 | pygame.quit() 209 | sys.exit() 210 | 211 | def board(): 212 | loop = True 213 | leftChange = 0 214 | rightChange = 0 215 | ball = Ball(yellow) 216 | 217 | while loop: 218 | for event in pygame.event.get(): 219 | if event.type == pygame.QUIT: 220 | close() 221 | if event.type == pygame.KEYDOWN: 222 | if event.key == pygame.K_q: 223 | close() 224 | if event.key == pygame.K_SPACE or event.key == pygame.K_p: 225 | Pause() 226 | if event.key == pygame.K_r: 227 | reset() 228 | if event.key == pygame.K_w: 229 | leftChange = -1 230 | if event.key == pygame.K_s: 231 | leftChange = 1 232 | if event.key == pygame.K_UP: 233 | rightChange = -1 234 | if event.key == pygame.K_DOWN: 235 | rightChange = 1 236 | if event.type == pygame.KEYUP: 237 | leftChange = 0 238 | rightChange = 0 239 | 240 | leftPaddle.move(leftChange) 241 | rightPaddle.move(rightChange) 242 | ball.move() 243 | ball.checkForPaddle() 244 | 245 | display.fill(background) 246 | showScore() 247 | 248 | ball.show() 249 | leftPaddle.show() 250 | rightPaddle.show() 251 | 252 | boundary() 253 | 254 | gameOver() 255 | 256 | pygame.display.update() 257 | clock.tick(60) 258 | 259 | board() 260 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gaming-in-Python 2 | 3 | Here you can find the source code to all the Games I made using Python Programming Language 4 | 5 | Details about all the Games can be Found on my Blog : https://jatinmandav.wordpress.com 6 | 7 | ## List of Games 8 | - [Balloon Shooter](BalloonShooter/) 9 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U 10 | - [Minesweeper](Minesweeper/) 11 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U 12 | - [Pong!](Pong/) 13 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U 14 | - [Snake 2D](Snake_2d/) 15 | - More about Snake 2D : https://jatinmandav.wordpress.com/2017/08/10/snake-game-in-python/ 16 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=ix1wyDwfuIU&t=41s 17 | - [Space Invaders](Space_Invaders/) 18 | - More about Space Invaders : https://jatinmandav.wordpress.com/2017/08/14/space-invaders-in-python/ 19 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=XryDmOnWY8A&t=25s 20 | - [StopWatch](Stopwatch/) 21 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U 22 | - [Tron 2D](Tron/) 23 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U 24 | - [Chain Reaction](Chain_Reaction/) 25 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=U0Hii6Jc9c8&t=1s 26 | - More About Chain Reaction : https://jatinmandav.wordpress.com/2017/09/27/chain-reaction-in-python/ 27 | - [8 Ball Pool](8_Ball_Pool/) 28 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=K2sJSeN85eo&feature=youtu.be 29 | - More About 8 Ball Pool : https://jatinmandav.wordpress.com/2017/10/13/8-ball-pool-in-python/ 30 | - [Stack Tower](Stack_Tower/) 31 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=SgtqhG28JnM&feature=youtu.be 32 | - More About Stack Tower : https://jatinmandav.wordpress.com/2017/11/05/stacks-in-python-using-pygame/ 33 | - [Dodge The Ball!](Dodge_The_Ball/) 34 | - Time Lapse Video on YouTube : https://www.youtube.com/watch?v=PD17M_Dwdf8&feature=youtu.be 35 | - [Battles](Battles/) 36 | Battles is a game inspired by popular mobile video game Clash of Clans 37 | - Video on YouTube : https://www.youtube.com/watch?v=QmZWxahFvzw&t=0s 38 | - More details on Blog : https://jatinmandav.wordpress.com/2018/01/03/battles-game-inspired-by-clash-of-clans/ 39 | - [Angry Birds](Angry_Birds/) 40 | - Video on YouTube : [Angry Birds - 14 Hour Time Lapse](https://www.youtube.com/watch?v=6in-mdiumcA&feature=youtu.be) 41 | - More on Blog : [Angry Birds in Python Using PyGame](https://jatinmandav.wordpress.com/2018/05/25/angry-birds-in-python-using-pygame/) 42 | -------------------------------------------------------------------------------- /Snake_2d/snake.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Snake - 2D 4 | # Language - Python 5 | # Modules - pygame, sys, random, copy, time 6 | # 7 | # Controls - Arrow Keys 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 | import pygame 18 | import sys 19 | import copy 20 | import random 21 | import time 22 | 23 | pygame.init() 24 | 25 | width = 500 26 | height = 500 27 | scale = 10 28 | score = 0 29 | 30 | food_x = 10 31 | food_y = 10 32 | 33 | display = pygame.display.set_mode((width, height)) 34 | pygame.display.set_caption("Snake Game") 35 | clock = pygame.time.Clock() 36 | 37 | background = (23, 32, 42) 38 | snake_colour = (236, 240, 241) 39 | food_colour = (148, 49, 38) 40 | snake_head = (247, 220, 111) 41 | 42 | 43 | # ----------- Snake Class ---------------- 44 | # self.history[0][0] is the location of the head of the snake 45 | 46 | class Snake: 47 | def __init__(self, x_start, y_start): 48 | self.x = x_start 49 | self.y = y_start 50 | self.w = 10 51 | self.h = 10 52 | self.x_dir = 1 53 | self.y_dir = 0 54 | self.history = [[self.x, self.y]] 55 | self.length = 1 56 | 57 | def reset(self): 58 | self.x = width/2-scale 59 | self.y = height/2-scale 60 | self.w = 10 61 | self.h = 10 62 | self.x_dir = 1 63 | self.y_dir = 0 64 | self.history = [[self.x, self.y]] 65 | self.length = 1 66 | 67 | #function to show the body of snake 68 | def show(self): 69 | for i in range(self.length): 70 | if not i == 0: 71 | pygame.draw.rect(display, snake_colour, (self.history[i][0], self.history[i][1], self.w, self.h)) 72 | else: 73 | pygame.draw.rect(display, snake_head, (self.history[i][0], self.history[i][1], self.w, self.h)) 74 | 75 | 76 | def check_eaten(self): 77 | if abs(self.history[0][0] - food_x) < scale and abs(self.history[0][1] - food_y) < scale: 78 | return True 79 | 80 | def grow(self): 81 | self.length += 1 82 | self.history.append(self.history[self.length-2]) 83 | 84 | def death(self): 85 | i = self.length - 1 86 | while i > 0: 87 | if abs(self.history[0][0] - self.history[i][0]) < self.w and abs(self.history[0][1] - self.history[i][1]) < self.h and self.length > 2: 88 | return True 89 | i -= 1 90 | 91 | def update(self): 92 | i = self.length - 1 93 | while i > 0: 94 | self.history[i] = copy.deepcopy(self.history[i-1]) 95 | i -= 1 96 | self.history[0][0] += self.x_dir*scale 97 | self.history[0][1] += self.y_dir*scale 98 | 99 | def autoplay(self): 100 | 101 | if abs(food_x-self.history[0][0]) < 10 and abs(food_y-self.history[0][1]) < 10: 102 | # if self.check_eaten(): 103 | # food.new_location() 104 | # score += 1 105 | # self.grow() 106 | print("") 107 | elif abs(food_x-self.history[0][0]) < 10: 108 | 109 | # if self.y_dir==1 or self.y_dir==-1: 110 | # self.y_dir=0 111 | # self.x_dir=1 112 | if self.x_dir==1 or self.x_dir==-1: 113 | if food_y>self.history[0][1]: 114 | self.y_dir=1 115 | else: 116 | self.y_dir=-1 117 | self.x_dir=0 118 | elif abs(food_y-self.history[0][1]) < 10 : 119 | 120 | # if self.x_dir==1 or self.x_dir==-1: 121 | # self.x_dir=0 122 | # self.y_dir=1 123 | if self.y_dir==1 or self.y_dir==-1: 124 | self.y_dir=0 125 | if food_x>self.history[0][0]: 126 | self.x_dir=1 127 | else: 128 | self.x_dir=-1 129 | 130 | 131 | elif food_x-self.history[0][0] >= 10 and food_y-self.history[0][1] >= 10: 132 | 133 | if self.x_dir==-1: 134 | self.y_dir=1 135 | self.x_dir=0 136 | elif self.y_dir==-1: 137 | self.y_dir=0 138 | self.x_dir=1 139 | elif self.history[0][0]-food_x >= 10 and food_y-self.history[0][1] >= 10: 140 | 141 | if self.x_dir==1: 142 | self.y_dir=1 143 | self.x_dir=0 144 | elif self.y_dir==1: 145 | self.y_dir=0 146 | self.x_dir=-1 147 | elif self.history[0][0]-food_x >= 10 and self.history[0][1]-food_y >= 10: 148 | 149 | if self.x_dir==1: 150 | self.y_dir=-1 151 | self.x_dir=0 152 | elif self.y_dir==1: 153 | self.y_dir=0 154 | self.x_dir=-1 155 | 156 | elif food_x-self.history[0][0] >= 10 and self.history[0][1]-food_y >= 10: 157 | 158 | if self.x_dir==-1: 159 | self.y_dir=-1 160 | self.x_dir=0 161 | elif self.y_dir==1: 162 | self.y_dir=0 163 | self.x_dir=1 164 | 165 | self.update() 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | # ----------- Food Class -------------- 175 | class Food: 176 | def new_location(self): 177 | global food_x, food_y 178 | food_x = random.randrange(1, width/scale-1)*scale 179 | food_y = random.randrange(1, height/scale-1)*scale 180 | 181 | def show(self): 182 | pygame.draw.rect(display, food_colour, (food_x, food_y, scale, scale)) 183 | 184 | 185 | def show_score(): 186 | font = pygame.font.SysFont("Copperplate Gothic Bold", 20) 187 | text = font.render("Score: " + str(score), True, snake_colour) 188 | display.blit(text, (scale, scale)) 189 | 190 | 191 | # ----------- Main Game Loop ------------- 192 | def gameLoop(): 193 | loop = True 194 | 195 | global score 196 | 197 | snake = Snake(width/2, height/2) #starting from mid of grid 198 | food = Food() 199 | food.new_location() 200 | ap=False 201 | 202 | while loop: 203 | 204 | display.fill(background) 205 | snake.show() 206 | food.show() 207 | show_score() 208 | 209 | for event in pygame.event.get(): 210 | if event.type == pygame.QUIT: 211 | pygame.quit() 212 | sys.exit() 213 | 214 | if event.type == pygame.KEYDOWN: 215 | if event.key == pygame.K_q: 216 | pygame.quit() 217 | sys.exit() 218 | if event.key==pygame.K_SPACE: #autoplay start 219 | ap=True 220 | if event.key==pygame.K_TAB: #autoplay end 221 | ap=False 222 | else: 223 | if snake.y_dir == 0: 224 | if event.key == pygame.K_UP: 225 | snake.x_dir = 0 226 | snake.y_dir = -1 227 | if event.key == pygame.K_DOWN: 228 | snake.x_dir = 0 229 | snake.y_dir = 1 230 | 231 | if snake.x_dir == 0: 232 | if event.key == pygame.K_LEFT: 233 | snake.x_dir = -1 234 | snake.y_dir = 0 235 | if event.key == pygame.K_RIGHT: 236 | snake.x_dir = 1 237 | snake.y_dir = 0 238 | 239 | 240 | if ap: 241 | snake.autoplay() 242 | else: 243 | snake.update() 244 | 245 | 246 | if snake.check_eaten(): 247 | food.new_location() 248 | score += 1 249 | snake.grow() 250 | 251 | 252 | if snake.death(): 253 | score = 0 254 | font = pygame.font.SysFont("Copperplate Gothic Bold", 50) 255 | text = font.render("Game Over!", True, snake_colour) 256 | display.blit(text, (width/2-50, height/2)) 257 | pygame.display.update() 258 | time.sleep(3) 259 | snake.reset() 260 | 261 | 262 | #updating the values if snake goes out of board 263 | if snake.history[0][0] > width: 264 | snake.history[0][0] = 0 265 | if snake.history[0][0] < 0: 266 | snake.history[0][0] = width 267 | 268 | if snake.history[0][1] > height: 269 | snake.history[0][1] = 0 270 | if snake.history[0][1] < 0: 271 | snake.history[0][1] = height 272 | 273 | pygame.display.update() 274 | clock.tick(10) #at most 10 frames should pass in 1 sec, it is used to control the speed of snake 275 | 276 | gameLoop() 277 | -------------------------------------------------------------------------------- /Space_Invaders/space invaders.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Space Invaders 4 | # Language - Python 5 | # Modules - pygame, sys, time 6 | # 7 | # Controls - Left and Right Keys to Move, Space to shoot 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 time 21 | 22 | # -------------- Initialization ------------ 23 | pygame.init() 24 | 25 | width = 700 26 | height = 500 27 | 28 | display = pygame.display.set_mode((width, height)) 29 | clock = pygame.time.Clock() 30 | pygame.display.set_caption("Space Invaders") 31 | 32 | ship_width = 40 33 | ship_height = 30 34 | 35 | # -------------- Colours ----------------- 36 | background = (74, 35, 90) 37 | white = (244, 246, 247) 38 | yellow = (241, 196, 15) 39 | orange = (186, 74, 0) 40 | green = (35, 155, 86) 41 | white1 = (253, 254, 254) 42 | dark_gray = (23, 32, 42) 43 | 44 | 45 | # -------------- Space-Ship Class -------------- 46 | class SpaceShip: 47 | def __init__(self, x, y, w, h, colour): 48 | self.x = x 49 | self.y = y 50 | self.w = w 51 | self.h = h 52 | self.colour = colour 53 | 54 | def draw(self): 55 | pygame.draw.rect(display, yellow, (self.x + self.w/2 - 8, self.y - 10, 16, 10)) 56 | pygame.draw.rect(display, self.colour, (self.x, self.y, self.w, self.h)) 57 | pygame.draw.rect(display, dark_gray, (self.x + 5, self.y + 6, 10, self.h - 10)) 58 | pygame.draw.rect(display, dark_gray, (self.x + self.w - 15, self.y + 6, 10, self.h - 10)) 59 | 60 | 61 | # ----------------- Bullet Class ------------- 62 | class Bullet: 63 | def __init__(self, x, y): 64 | self.x = x 65 | self.y = y 66 | self.d = 10 67 | self.speed = -5 68 | 69 | def draw(self): 70 | pygame.draw.ellipse(display, orange, (self.x, self.y, self.d, self.d)) 71 | 72 | def move(self): 73 | self.y += self.speed 74 | 75 | def hit(self, x, y, d): 76 | if x < self.x < x + d: 77 | if y + d > self.y > y: 78 | return True 79 | 80 | 81 | # ------------------ Alien Class --------------- 82 | class Alien: 83 | def __init__(self, x, y, d): 84 | self.x = x 85 | self.y = y 86 | self.d = d 87 | self.x_dir = 1 88 | self.speed = 3 89 | 90 | def draw(self): 91 | pygame.draw.ellipse(display, green, (self.x, self.y, self.d, self.d)) 92 | pygame.draw.ellipse(display, dark_gray, (self.x + 10, self.y + self.d/3, 8, 8), 2) 93 | pygame.draw.ellipse(display, dark_gray, (self.x + self.d - 20, self.y + self.d/3, 8, 8), 2) 94 | pygame.draw.rect(display, dark_gray, (self.x, self.y+self.d-20, 50, 7)) 95 | 96 | def move(self): 97 | self.x += self.x_dir*self.speed 98 | 99 | def shift_down(self): 100 | self.y += self.d 101 | 102 | 103 | # ------------------- Saved ------------------ 104 | def saved(): 105 | font = pygame.font.SysFont("Wide Latin", 22) 106 | font_large = pygame.font.SysFont("Wide Latin", 43) 107 | text2 = font_large.render("Congratulations!", True, white1) 108 | text = font.render("You Prevented the Alien Invasion!", True, white1) 109 | display.blit(text2, (60, height/2)) 110 | display.blit(text, (45, height/2 + 100)) 111 | pygame.display.update() 112 | time.sleep(3) 113 | 114 | 115 | # -------------------- Death ---------------- 116 | def GameOver(): 117 | font = pygame.font.SysFont("Chiller", 50) 118 | font_large = pygame.font.SysFont("Chiller", 100) 119 | text2 = font_large.render("Game Over!", True, white1) 120 | text = font.render("You Could not Prevent the Alien Invasion!", True, white1) 121 | display.blit(text2, (180, height/2-50)) 122 | display.blit(text, (45, height/2 + 100)) 123 | 124 | 125 | # --------------------- The Game ------------------ 126 | def game(): 127 | invasion = False 128 | ship = SpaceShip(width/2-ship_width/2, height-ship_height - 10, ship_width, ship_height, white) 129 | 130 | bullets = [] 131 | num_bullet = 0 132 | for i in range(num_bullet): 133 | i = Bullet(width/2 - 5, height - ship_height - 20) 134 | bullets.append(i) 135 | 136 | x_move = 0 137 | 138 | aliens = [] 139 | num_aliens = 8 140 | d = 50 141 | for i in range(num_aliens): 142 | i = Alien((i+1)*d + i*20, d+20, d) 143 | aliens.append(i) 144 | 145 | while not invasion: 146 | for event in pygame.event.get(): 147 | if event.type == pygame.QUIT: 148 | pygame.quit() 149 | sys.exit() 150 | 151 | if event.type == pygame.KEYDOWN: 152 | if event.key == pygame.K_q: 153 | pygame.quit() 154 | sys.exit() 155 | 156 | if event.key == pygame.K_RIGHT: 157 | x_move = 5 158 | 159 | if event.key == pygame.K_LEFT: 160 | x_move = -5 161 | 162 | if event.key == pygame.K_SPACE: 163 | num_bullet += 1 164 | i = Bullet(ship.x + ship_width/2 - 5, ship.y) 165 | bullets.append(i) 166 | 167 | if event.type == pygame.KEYUP: 168 | x_move = 0 169 | 170 | display.fill(background) 171 | 172 | for i in range(num_bullet): 173 | bullets[i].draw() 174 | bullets[i].move() 175 | 176 | for alien in list(aliens): 177 | alien.draw() 178 | alien.move() 179 | for item in list(bullets): 180 | if item.hit(alien.x, alien.y, alien.d): 181 | bullets.remove(item) 182 | num_bullet -= 1 183 | aliens.remove(alien) 184 | num_aliens -= 1 185 | 186 | if num_aliens == 0: 187 | saved() 188 | invasion = True 189 | 190 | for i in range(num_aliens): 191 | if aliens[i].x + d >= width: 192 | for j in range(num_aliens): 193 | aliens[j].x_dir = -1 194 | aliens[j].shift_down() 195 | 196 | if aliens[i].x <= 0: 197 | for j in range(num_aliens): 198 | aliens[j].x_dir = 1 199 | aliens[j].shift_down() 200 | 201 | try: 202 | if aliens[0].y + d > height: 203 | GameOver() 204 | pygame.display.update() 205 | time.sleep(3) 206 | invasion = True 207 | except Exception as e: 208 | pass 209 | 210 | ship.x += x_move 211 | 212 | if ship.x < 0: 213 | ship.x -= x_move 214 | if ship.x + ship_width > width: 215 | ship.x -= x_move 216 | 217 | ship.draw() 218 | 219 | pygame.display.update() 220 | clock.tick(60) 221 | 222 | # ----------------- Calling the Game Function --------------------- 223 | game() 224 | -------------------------------------------------------------------------------- /Stack_Tower/Stacks.py: -------------------------------------------------------------------------------- 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() 221 | -------------------------------------------------------------------------------- /Stopwatch/Stopwatch.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------------------------- 2 | # 3 | # Jatin Kumar Mandav 4 | # 5 | # Stopwatch Using Pygame 6 | # Pause or Unpause : Space Bar or 'p' 7 | # Reset : 'r' 8 | # Quit : 'q' 9 | # 10 | # Website : https://jatinmandav.wordpress.com 11 | # YouTube : https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ?view_as=subscriber 12 | # 13 | # Facebook : facebook.com/jatinmandav 14 | # Twitter : @jatinmandav 15 | # Gmail : jatinmandav3@gmail.com 16 | # 17 | # ------------------------------------------------------------------------------------------- 18 | 19 | import pygame 20 | import sys 21 | import time 22 | 23 | # Initializing of Pygame 24 | pygame.init() 25 | 26 | width = 200 27 | height = 100 28 | display = pygame.display.set_mode((width, height)) 29 | pygame.display.set_caption(" ") 30 | clock = pygame.time.Clock() 31 | 32 | dark_gray = (23, 32, 42) 33 | white = (230, 230, 230) 34 | 35 | seconds = 0 36 | pause = False 37 | 38 | # Font and Size 39 | font = pygame.font.SysFont("Times New Roman", 24) 40 | 41 | # Close the Window 42 | def close(): 43 | pygame.quit() 44 | sys.exit() 45 | 46 | # Blit time and text to Pygame Window 47 | def showTime(): 48 | hours = seconds/3600 49 | minutes = (seconds/60)%60 50 | sec = seconds%60 51 | 52 | text = font.render("HH MM SS", True, white) 53 | time = font.render(str(hours).zfill(2) + " " + str(minutes).zfill(2) + " " + str(sec).zfill(2), True, white) 54 | display.blit(text, (10, 10)) 55 | display.blit(time, (13, 40)) 56 | 57 | # Pause the Stopwatch 58 | def Pause(): 59 | while pause: 60 | for event in pygame.event.get(): 61 | if event.type == pygame.QUIT: 62 | close() 63 | if event.type == pygame.KEYDOWN: 64 | if event.key == pygame.K_SPACE or pygame.key == pygame.K_p: 65 | stopWatch() 66 | if event.key == pygame.K_r: 67 | reset() 68 | if event.key == pygame.K_q: 69 | close() 70 | 71 | pauseText = font.render("Paused!", True, white) 72 | display.blit(pauseText, (10, height - 35)) 73 | 74 | pygame.display.update() 75 | clock.tick(60) 76 | 77 | # Reset StopWatch 78 | def reset(): 79 | global seconds 80 | seconds = 0 81 | 82 | # StopWatch 83 | def stopWatch(): 84 | tick = True 85 | global seconds, pause 86 | pause = False 87 | while tick: 88 | for event in pygame.event.get(): 89 | if event.type == pygame.QUIT: 90 | close() 91 | if event.type == pygame.KEYDOWN: 92 | if event.key == pygame.K_SPACE or event.key == pygame.K_p: 93 | pause = True 94 | Pause() 95 | if event.key == pygame.K_r: 96 | reset() 97 | if event.key == pygame.K_q: 98 | close() 99 | 100 | display.fill(dark_gray) 101 | showTime() 102 | seconds += 1 103 | 104 | pygame.display.update() 105 | clock.tick(1) 106 | 107 | stopWatch() 108 | 109 | -------------------------------------------------------------------------------- /Tic Tac Toe .py: -------------------------------------------------------------------------------- 1 | # Implementation of Two Player Tic-Tac-Toe game in Python. 2 | 3 | ''' We will make the board using dictionary 4 | in which keys will be the location(i.e : top-left,mid-right,etc.) 5 | and initialliy it's values will be empty space and then after every move 6 | we will change the value according to player's choice of move. ''' 7 | 8 | theBoard = {'1': ' ', '2': ' ', '3': ' ', 9 | '4': ' ', '5': ' ', '6': ' ', 10 | '7': ' ', '8': ' ', '9': ' '} 11 | 12 | board_keys = [] 13 | 14 | for key in theBoard: 15 | board_keys.append(key) 16 | 17 | ''' We will have to print the updated board after every move in the game and 18 | thus we will make a function in which we'll define the printBoard function 19 | so that we can easily print the board everytime by calling this function. ''' 20 | 21 | 22 | def printBoard(board): 23 | print(board['1'] + '|' + board['2'] + '|' + board['3']) 24 | print('-+-+-') 25 | print(board['4'] + '|' + board['5'] + '|' + board['6']) 26 | print('-+-+-') 27 | print(board['7'] + '|' + board['8'] + '|' + board['9']) 28 | 29 | 30 | # Now we'll write the main function which has all the gameplay functionality. 31 | def game(): 32 | turn = 'X' 33 | count = 0 34 | 35 | for i in range(10): 36 | printBoard(theBoard) 37 | print("It's your turn," + turn + ".Move to which place?") 38 | 39 | move = input() 40 | 41 | if theBoard[move] == ' ': 42 | theBoard[move] = turn 43 | count += 1 44 | else: 45 | print("That place is already filled.\nMove to which place?") 46 | continue 47 | 48 | # Now we will check if player X or O has won,for every move after 5 moves. 49 | if count >= 5: 50 | if theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the top 51 | printBoard(theBoard) 52 | print("\nGame Over.\n") 53 | print(" **** " + turn + " won. ****") 54 | break 55 | elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle 56 | printBoard(theBoard) 57 | print("\nGame Over.\n") 58 | print(" **** " + turn + " won. ****") 59 | break 60 | elif theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the bottom 61 | printBoard(theBoard) 62 | print("\nGame Over.\n") 63 | print(" **** " + turn + " won. ****") 64 | break 65 | elif theBoard['7'] == theBoard['4'] == theBoard['1'] != ' ': # down the left side 66 | printBoard(theBoard) 67 | print("\nGame Over.\n") 68 | print(" **** " + turn + " won. ****") 69 | break 70 | elif theBoard['8'] == theBoard['5'] == theBoard['2'] != ' ': # down the middle 71 | printBoard(theBoard) 72 | print("\nGame Over.\n") 73 | print(" **** " + turn + " won. ****") 74 | break 75 | elif theBoard['9'] == theBoard['6'] == theBoard['3'] != ' ': # down the right side 76 | printBoard(theBoard) 77 | print("\nGame Over.\n") 78 | print(" **** " + turn + " won. ****") 79 | break 80 | elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal 81 | printBoard(theBoard) 82 | print("\nGame Over.\n") 83 | print(" **** " + turn + " won. ****") 84 | break 85 | elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal 86 | printBoard(theBoard) 87 | print("\nGame Over.\n") 88 | print(" **** " + turn + " won. ****") 89 | break 90 | 91 | # If neither X nor O wins and the board is full, we'll declare the result as 'tie'. 92 | if count == 9: 93 | print("\nGame Over.\n") 94 | print("It's a Tie!!") 95 | 96 | # Now we have to change the player after every move. 97 | if turn == 'X': 98 | turn = 'O' 99 | else: 100 | turn = 'X' 101 | 102 | # Now we will ask if player wants to restart the game or not. 103 | restart = input("Do want to play Again?(y/n)") 104 | if restart == "y" or restart == "Y": 105 | for key in board_keys: 106 | theBoard[key] = " " 107 | 108 | game() 109 | 110 | 111 | if __name__ == "__main__": 112 | game() -------------------------------------------------------------------------------- /Tron/Tron.py: -------------------------------------------------------------------------------- 1 | # ----------------------------------------------------------------------------- 2 | # 3 | # Tron 4 | # Language - Python 5 | # Modules - pygame, sys 6 | # 7 | # Controls - Arrow Keys for Player 2(Yellow) and WASD Keys for Player 1(Red) 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 | 21 | pygame.init() 22 | 23 | width = 600 24 | height = 600 25 | display = pygame.display.set_mode((width, height)) 26 | pygame.display.set_caption("Tron 2D") 27 | clock = pygame.time.Clock() 28 | 29 | background = (27, 79, 114) 30 | white = (236, 240, 241) 31 | yellow = (241, 196, 15) 32 | darkYellow = (247, 220, 111) 33 | red = (231, 76, 60) 34 | darkRed = (241, 148, 138) 35 | darkBlue = (40, 116, 166) 36 | 37 | font = pygame.font.SysFont("Agency FB", 65) 38 | 39 | w = 10 40 | 41 | # Tron Bike Class 42 | class tronBike: 43 | def __init__(self, number, color, darkColor, side): 44 | self.w = w 45 | self.h = w 46 | self.x = abs(side - 100) 47 | self.y = height/2 - self.h 48 | self.speed = 10 49 | self.color = color 50 | self.darkColor = darkColor 51 | self.history = [[self.x, self.y]] 52 | self.number = number 53 | self.length = 1 54 | 55 | # Draw / Show the Bike 56 | def draw(self): 57 | for i in range(len(self.history)): 58 | if i == self.length - 1: 59 | pygame.draw.rect(display, self.darkColor, (self.history[i][0], self.history[i][1], self.w, self.h)) 60 | else: 61 | pygame.draw.rect(display, self.color, (self.history[i][0], self.history[i][1], self.w, self.h)) 62 | 63 | # Move the Bike 64 | def move(self, xdir, ydir): 65 | self.x += xdir*self.speed 66 | self.y += ydir*self.speed 67 | self.history.append([self.x, self.y]) 68 | self.length += 1 69 | if self.x < 0 or self.x > width or self.y < 0 or self.y > height: 70 | gameOver(self.number) 71 | 72 | # Check if Bike Collides with Route 73 | def checkIfHit(self, opponent): 74 | if abs(opponent.history[opponent.length - 1][0] - self.history[self.length - 1][0]) < self.w and abs(opponent.history[opponent.length - 1][1] - self.history[self.length - 1][1]) < self.h: 75 | gameOver(0) 76 | for i in range(opponent.length): 77 | if abs(opponent.history[i][0] - self.history[self.length - 1][0]) < self.w and abs(opponent.history[i][1] - self.history[self.length - 1][1]) < self.h: 78 | gameOver(self.number) 79 | 80 | for i in range(len(self.history) - 1): 81 | if abs(self.history[i][0] - self.x) < self.w and abs(self.history[i][1] - self.y) < self.h and self.length > 2: 82 | gameOver(self.number) 83 | 84 | def gameOver(number): 85 | while True: 86 | for event in pygame.event.get(): 87 | if event.type == pygame.QUIT: 88 | close() 89 | if event.type == pygame.KEYDOWN: 90 | if event.key == pygame.K_q: 91 | close() 92 | if event.key == pygame.K_r: 93 | tron() 94 | if number == 0: 95 | text = font.render("Both the Players Collided!", True, white) 96 | else: 97 | text = font.render("Player %d Lost the Tron!" %(number), True, white) 98 | 99 | display.blit(text, (50, height/2)) 100 | 101 | pygame.display.update() 102 | clock.tick(60) 103 | 104 | def drawGrid(): 105 | squares = 50 106 | for i in range(width/squares): 107 | pygame.draw.line(display, darkBlue, (i*squares, 0), (i*squares, height)) 108 | pygame.draw.line(display, darkBlue, (0, i*squares), (width, i*squares)) 109 | 110 | def close(): 111 | pygame.quit() 112 | sys.exit() 113 | 114 | 115 | def tron(): 116 | loop = True 117 | 118 | bike1 = tronBike(1, red, darkRed, 0) 119 | bike2 = tronBike(2, yellow, darkYellow, width) 120 | 121 | x1 = 1 122 | y1 = 0 123 | x2 = -1 124 | y2 = 0 125 | 126 | while loop: 127 | for event in pygame.event.get(): 128 | if event.type == pygame.QUIT: 129 | close() 130 | if event.type == pygame.KEYDOWN: 131 | if event.key == pygame.K_q: 132 | close() 133 | if event.key == pygame.K_UP: 134 | if not (x2 == 0 and y2 == 1): 135 | x2 = 0 136 | y2 = -1 137 | if event.key == pygame.K_DOWN: 138 | if not (x2 == 0 and y2 == -1): 139 | x2 = 0 140 | y2 = 1 141 | if event.key == pygame.K_LEFT: 142 | if not (x2 == 1 and y2 == 0): 143 | x2 = -1 144 | y2 = 0 145 | if event.key == pygame.K_RIGHT: 146 | if not (x2 == -1 and y2 == 0): 147 | x2 = 1 148 | y2 = 0 149 | if event.key == pygame.K_w: 150 | if not (x1 == 0 and y1 == 1): 151 | x1 = 0 152 | y1 = -1 153 | if event.key == pygame.K_s: 154 | if not (x1 == 0 and y1 == -1): 155 | x1 = 0 156 | y1 = 1 157 | if event.key == pygame.K_a: 158 | if not (x1 == 1 and y1 == 0): 159 | x1 = -1 160 | y1 = 0 161 | if event.key == pygame.K_d: 162 | if not (x1 == -1 and y1 == 0): 163 | x1 = 1 164 | y1 = 0 165 | 166 | 167 | display.fill(background) 168 | drawGrid() 169 | bike1.draw() 170 | bike2.draw() 171 | 172 | bike1.move(x1, y1) 173 | bike2.move(x2, y2) 174 | 175 | bike1.checkIfHit(bike2) 176 | bike2.checkIfHit(bike1) 177 | 178 | pygame.display.update() 179 | clock.tick(10) 180 | 181 | tron() 182 | --------------------------------------------------------------------------------