Create repo
376
8_Ball_Pool/8BallPool.py
Normal file
@ -0,0 +1,376 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# 8 Ball Pool
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, random, math
|
||||||
|
#
|
||||||
|
# Controls - Mouse, the length of Stick is propotional to Force applied
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
import random
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
width = 660
|
||||||
|
height = 360
|
||||||
|
outerHeight = 400
|
||||||
|
margin = 30
|
||||||
|
display = pygame.display.set_mode((width, outerHeight))
|
||||||
|
pygame.display.set_caption("8 Ball Pool")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
background = (51, 51, 51)
|
||||||
|
white = (236, 240, 241)
|
||||||
|
|
||||||
|
gray = (123, 125, 125)
|
||||||
|
|
||||||
|
black = (23, 32, 42)
|
||||||
|
yellow = (244, 208, 63)
|
||||||
|
blue = (52, 152, 219)
|
||||||
|
red = (203, 67, 53)
|
||||||
|
purple = (136, 78, 160)
|
||||||
|
orange = (230, 126, 34)
|
||||||
|
green = (40, 180, 99)
|
||||||
|
brown = (100, 30, 22)
|
||||||
|
stickColor = (249, 231, 159)
|
||||||
|
|
||||||
|
colors = [yellow, blue, red, purple, orange, green, brown, black, yellow, blue, red, purple, orange, green, brown]
|
||||||
|
|
||||||
|
balls = []
|
||||||
|
noBalls = 15
|
||||||
|
radius = 10
|
||||||
|
friction = 0.005
|
||||||
|
|
||||||
|
# Ball Class
|
||||||
|
class Ball:
|
||||||
|
def __init__(self, x, y, speed, color, angle, ballNum):
|
||||||
|
self.x = x + radius
|
||||||
|
self.y = y + radius
|
||||||
|
self.color = color
|
||||||
|
self.angle = angle
|
||||||
|
self.speed = speed
|
||||||
|
self.ballNum = ballNum
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 10)
|
||||||
|
|
||||||
|
# Draws Balls on Display Window
|
||||||
|
def draw(self, x, y):
|
||||||
|
pygame.draw.ellipse(display, self.color, (x - radius, y - radius, radius*2, radius*2))
|
||||||
|
if self.color == black or self.ballNum == "cue":
|
||||||
|
ballNo = self.font.render(str(self.ballNum), True, white)
|
||||||
|
display.blit(ballNo, (x - 5, y - 5))
|
||||||
|
else:
|
||||||
|
ballNo = self.font.render(str(self.ballNum), True, black)
|
||||||
|
if self.ballNum > 9:
|
||||||
|
display.blit(ballNo, (x - 6, y - 5))
|
||||||
|
else:
|
||||||
|
display.blit(ballNo, (x - 5, y - 5))
|
||||||
|
|
||||||
|
# Moves the Ball around the Screen
|
||||||
|
def move(self):
|
||||||
|
self.speed -= friction
|
||||||
|
if self.speed <= 0:
|
||||||
|
self.speed = 0
|
||||||
|
self.x = self.x + self.speed*cos(radians(self.angle))
|
||||||
|
self.y = self.y + self.speed*sin(radians(self.angle))
|
||||||
|
|
||||||
|
if not (self.x < width - radius - margin):
|
||||||
|
self.x = width - radius - margin
|
||||||
|
self.angle = 180 - self.angle
|
||||||
|
if not(radius + margin < self.x):
|
||||||
|
self.x = radius + margin
|
||||||
|
self.angle = 180 - self.angle
|
||||||
|
if not (self.y < height - radius - margin):
|
||||||
|
self.y = height - radius - margin
|
||||||
|
self.angle = 360 - self.angle
|
||||||
|
if not(radius + margin < self.y):
|
||||||
|
self.y = radius + margin
|
||||||
|
self.angle = 360 - self.angle
|
||||||
|
|
||||||
|
# Pocket Class
|
||||||
|
class Pockets:
|
||||||
|
def __init__(self, x, y, color):
|
||||||
|
self.r = margin/2
|
||||||
|
self.x = x + self.r + 10
|
||||||
|
self.y = y + self.r + 10
|
||||||
|
self.color = color
|
||||||
|
|
||||||
|
# Draws the Pockets on Pygame Window
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.ellipse(display, self.color, (self.x - self.r, self.y - self.r, self.r*2, self.r*2))
|
||||||
|
|
||||||
|
# Checks if ball has entered the Hole
|
||||||
|
def checkPut(self):
|
||||||
|
global balls
|
||||||
|
ballsCopy = balls[:]
|
||||||
|
for i in range(len(balls)):
|
||||||
|
dist = ((self.x - balls[i].x)**2 + (self.y - balls[i].y)**2)**0.5
|
||||||
|
if dist < self.r + radius:
|
||||||
|
if balls[i] in ballsCopy:
|
||||||
|
if balls[i].ballNum == 8:
|
||||||
|
gameOver()
|
||||||
|
else:
|
||||||
|
ballsCopy.remove(balls[i])
|
||||||
|
|
||||||
|
balls = ballsCopy[:]
|
||||||
|
|
||||||
|
# Cue Stick Class
|
||||||
|
class CueStick:
|
||||||
|
def __init__(self, x, y, length, color):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.length = length
|
||||||
|
self.color = color
|
||||||
|
self.tangent = 0
|
||||||
|
|
||||||
|
# Applies force to Cue Ball
|
||||||
|
def applyForce(self, cueBall, force):
|
||||||
|
cueBall.angle = self.tangent
|
||||||
|
cueBall.speed = force
|
||||||
|
|
||||||
|
# Draws Cue Stick on Pygame Window
|
||||||
|
def draw(self, cuex, cuey):
|
||||||
|
self.x, self.y = pygame.mouse.get_pos()
|
||||||
|
self.tangent = (degrees(atan2((cuey - self.y), (cuex - self.x))))
|
||||||
|
pygame.draw.line(display, white, (cuex + self.length*cos(radians(self.tangent)), cuey + self.length*sin(radians(self.tangent))), (cuex, cuey), 1)
|
||||||
|
pygame.draw.line(display, self.color, (self.x, self.y), (cuex, cuey), 3)
|
||||||
|
|
||||||
|
|
||||||
|
# Checks Collision
|
||||||
|
def collision(ball1, ball2):
|
||||||
|
dist = ((ball1.x - ball2.x)**2 + (ball1.y - ball2.y)**2)**0.5
|
||||||
|
if dist <= radius*2:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Checks if Cue Ball hits any Ball
|
||||||
|
def checkCueCollision(cueBall):
|
||||||
|
for i in range(len(balls)):
|
||||||
|
if collision(cueBall, balls[i]):
|
||||||
|
if balls[i].x == cueBall.x:
|
||||||
|
angleIncline = 2*90
|
||||||
|
else:
|
||||||
|
u1 = balls[i].speed
|
||||||
|
u2 = cueBall.speed
|
||||||
|
|
||||||
|
balls[i].speed = ((u1*cos(radians(balls[i].angle)))**2 + (u2*sin(radians(cueBall.angle)))**2)**0.5
|
||||||
|
cueBall.speed = ((u2*cos(radians(cueBall.angle)))**2 + (u1*sin(radians(balls[i].angle)))**2)**0.5
|
||||||
|
|
||||||
|
tangent = degrees((atan((balls[i].y - cueBall.y)/(balls[i].x - cueBall.x)))) + 90
|
||||||
|
angle = tangent + 90
|
||||||
|
|
||||||
|
balls[i].angle = (2*tangent - balls[i].angle)
|
||||||
|
cueBall.angle = (2*tangent - cueBall.angle)
|
||||||
|
|
||||||
|
balls[i].x += (balls[i].speed)*sin(radians(angle))
|
||||||
|
balls[i].y -= (balls[i].speed)*cos(radians(angle))
|
||||||
|
cueBall.x -= (cueBall.speed)*sin(radians(angle))
|
||||||
|
cueBall.y += (cueBall.speed)*cos(radians(angle))
|
||||||
|
|
||||||
|
|
||||||
|
# Checks Collision Between Balls
|
||||||
|
def checkCollision():
|
||||||
|
for i in range(len(balls)):
|
||||||
|
for j in range(len(balls) - 1, i, -1):
|
||||||
|
if collision(balls[i], balls[j]):
|
||||||
|
if balls[i].x == balls[j].x:
|
||||||
|
angleIncline = 2*90
|
||||||
|
else:
|
||||||
|
u1 = balls[i].speed
|
||||||
|
u2 = balls[j].speed
|
||||||
|
|
||||||
|
balls[i].speed = ((u1*cos(radians(balls[i].angle)))**2 + (u2*sin(radians(balls[j].angle)))**2)**0.5
|
||||||
|
balls[j].speed = ((u2*cos(radians(balls[j].angle)))**2 + (u1*sin(radians(balls[i].angle)))**2)**0.5
|
||||||
|
|
||||||
|
tangent = degrees((atan((balls[i].y - balls[j].y)/(balls[i].x - balls[j].x)))) + 90
|
||||||
|
angle = tangent + 90
|
||||||
|
|
||||||
|
balls[i].angle = (2*tangent - balls[i].angle)
|
||||||
|
balls[j].angle = (2*tangent - balls[j].angle)
|
||||||
|
|
||||||
|
balls[i].x += (balls[i].speed)*sin(radians(angle))
|
||||||
|
balls[i].y -= (balls[i].speed)*cos(radians(angle))
|
||||||
|
balls[j].x -= (balls[j].speed)*sin(radians(angle))
|
||||||
|
balls[j].y += (balls[j].speed)*cos(radians(angle))
|
||||||
|
|
||||||
|
def border():
|
||||||
|
pygame.draw.rect(display, gray, (0, 0, width, 30))
|
||||||
|
pygame.draw.rect(display, gray, (0, 0, 30, height))
|
||||||
|
pygame.draw.rect(display, gray, (width - 30, 0, width, height))
|
||||||
|
pygame.draw.rect(display, gray, (0, height - 30, width, height))
|
||||||
|
|
||||||
|
def score():
|
||||||
|
font = pygame.font.SysFont("Agency FB", 30)
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (51, 51, 51), (0, height, width, outerHeight))
|
||||||
|
for i in range(len(balls)):
|
||||||
|
balls[i].draw((i + 1)*2*(radius + 1), height + radius + 10)
|
||||||
|
|
||||||
|
text = font.render("Remaining Balls: " + str(len(balls)), True, stickColor)
|
||||||
|
display.blit(text, (width/2 + 50, height + radius/2))
|
||||||
|
|
||||||
|
|
||||||
|
def reset():
|
||||||
|
global balls, noBalls
|
||||||
|
noBalls = 15
|
||||||
|
balls = []
|
||||||
|
|
||||||
|
s = 70
|
||||||
|
|
||||||
|
b1 = Ball(s, height/2 - 4*radius, 0, colors[0], 0, 1)
|
||||||
|
b2 = Ball(s + 2*radius, height/2 - 3*radius, 0, colors[1], 0, 2)
|
||||||
|
b3 = Ball(s, height/2 - 2*radius, 0, colors[2], 0, 3)
|
||||||
|
b4 = Ball(s + 4*radius, height/2 - 2*radius, 0, colors[3], 0, 4)
|
||||||
|
b5 = Ball(s + 2*radius, height/2 - 1*radius, 0, colors[4], 0, 5)
|
||||||
|
b6 = Ball(s, height/2, 0, colors[5], 0, 6)
|
||||||
|
b7 = Ball(s + 6*radius, height/2 - 1*radius, 0, colors[6], 0, 7)
|
||||||
|
b8 = Ball(s + 4*radius, height/2, 0, colors[7], 0, 8)
|
||||||
|
b9 = Ball(s + 8*radius, height/2, 0, colors[8], 0, 9)
|
||||||
|
b10 = Ball(s + 6*radius, height/2 + 1*radius, 0, colors[9], 0, 10)
|
||||||
|
b11 = Ball(s + 2*radius, height/2 + 1*radius, 0, colors[10], 0, 11)
|
||||||
|
b12 = Ball(s, height/2 + 2*radius, 0, colors[11], 0, 12)
|
||||||
|
b13 = Ball(s + 4*radius, height/2 + 2*radius, 0, colors[12], 0, 13)
|
||||||
|
b14 = Ball(s + 2*radius, height/2 + 3*radius, 0, colors[13], 0, 14)
|
||||||
|
b15 = Ball(s, height/2 + 4*radius, 0, colors[14], 0, 15)
|
||||||
|
|
||||||
|
balls.append(b1)
|
||||||
|
balls.append(b2)
|
||||||
|
balls.append(b3)
|
||||||
|
balls.append(b4)
|
||||||
|
balls.append(b5)
|
||||||
|
balls.append(b6)
|
||||||
|
balls.append(b7)
|
||||||
|
balls.append(b8)
|
||||||
|
balls.append(b9)
|
||||||
|
balls.append(b10)
|
||||||
|
balls.append(b11)
|
||||||
|
balls.append(b12)
|
||||||
|
balls.append(b13)
|
||||||
|
balls.append(b14)
|
||||||
|
balls.append(b15)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def gameOver():
|
||||||
|
font = pygame.font.SysFont("Agency FB", 75)
|
||||||
|
if len(balls) == 0:
|
||||||
|
text = font.render("You Won!", True, (133, 193, 233))
|
||||||
|
else:
|
||||||
|
text = font.render("You Lost! Black in Hole!", True, (241, 148, 138))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
poolTable()
|
||||||
|
display.blit(text, (50, height/2))
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick()
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# Main Function
|
||||||
|
def poolTable():
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
reset()
|
||||||
|
|
||||||
|
noPockets = 6
|
||||||
|
pockets = []
|
||||||
|
|
||||||
|
p1 = Pockets(0, 0, black)
|
||||||
|
p2 = Pockets(width/2 - p1.r*2, 0, black)
|
||||||
|
p3 = Pockets(width - p1.r - margin - 5, 0, black)
|
||||||
|
p4 = Pockets(0, height - margin - 5 - p1.r, black)
|
||||||
|
p5 = Pockets(width/2 - p1.r*2, height - margin - 5 - p1.r, black)
|
||||||
|
p6 = Pockets(width - p1.r - margin - 5, height - margin - 5 - p1.r, black)
|
||||||
|
|
||||||
|
pockets.append(p1)
|
||||||
|
pockets.append(p2)
|
||||||
|
pockets.append(p3)
|
||||||
|
pockets.append(p4)
|
||||||
|
pockets.append(p5)
|
||||||
|
pockets.append(p6)
|
||||||
|
|
||||||
|
cueBall = Ball(width/2, height/2, 0, white, 0, "cue")
|
||||||
|
cueStick = CueStick(0, 0, 100, stickColor)
|
||||||
|
|
||||||
|
|
||||||
|
start = 0
|
||||||
|
end = 0
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
poolTable()
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
start = [cueBall.x, cueBall.y]
|
||||||
|
x, y = pygame.mouse.get_pos()
|
||||||
|
end = [x ,y]
|
||||||
|
dist = ((start[0] - end[0])**2 + (start[1] - end[1])**2)**0.5
|
||||||
|
force = dist/10.0
|
||||||
|
if force > 10:
|
||||||
|
force = 10
|
||||||
|
|
||||||
|
cueStick.applyForce(cueBall, force)
|
||||||
|
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
|
||||||
|
cueBall.draw(cueBall.x, cueBall.y)
|
||||||
|
cueBall.move()
|
||||||
|
|
||||||
|
if not (cueBall.speed > 0):
|
||||||
|
|
||||||
|
cueStick.draw(cueBall.x, cueBall.y)
|
||||||
|
|
||||||
|
for i in range(len(balls)):
|
||||||
|
balls[i].draw(balls[i].x, balls[i].y)
|
||||||
|
|
||||||
|
for i in range(len(balls)):
|
||||||
|
balls[i].move()
|
||||||
|
|
||||||
|
checkCollision()
|
||||||
|
checkCueCollision(cueBall)
|
||||||
|
border()
|
||||||
|
|
||||||
|
for i in range(noPockets):
|
||||||
|
pockets[i].draw()
|
||||||
|
|
||||||
|
for i in range(noPockets):
|
||||||
|
pockets[i].checkPut()
|
||||||
|
|
||||||
|
if len(balls) == 1 and balls[0].ballNum == 8:
|
||||||
|
gameOver()
|
||||||
|
|
||||||
|
score()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
poolTable()
|
BIN
Angry_Birds/Fonts/Comic_Kings.ttf
Normal file
BIN
Angry_Birds/Fonts/arfmoochikncheez.ttf
Normal file
BIN
Angry_Birds/Images/bird.png
Normal file
After Width: | Height: | Size: 4.5 KiB |
BIN
Angry_Birds/Images/block1.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
Angry_Birds/Images/block_destroyed1.png
Normal file
After Width: | Height: | Size: 9.9 KiB |
BIN
Angry_Birds/Images/game_play1.png
Normal file
After Width: | Height: | Size: 217 KiB |
BIN
Angry_Birds/Images/game_play2.png
Normal file
After Width: | Height: | Size: 203 KiB |
BIN
Angry_Birds/Images/pig1.png
Normal file
After Width: | Height: | Size: 3.4 KiB |
BIN
Angry_Birds/Images/pig3.png
Normal file
After Width: | Height: | Size: 4.5 KiB |
BIN
Angry_Birds/Images/pig_damaged.png
Normal file
After Width: | Height: | Size: 5.3 KiB |
BIN
Angry_Birds/Images/wall_horizontal.png
Normal file
After Width: | Height: | Size: 2.6 KiB |
BIN
Angry_Birds/Images/wall_vertical.png
Normal file
After Width: | Height: | Size: 2.8 KiB |
32
Angry_Birds/README.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Angry Birds
|
||||||
|
|
||||||
|
Here is a Small Attempt to Recreate One of the popular Games, Angry Birds in Python using Pygame
|
||||||
|
|
||||||
|
Link to 14 Hour Time Lapse of coding the game from Scratch: [Angry Birds - 14 Hour Time Lapse](https://youtu.be/6in-mdiumcA)
|
||||||
|
|
||||||
|
Link to Blog: [Angry Birds in Python Using PyGame](https://jatinmandav.wordpress.com/2018/05/25/angry-birds-in-python-using-pygame/)
|
||||||
|
|
||||||
|
### Requirements:
|
||||||
|
|
||||||
|
[Pygame Module](https://www.pygame.org)
|
||||||
|
|
||||||
|
pip install pygame
|
||||||
|
|
||||||
|
|
||||||
|
## Usage:
|
||||||
|
|
||||||
|
wget https://github.com/jatinmandav/Gaming-in-Python/tree/master/Angry_Birds
|
||||||
|
|
||||||
|
From Angry_Birds/ Directory:
|
||||||
|
|
||||||
|
$ python3 main.py or
|
||||||
|
|
||||||
|
$ python main.py
|
||||||
|
|
||||||
|
|
||||||
|
### Game Play Screenshot:
|
||||||
|
|
||||||
|
<p align="center"> <img src="Images/game_play1.png"/> </p>
|
||||||
|
|
||||||
|
|
||||||
|
<p align="center"> <img src="Images/game_play2.png"/> </p>
|
BIN
Angry_Birds/__pycache__/interface.cpython-310.pyc
Normal file
BIN
Angry_Birds/__pycache__/interface.cpython-35.pyc
Normal file
BIN
Angry_Birds/__pycache__/maps.cpython-310.pyc
Normal file
BIN
Angry_Birds/__pycache__/maps.cpython-35.pyc
Normal file
BIN
Angry_Birds/__pycache__/objects.cpython-310.pyc
Normal file
BIN
Angry_Birds/__pycache__/objects.cpython-35.pyc
Normal file
BIN
Angry_Birds/__pycache__/physics_engine.cpython-310.pyc
Normal file
BIN
Angry_Birds/__pycache__/physics_engine.cpython-35.pyc
Normal file
71
Angry_Birds/interface.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
'''
|
||||||
|
|
||||||
|
Game: Angry Birds
|
||||||
|
File: interface.py
|
||||||
|
|
||||||
|
Contents: Class Buttons and Class Labels for User Interface
|
||||||
|
|
||||||
|
Requirements: Pygame, sys
|
||||||
|
|
||||||
|
By: Jatin Kumar Mandav
|
||||||
|
|
||||||
|
Blog: https://www.jatinmandav.wordpress.com
|
||||||
|
Twitter: @jatinmandav
|
||||||
|
YouTube: https://www.youtube.com/mandav
|
||||||
|
|
||||||
|
'''
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
display = None
|
||||||
|
|
||||||
|
def init(screen):
|
||||||
|
global display
|
||||||
|
display = screen
|
||||||
|
|
||||||
|
class Button:
|
||||||
|
def __init__(self, x, y, w, h, action=None, colorNotActive=(189, 195, 199), colorActive=None):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
|
||||||
|
self.colorActive = colorActive
|
||||||
|
self.colorNotActive = colorNotActive
|
||||||
|
|
||||||
|
self.action = action
|
||||||
|
|
||||||
|
self.font = None
|
||||||
|
self.text = None
|
||||||
|
self.text_pos = None
|
||||||
|
|
||||||
|
def add_text(self, text, size=20, font="Times New Roman", text_color=(0, 0, 0)):
|
||||||
|
self.font = pygame.font.Font(font, size)
|
||||||
|
self.text = self.font.render(text, True, text_color)
|
||||||
|
self.text_pos = self.text.get_rect()
|
||||||
|
|
||||||
|
self.text_pos.center = (self.x + self.w/2, self.y + self.h/2)
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
if self.isActive():
|
||||||
|
if not self.colorActive == None:
|
||||||
|
pygame.draw.rect(display, self.colorActive, (self.x, self.y, self.w, self.h))
|
||||||
|
else:
|
||||||
|
pygame.draw.rect(display, self.colorNotActive, (self.x, self.y, self.w, self.h))
|
||||||
|
|
||||||
|
if self.text:
|
||||||
|
display.blit(self.text, self.text_pos)
|
||||||
|
|
||||||
|
def isActive(self):
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
if (self.x < pos[0] < self.x + self.w) and (self.y < pos[1] < self.y + self.h):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
class Label(Button):
|
||||||
|
def draw(self):
|
||||||
|
if self.text:
|
||||||
|
display.blit(self.text, self.text_pos)
|
91
Angry_Birds/main.py
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
'''
|
||||||
|
|
||||||
|
Game: Angry Birds
|
||||||
|
File: main.py
|
||||||
|
|
||||||
|
Contents: The Main file to Start the Game!
|
||||||
|
|
||||||
|
Requirements: Pygame, sys, random, math
|
||||||
|
Supporting Modules: interface.py, physics_engine.py, maps.py, objects.py
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
Angry_Birds/$ python3 main.py or
|
||||||
|
Angry_Birds/$ python main.py
|
||||||
|
|
||||||
|
By: Jatin Kumar Mandav
|
||||||
|
|
||||||
|
Blog: https://www.jatinmandav.wordpress.com
|
||||||
|
Twitter: @jatinmandav
|
||||||
|
YouTube: https://www.youtube.com/mandav
|
||||||
|
|
||||||
|
'''
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
from math import *
|
||||||
|
|
||||||
|
import physics_engine
|
||||||
|
import objects
|
||||||
|
import maps
|
||||||
|
import interface
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
width = 1800
|
||||||
|
height = 700
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
physics_engine.init(display)
|
||||||
|
objects.init(display)
|
||||||
|
maps.init(display)
|
||||||
|
interface.init(display)
|
||||||
|
|
||||||
|
background = (51, 51, 51)
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
def start_game(map):
|
||||||
|
map.draw_map()
|
||||||
|
|
||||||
|
def GAME():
|
||||||
|
map = maps.Maps()
|
||||||
|
|
||||||
|
welcome = interface.Label(700, 100, 400, 200, None, background)
|
||||||
|
welcome.add_text("ANGRY BIRDS", 80, "Fonts/arfmoochikncheez.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
start = interface.Button(500, 400, 300, 100, start_game, (244, 208, 63), (247, 220, 111))
|
||||||
|
start.add_text("START GAME", 60, "Fonts/arfmoochikncheez.ttf", background)
|
||||||
|
|
||||||
|
exit = interface.Button(1000, 400, 300, 100, close, (241, 148, 138), (245, 183, 177))
|
||||||
|
exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", background)
|
||||||
|
|
||||||
|
mandav = interface.Button(width - 300, height - 80, 300, 100, None, background)
|
||||||
|
mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", (41, 41, 41))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
if exit.isActive():
|
||||||
|
exit.action()
|
||||||
|
if start.isActive():
|
||||||
|
start_game(map)
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
|
||||||
|
start.draw()
|
||||||
|
exit.draw()
|
||||||
|
welcome.draw()
|
||||||
|
mandav.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
GAME()
|
645
Angry_Birds/maps.py
Normal file
@ -0,0 +1,645 @@
|
|||||||
|
'''
|
||||||
|
|
||||||
|
Game: Angry Birds
|
||||||
|
File: maps.py
|
||||||
|
|
||||||
|
Contents: Class MAPS, that puts everything in action!
|
||||||
|
|
||||||
|
Requirements: Pygame, sys
|
||||||
|
Supporting Modules: physics_engine, interface, objects
|
||||||
|
|
||||||
|
By: Jatin Kumar Mandav
|
||||||
|
|
||||||
|
Blog: https://www.jatinmandav.wordpress.com
|
||||||
|
Twitter: @jatinmandav
|
||||||
|
YouTube: https://www.youtube.com/mandav
|
||||||
|
|
||||||
|
'''
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import physics_engine
|
||||||
|
import objects
|
||||||
|
import interface
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
width = None
|
||||||
|
height = None
|
||||||
|
display = None
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
ground = 50
|
||||||
|
|
||||||
|
d_velocity = 2.0
|
||||||
|
|
||||||
|
def init(screen):
|
||||||
|
global width, height, display
|
||||||
|
display = screen
|
||||||
|
(width, height) = display.get_rect().size
|
||||||
|
height -= ground
|
||||||
|
interface.init(display)
|
||||||
|
|
||||||
|
def all_rest(pigs, birds, blocks):
|
||||||
|
threshold = 0.15
|
||||||
|
for pig in pigs:
|
||||||
|
if pig.velocity.magnitude >= threshold:
|
||||||
|
return False
|
||||||
|
|
||||||
|
for bird in birds:
|
||||||
|
if bird.velocity.magnitude >= threshold:
|
||||||
|
return False
|
||||||
|
|
||||||
|
for block in blocks:
|
||||||
|
if block.velocity.magnitude >= threshold:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
class Maps:
|
||||||
|
def __init__(self):
|
||||||
|
self.level = 1
|
||||||
|
self.max_level = 15
|
||||||
|
self.color = {'background': (51, 51, 51)}
|
||||||
|
self.score = 0
|
||||||
|
|
||||||
|
def wait_level(self):
|
||||||
|
time = 0
|
||||||
|
while time < 3:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
time += 1
|
||||||
|
clock.tick(1)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
def check_win(self, pigs, birds):
|
||||||
|
if pigs == []:
|
||||||
|
print("WON!")
|
||||||
|
return True
|
||||||
|
if (not pigs == []) and birds == []:
|
||||||
|
print("LOST!")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def pause(self):
|
||||||
|
pause_text = interface.Label(700, 200, 400, 200, None, self.color['background'])
|
||||||
|
pause_text.add_text("GAME PAUSED", 70, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
replay = interface.Button(350, 500, 300, 100, self.draw_map, (244, 208, 63), (247, 220, 111))
|
||||||
|
replay.add_text("RESTART", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
resume = interface.Button(750, 500, 300, 100, None, (88, 214, 141), (171, 235, 198))
|
||||||
|
resume.add_text("RESUME", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
exit = interface.Button(1150, 500, 300, 100, close, (241, 148, 138), (245, 183, 177))
|
||||||
|
exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background'])
|
||||||
|
mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 ))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_p:
|
||||||
|
return
|
||||||
|
if event.key == pygame.K_ESCAPE:
|
||||||
|
return
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
if replay.isActive():
|
||||||
|
replay.action()
|
||||||
|
if resume.isActive():
|
||||||
|
return
|
||||||
|
if exit.isActive():
|
||||||
|
exit.action()
|
||||||
|
|
||||||
|
replay.draw()
|
||||||
|
resume.draw()
|
||||||
|
exit.draw()
|
||||||
|
pause_text.draw()
|
||||||
|
mandav.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
def draw_map(self):
|
||||||
|
birds = []
|
||||||
|
pigs = []
|
||||||
|
blocks = []
|
||||||
|
walls = []
|
||||||
|
self.score = 0
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 40, 20))
|
||||||
|
pigs.append(physics_engine.Pig(1500, height - 40, 20))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1300, height - 60, 60))
|
||||||
|
|
||||||
|
elif self.level == 2:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1000, height - 40, 20))
|
||||||
|
pigs.append(physics_engine.Pig(1400, height - 40, 20))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1200, height - 60, 60))
|
||||||
|
blocks.append(physics_engine.Block(1200, height - 2*35, 60))
|
||||||
|
blocks.append(physics_engine.Block(1500, height - 60, 60))
|
||||||
|
|
||||||
|
elif self.level == 3:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1200, height - 60, 30))
|
||||||
|
pigs.append(physics_engine.Pig(1300, height - 60, 30))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1000, height - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(1000, height - 2*60, 100))
|
||||||
|
blocks.append(physics_engine.Block(1500, height - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(1500, height - 2*60, 100))
|
||||||
|
|
||||||
|
elif self.level == 4:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1200, 500 - 60, 30))
|
||||||
|
pigs.append(physics_engine.Pig(1300, height - 60, 30))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(1000, 450, 500, 20))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1100, height - 100, 100))
|
||||||
|
|
||||||
|
elif self.level == 5:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1300, 500 - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1300, height - 60, 25))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(500, 400, 100, height - 400))
|
||||||
|
walls.append(objects.Slab(1000, 450, 500, 30))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1150, 500 - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(1100, height - 100, 100))
|
||||||
|
|
||||||
|
elif self.level == 6:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1300, 500 - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1300, height - 60, 25))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(1000, 0, 30, 450))
|
||||||
|
walls.append(objects.Slab(1000, 450, 500, 30))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1150, 500 - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(1100, height - 100, 100))
|
||||||
|
|
||||||
|
elif self.level == 7:
|
||||||
|
for i in range(4):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, 500 - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1300, 500 - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1200, height - 60, 25))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(1200, 250, 30, 200))
|
||||||
|
walls.append(objects.Slab(1000, 450, 500, 30))
|
||||||
|
|
||||||
|
elif self.level == 8:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1200, height - 60, 25))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(700, 250, 30, height - 250))
|
||||||
|
|
||||||
|
elif self.level == 9:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1450, height - 60, 25))
|
||||||
|
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1250, height - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(1250, height - 2*60, 100))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(700, 400, 30, height - 400))
|
||||||
|
|
||||||
|
elif self.level == 10:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1450, height - 60, 25))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1250, height - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(1250, height - 2*60, 100))
|
||||||
|
blocks.append(physics_engine.Block(900, height - 100, 100))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(900, 400, 500, 30))
|
||||||
|
|
||||||
|
elif self.level == 11:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1450, height - 60, 25))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1250, height - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(1250, height - 2*60, 100))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(900, 400, 500, 30))
|
||||||
|
walls.append(objects.Slab(900, 400, 30, height - 400))
|
||||||
|
|
||||||
|
elif self.level == 12:
|
||||||
|
for i in range(3):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1450, height - 60, 25))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(900, 400, 500, 30))
|
||||||
|
walls.append(objects.Slab(1200, 500, 30, height - 500))
|
||||||
|
|
||||||
|
elif self.level == 13:
|
||||||
|
for i in range(4):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1200, 400 - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1450, height - 60, 25))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(900, height - 100, 100))
|
||||||
|
blocks.append(physics_engine.Block(900, height - 2*60, 100))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(900, 400, 500, 40))
|
||||||
|
walls.append(objects.Slab(1200, 500, 30, height - 500))
|
||||||
|
|
||||||
|
elif self.level == 14:
|
||||||
|
for i in range(4):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(1100, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1100, 400 - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1450, height - 60, 25))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(900, height - 100, 100))
|
||||||
|
|
||||||
|
blocks.append(physics_engine.Block(1300, 400 - 100, 100))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(900, 400, 500, 40))
|
||||||
|
walls.append(objects.Slab(900, 0, 30, 400))
|
||||||
|
|
||||||
|
elif self.level == 15:
|
||||||
|
for i in range(5):
|
||||||
|
new_bird = physics_engine.Bird(40*i + 5*i, height - 40, 20, None, "BIRD")
|
||||||
|
birds.append(new_bird)
|
||||||
|
|
||||||
|
pigs.append(physics_engine.Pig(900, height - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(width - 400, 400 - 60, 25))
|
||||||
|
pigs.append(physics_engine.Pig(1700, height - 60, 25))
|
||||||
|
|
||||||
|
|
||||||
|
walls.append(objects.Slab(800, 400, 30, height - 400))
|
||||||
|
walls.append(objects.Slab(1000, 500, 30, height - 500))
|
||||||
|
|
||||||
|
walls.append(objects.Slab(width - 500, 400, 500, 40))
|
||||||
|
walls.append(objects.Slab(width - 500, 150, 60, 400 - 150))
|
||||||
|
|
||||||
|
|
||||||
|
self.start_level(birds, pigs, blocks, walls)
|
||||||
|
|
||||||
|
def replay_level(self):
|
||||||
|
self.level -= 1
|
||||||
|
self.draw_map()
|
||||||
|
|
||||||
|
def start_again(self):
|
||||||
|
self.level = 1
|
||||||
|
self.draw_map()
|
||||||
|
|
||||||
|
def level_cleared(self):
|
||||||
|
self.level += 1
|
||||||
|
|
||||||
|
level_cleared_text = interface.Label(700, 100, 400, 200, None, self.color['background'])
|
||||||
|
if self.level <= self.max_level:
|
||||||
|
level_cleared_text.add_text("LEVEL " + str(self.level - 1) + " CLEARED!", 80, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
else:
|
||||||
|
level_cleared_text.add_text("ALL LEVEL CLEARED!", 80, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
score_text = interface.Label(750, 300, 300, 100, None, self.color['background'])
|
||||||
|
score_text.add_text("SCORE: " + str(self.score), 55, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
replay = interface.Button(350, 500, 300, 100, self.replay_level, (244, 208, 63), (247, 220, 111))
|
||||||
|
replay.add_text("PLAY AGAIN", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
if self.level <= self.max_level:
|
||||||
|
next = interface.Button(750, 500, 300, 100, self.draw_map, (88, 214, 141), (171, 235, 198))
|
||||||
|
next.add_text("CONTINUE", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
else:
|
||||||
|
next = interface.Button(750, 500, 300, 100, self.start_again, (88, 214, 141), (171, 235, 198))
|
||||||
|
next.add_text("START AGAIN", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
exit = interface.Button(1150, 500, 300, 100, close, (241, 148, 138), (245, 183, 177))
|
||||||
|
exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background'])
|
||||||
|
mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 ))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
if replay.isActive():
|
||||||
|
replay.action()
|
||||||
|
if next.isActive():
|
||||||
|
next.action()
|
||||||
|
if exit.isActive():
|
||||||
|
exit.action()
|
||||||
|
|
||||||
|
replay.draw()
|
||||||
|
next.draw()
|
||||||
|
exit.draw()
|
||||||
|
level_cleared_text.draw()
|
||||||
|
score_text.draw()
|
||||||
|
mandav.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
def level_failed(self):
|
||||||
|
level_failed_text = interface.Label(700, 100, 400, 200, None, self.color['background'])
|
||||||
|
level_failed_text.add_text("LEVEL FAILED!", 80, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
score_text = interface.Label(750, 300, 300, 100, None, self.color['background'])
|
||||||
|
score_text.add_text("SCORE: " + str(self.score), 55, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
replay = interface.Button(500, 500, 300, 100, self.draw_map, (244, 208, 63), (247, 220, 111))
|
||||||
|
replay.add_text("TRY AGAIN", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
exit = interface.Button(1000, 500, 300, 100, close, (241, 148, 138), (245, 183, 177))
|
||||||
|
exit.add_text("QUIT", 60, "Fonts/arfmoochikncheez.ttf", self.color['background'])
|
||||||
|
|
||||||
|
mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background'])
|
||||||
|
mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 ))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
if replay.isActive():
|
||||||
|
replay.action()
|
||||||
|
if exit.isActive():
|
||||||
|
exit.action()
|
||||||
|
|
||||||
|
replay.draw()
|
||||||
|
exit.draw()
|
||||||
|
level_failed_text.draw()
|
||||||
|
score_text.draw()
|
||||||
|
mandav.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
def start_level(self, birds, pigs, blocks, walls):
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
slingshot = physics_engine.Slingshot(200, height - 200, 30, 200)
|
||||||
|
|
||||||
|
birds[0].load(slingshot)
|
||||||
|
|
||||||
|
mouse_click = False
|
||||||
|
flag = 1
|
||||||
|
|
||||||
|
pigs_to_remove = []
|
||||||
|
blocks_to_remove = []
|
||||||
|
|
||||||
|
score_text = interface.Label(50, 10, 100, 50, None, self.color['background'])
|
||||||
|
score_text.add_text("SCORE: " + str(self.score), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
birds_remaining = interface.Label(120, 50, 100, 50, None, self.color['background'])
|
||||||
|
birds_remaining.add_text("BIRDS REMAINING: " + str(len(birds)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
pigs_remaining = interface.Label(110, 90, 100, 50, None, self.color['background'])
|
||||||
|
pigs_remaining.add_text("PIGS REMAINING: " + str(len(pigs)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
|
||||||
|
mandav = interface.Label(width - 270, height + ground - 70, 300, 100, None, self.color['background'])
|
||||||
|
mandav.add_text("MANDAV", 60, "Fonts/arfmoochikncheez.ttf", ( 113, 125, 126 ))
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
self.draw_map()
|
||||||
|
if event.key == pygame.K_p:
|
||||||
|
self.pause()
|
||||||
|
if event.key == pygame.K_ESCAPE:
|
||||||
|
self.pause()
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
if birds[0].mouse_selected():
|
||||||
|
mouse_click = True
|
||||||
|
if event.type == pygame.MOUSEBUTTONUP:
|
||||||
|
mouse_click = False
|
||||||
|
if birds[0].mouse_selected():
|
||||||
|
flag = 0
|
||||||
|
|
||||||
|
if (not birds[0].loaded) and all_rest(pigs, birds, blocks):
|
||||||
|
print("LOADED!")
|
||||||
|
birds.pop(0)
|
||||||
|
if self.check_win(pigs, birds) == 1:
|
||||||
|
self.score += len(birds)*100
|
||||||
|
self.level_cleared()
|
||||||
|
elif self.check_win(pigs,birds) == 0:
|
||||||
|
self.level_failed()
|
||||||
|
|
||||||
|
if not birds == []:
|
||||||
|
birds[0].load(slingshot)
|
||||||
|
flag = 1
|
||||||
|
|
||||||
|
if mouse_click:
|
||||||
|
birds[0].reposition(slingshot, mouse_click)
|
||||||
|
|
||||||
|
if not flag:
|
||||||
|
birds[0].unload()
|
||||||
|
|
||||||
|
#display.fill(self.color['background'])
|
||||||
|
color = self.color['background']
|
||||||
|
for i in range(3):
|
||||||
|
color = (color[0] + 5, color[1] + 5, color[2] + 5)
|
||||||
|
pygame.draw.rect(display, color, (0, i*300, width, 300))
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (77, 86, 86), (0, height, width, 50))
|
||||||
|
|
||||||
|
|
||||||
|
slingshot.draw(birds[0])
|
||||||
|
|
||||||
|
for i in range(len(pigs)):
|
||||||
|
for j in range(len(blocks)):
|
||||||
|
pig_v, block_v = pigs[i].velocity.magnitude, blocks[j].velocity.magnitude
|
||||||
|
pigs[i], blocks[j], result_block_pig = physics_engine.collision_handler(pigs[i], blocks[j], "BALL_N_BLOCK")
|
||||||
|
pig_v1, block_v1 = pigs[i].velocity.magnitude, blocks[j].velocity.magnitude
|
||||||
|
|
||||||
|
if result_block_pig:
|
||||||
|
if abs(pig_v - pig_v1) > d_velocity:
|
||||||
|
blocks_to_remove.append(blocks[j])
|
||||||
|
blocks[j].destroy()
|
||||||
|
if abs(block_v - block_v1) > d_velocity:
|
||||||
|
pigs_to_remove.append(pigs[i])
|
||||||
|
pigs[i].dead()
|
||||||
|
|
||||||
|
for i in range(len(birds)):
|
||||||
|
if not (birds[i].loaded or birds[i].velocity.magnitude == 0):
|
||||||
|
for j in range(len(blocks)):
|
||||||
|
birds_v, block_v = birds[i].velocity.magnitude, blocks[j].velocity.magnitude
|
||||||
|
birds[i], blocks[j], result_bird_block = physics_engine.collision_handler(birds[i], blocks[j], "BALL_N_BLOCK")
|
||||||
|
birds_v1, block_v1 = birds[i].velocity.magnitude, blocks[j].velocity.magnitude
|
||||||
|
|
||||||
|
if result_bird_block:
|
||||||
|
if abs(birds_v - birds_v1) > d_velocity:
|
||||||
|
if not blocks[j] in blocks_to_remove:
|
||||||
|
blocks_to_remove.append(blocks[j])
|
||||||
|
blocks[j].destroy()
|
||||||
|
|
||||||
|
for i in range(len(pigs)):
|
||||||
|
pigs[i].move()
|
||||||
|
for j in range(i+1, len(pigs)):
|
||||||
|
pig1_v, pig2_v = pigs[i].velocity.magnitude, pigs[j].velocity.magnitude
|
||||||
|
pigs[i], pigs[j], result = physics_engine.collision_handler(pigs[i], pigs[j], "BALL")
|
||||||
|
pig1_v1, pig2_v1 = pigs[i].velocity.magnitude, pigs[j].velocity.magnitude
|
||||||
|
result = True
|
||||||
|
if result:
|
||||||
|
if abs(pig1_v - pig1_v1) > d_velocity:
|
||||||
|
if not pigs[j] in pigs_to_remove:
|
||||||
|
pigs_to_remove.append(pigs[j])
|
||||||
|
pigs[j].dead()
|
||||||
|
if abs(pig2_v - pig2_v1) > d_velocity:
|
||||||
|
if not pigs[i] in pigs_to_remove:
|
||||||
|
pigs_to_remove.append(pigs[i])
|
||||||
|
pigs[i].dead()
|
||||||
|
|
||||||
|
for wall in walls:
|
||||||
|
pigs[i] = wall.collision_manager(pigs[i])
|
||||||
|
|
||||||
|
pigs[i].draw()
|
||||||
|
|
||||||
|
for i in range(len(birds)):
|
||||||
|
if (not birds[i].loaded) and birds[i].velocity.magnitude:
|
||||||
|
birds[0].move()
|
||||||
|
for j in range(len(pigs)):
|
||||||
|
bird_v, pig_v = birds[i].velocity.magnitude, pigs[j].velocity.magnitude
|
||||||
|
birds[i], pigs[j], result_bird_pig = physics_engine.collision_handler(birds[i], pigs[j], "BALL")
|
||||||
|
bird_v1, pig_v1 = birds[i].velocity.magnitude, pigs[j].velocity.magnitude
|
||||||
|
result = True
|
||||||
|
if result_bird_pig:
|
||||||
|
if abs(bird_v - bird_v1) > d_velocity:
|
||||||
|
if not pigs[j] in pigs_to_remove:
|
||||||
|
pigs_to_remove.append(pigs[j])
|
||||||
|
pigs[j].dead()
|
||||||
|
|
||||||
|
if birds[i].loaded:
|
||||||
|
birds[i].project_path()
|
||||||
|
|
||||||
|
for wall in walls:
|
||||||
|
birds[i] = wall.collision_manager(birds[i])
|
||||||
|
|
||||||
|
birds[i].draw()
|
||||||
|
|
||||||
|
for i in range(len(blocks)):
|
||||||
|
for j in range(i + 1, len(blocks)):
|
||||||
|
block1_v, block2_v = blocks[i].velocity.magnitude, blocks[j].velocity.magnitude
|
||||||
|
blocks[i], blocks[j], result_block = physics_engine.block_collision_handler(blocks[i], blocks[j])
|
||||||
|
block1_v1, block2_v1 = blocks[i].velocity.magnitude, blocks[j].velocity.magnitude
|
||||||
|
|
||||||
|
if result_block:
|
||||||
|
if abs(block1_v - block1_v1) > d_velocity:
|
||||||
|
if not blocks[j] in blocks_to_remove:
|
||||||
|
blocks_to_remove.append(blocks[j])
|
||||||
|
blocks[j].destroy()
|
||||||
|
if abs(block2_v - block2_v1) > d_velocity:
|
||||||
|
if not blocks[i] in blocks_to_remove:
|
||||||
|
blocks_to_remove.append(blocks[i])
|
||||||
|
blocks[i].destroy()
|
||||||
|
|
||||||
|
blocks[i].move()
|
||||||
|
|
||||||
|
for wall in walls:
|
||||||
|
blocks[i] = wall.collision_manager(blocks[i], "BLOCK")
|
||||||
|
|
||||||
|
blocks[i].draw()
|
||||||
|
|
||||||
|
for wall in walls:
|
||||||
|
wall.draw()
|
||||||
|
|
||||||
|
score_text.add_text("SCORE: " + str(self.score), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
score_text.draw()
|
||||||
|
|
||||||
|
birds_remaining.add_text("BIRDS REMAINING: " + str(len(birds)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
birds_remaining.draw()
|
||||||
|
|
||||||
|
pigs_remaining.add_text("PIGS REMAINING: " + str(len(pigs)), 25, "Fonts/Comic_Kings.ttf", (236, 240, 241))
|
||||||
|
pigs_remaining.draw()
|
||||||
|
|
||||||
|
mandav.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
if all_rest(pigs, birds, blocks):
|
||||||
|
for pig in pigs_to_remove:
|
||||||
|
if pig in pigs:
|
||||||
|
pigs.remove(pig)
|
||||||
|
self.score += 100
|
||||||
|
|
||||||
|
for block in blocks_to_remove:
|
||||||
|
if block in blocks:
|
||||||
|
blocks.remove(block)
|
||||||
|
self.score += 50
|
||||||
|
|
||||||
|
pigs_to_remove = []
|
||||||
|
blocks_to_remove = []
|
||||||
|
|
||||||
|
clock.tick(60)
|
103
Angry_Birds/objects.py
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
'''
|
||||||
|
|
||||||
|
Game: Angry Birds
|
||||||
|
File: objects.py
|
||||||
|
|
||||||
|
Contents: Class SLAB or WALL
|
||||||
|
|
||||||
|
Requirements: Pygame, sys, math
|
||||||
|
Supporting Modules: physics_engine
|
||||||
|
|
||||||
|
By: Jatin Kumar Mandav
|
||||||
|
|
||||||
|
Blog: https://www.jatinmandav.wordpress.com
|
||||||
|
Twitter: @jatinmandav
|
||||||
|
YouTube: https://www.youtube.com/mandav
|
||||||
|
|
||||||
|
'''
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
|
||||||
|
import physics_engine
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
display = None
|
||||||
|
width = None
|
||||||
|
height = None
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
ground = 50
|
||||||
|
|
||||||
|
def init(screen):
|
||||||
|
global width, height, display
|
||||||
|
display = screen
|
||||||
|
(width, height) = display.get_rect().size
|
||||||
|
height -= ground
|
||||||
|
|
||||||
|
class Slab:
|
||||||
|
def __init__(self, x, y, w, h, color=(255, 255, 255)):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
|
||||||
|
if self.w > self.h:
|
||||||
|
self.image = pygame.image.load("Images/wall_horizontal.png")
|
||||||
|
else:
|
||||||
|
self.image = pygame.image.load("Images/wall_vertical.png")
|
||||||
|
|
||||||
|
self.image = pygame.transform.scale(self.image, (self.w, self.h))
|
||||||
|
|
||||||
|
self.color = color
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
display.blit(self.image, (self.x, self.y))
|
||||||
|
|
||||||
|
def collision_manager(self, ball, type="BALL"):
|
||||||
|
if type == "BALL":
|
||||||
|
if (ball.y + ball.r > self.y) and (ball.y < self.y + self.h):
|
||||||
|
if (ball.x < self.x + self.w) and (ball.x + ball.r > self.x + self.w):
|
||||||
|
ball.x = 2*(self.x + self.w) - ball.x
|
||||||
|
ball.velocity.angle = - ball.velocity.angle
|
||||||
|
ball.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
elif ball.x + ball.r > self.x and (ball.x < self.x):
|
||||||
|
ball.x = 2*(self.x - ball.r) - ball.x
|
||||||
|
ball.velocity.angle = - ball.velocity.angle
|
||||||
|
ball.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
if (ball.x + ball.r > self.x) and (ball.x < self.x + self.w):
|
||||||
|
if ball.y + ball.r > self.y and ball.y < self.y:
|
||||||
|
ball.y = 2*(self.y - ball.r) - ball.y
|
||||||
|
ball.velocity.angle = pi - ball.velocity.angle
|
||||||
|
ball.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
elif (ball.y < self.y + self.h) and (ball.y + ball.r > self.y + self.h):
|
||||||
|
ball.y = 2*(self.y + self.h) - ball.y
|
||||||
|
ball.velocity.angle = pi - ball.velocity.angle
|
||||||
|
ball.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
|
||||||
|
return ball
|
||||||
|
else:
|
||||||
|
block = ball
|
||||||
|
if (block.y + block.h > self.y) and (block.y < self.y + self.h):
|
||||||
|
if (block.x < self.x + self.w) and (block.x + block.w > self.x + self.w):
|
||||||
|
block.x = 2*(self.x + self.w) - block.x
|
||||||
|
block.velocity.angle = - block.velocity.angle
|
||||||
|
block.rotateAngle = - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
elif block.x + block.w > self.x and (block.x < self.x):
|
||||||
|
block.x = 2*(self.x - block.w) - block.x
|
||||||
|
block.velocity.angle = - block.velocity.angle
|
||||||
|
block.rotateAngle = - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
if (block.x + block.w > self.x) and (block.x < self.x + self.w):
|
||||||
|
if block.y + block.h > self.y and block.y < self.y:
|
||||||
|
block.y = 2*(self.y - block.h) - block.y
|
||||||
|
block.velocity.angle = pi - block.velocity.angle
|
||||||
|
block.rotateAngle = pi - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
elif (block.y < self.y + self.h) and (block.y + block.h > self.y + self.h):
|
||||||
|
block.y = 2*(self.y + self.h) - block.y
|
||||||
|
block.velocity.angle = pi - block.velocity.angle
|
||||||
|
block.rotateAngle = pi - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= physics_engine.elasticity
|
||||||
|
|
||||||
|
return block
|
393
Angry_Birds/physics_engine.py
Normal file
@ -0,0 +1,393 @@
|
|||||||
|
'''
|
||||||
|
|
||||||
|
Game: Angry Birds
|
||||||
|
File: physics_engine.py
|
||||||
|
|
||||||
|
Contents: Class Vector
|
||||||
|
Class PIG
|
||||||
|
Class BIRD
|
||||||
|
Class BLOCK
|
||||||
|
Class SLINGSHOT
|
||||||
|
func collision_handler, block_collision_handler
|
||||||
|
|
||||||
|
Requirements: Pygame, sys, math, random
|
||||||
|
|
||||||
|
By: Jatin Kumar Mandav
|
||||||
|
|
||||||
|
Blog: https://www.jatinmandav.wordpress.com
|
||||||
|
Twitter: @jatinmandav
|
||||||
|
YouTube: https://www.youtube.com/mandav
|
||||||
|
|
||||||
|
'''
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
import random
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
width = None
|
||||||
|
height = None
|
||||||
|
display = None
|
||||||
|
ground = 50
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
def init(screen):
|
||||||
|
global width, height, display
|
||||||
|
display = screen
|
||||||
|
(width, height) = display.get_rect().size
|
||||||
|
height -= ground
|
||||||
|
|
||||||
|
class Vector:
|
||||||
|
def __init__(self, magnitude=0, angle=radians(0)):
|
||||||
|
self.magnitude = magnitude
|
||||||
|
self.angle = angle
|
||||||
|
|
||||||
|
def add_vectors(vector1, vector2):
|
||||||
|
x = sin(vector1.angle)*vector1.magnitude + sin(vector2.angle)*vector2.magnitude
|
||||||
|
y = cos(vector1.angle)*vector1.magnitude + cos(vector2.angle)*vector2.magnitude
|
||||||
|
|
||||||
|
new_angle = 0.5*pi - atan2(y, x)
|
||||||
|
new_magnitude = hypot(x, y)
|
||||||
|
|
||||||
|
new_vector = Vector(new_magnitude, new_angle)
|
||||||
|
return new_vector
|
||||||
|
|
||||||
|
gravity = Vector(0.2, pi)
|
||||||
|
inverse_friction = 0.99
|
||||||
|
elasticity = 0.8
|
||||||
|
block_elasticity = 0.7
|
||||||
|
|
||||||
|
class Pig:
|
||||||
|
def __init__(self, x, y, r, v=None, type="PIG", loaded = False, color=(255, 255, 255)):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.r = r
|
||||||
|
if v == None:
|
||||||
|
self.velocity = Vector()
|
||||||
|
else:
|
||||||
|
self.velocity = v
|
||||||
|
|
||||||
|
self.pig1_image = pygame.image.load("Images/pig1.png")
|
||||||
|
self.pig2_image = pygame.image.load("Images/pig3.png")
|
||||||
|
|
||||||
|
self.pig_dead = pygame.image.load("Images/pig_damaged.png")
|
||||||
|
|
||||||
|
self.bird_image = pygame.image.load("Images/bird.png")
|
||||||
|
|
||||||
|
if type == "PIG":
|
||||||
|
self.image = random.choice([self.pig1_image, self.pig2_image])
|
||||||
|
else:
|
||||||
|
self.image = self.bird_image
|
||||||
|
|
||||||
|
self.type = type
|
||||||
|
self.color = color
|
||||||
|
self.loaded = loaded
|
||||||
|
self.path = []
|
||||||
|
self.count = 0
|
||||||
|
self.animate_count = 0
|
||||||
|
self.isDead = False
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
self.animate_count += 1
|
||||||
|
|
||||||
|
if self.type == "BIRD" and not self.loaded:
|
||||||
|
for point in self.path:
|
||||||
|
pygame.draw.ellipse(display, self.color, (point[0], point[1], 3, 3), 1)
|
||||||
|
|
||||||
|
if (self.type == "PIG") and (not self.animate_count%20) and (not self.isDead):
|
||||||
|
self.image = random.choice([self.pig1_image, self.pig2_image])
|
||||||
|
|
||||||
|
display.blit(self.image, (self.x - self.r, self.y - self.r))
|
||||||
|
|
||||||
|
|
||||||
|
def dead(self):
|
||||||
|
self.isDead = True
|
||||||
|
self.image = self.pig_dead
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.velocity = add_vectors(self.velocity, gravity)
|
||||||
|
|
||||||
|
self.x += self.velocity.magnitude*sin(self.velocity.angle)
|
||||||
|
self.y -= self.velocity.magnitude*cos(self.velocity.angle)
|
||||||
|
|
||||||
|
self.velocity.magnitude *= inverse_friction
|
||||||
|
|
||||||
|
if self.x > width - self.r:
|
||||||
|
self.x = 2*(width - self.r) - self.x
|
||||||
|
self.velocity.angle *= -1
|
||||||
|
self.velocity.magnitude *= elasticity
|
||||||
|
elif self.x < self.r:
|
||||||
|
self.x = 2*self.r - self.x
|
||||||
|
self.velocity.angle *= -1
|
||||||
|
self.velocity.magnitude *= elasticity
|
||||||
|
|
||||||
|
if self.y > height - self.r:
|
||||||
|
self.y = 2*(height - self.r) - self.y
|
||||||
|
self.velocity.angle = pi - self.velocity.angle
|
||||||
|
self.velocity.magnitude *= elasticity
|
||||||
|
elif self.y < self.r:
|
||||||
|
self.y = 2*self.r - self.y
|
||||||
|
self.velocity.angle = pi - self.velocity.angle
|
||||||
|
self.velocity.magnitude *= elasticity
|
||||||
|
|
||||||
|
self.count += 1
|
||||||
|
if self.count%1 == 0:
|
||||||
|
self.path.append((self.x, self.y))
|
||||||
|
|
||||||
|
class Bird(Pig):
|
||||||
|
def load(self, slingshot):
|
||||||
|
self.x = slingshot.x
|
||||||
|
self.y = slingshot.y
|
||||||
|
self.loaded = True
|
||||||
|
|
||||||
|
def mouse_selected(self):
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
dx = pos[0] - self.x
|
||||||
|
dy = pos[1] - self.y
|
||||||
|
dist = hypot(dy, dx)
|
||||||
|
if dist < self.r:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def reposition(self, slingshot, mouse_click):
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
if self.mouse_selected():
|
||||||
|
self.x = pos[0]
|
||||||
|
self.y = pos[1]
|
||||||
|
|
||||||
|
dx = slingshot.x - self.x
|
||||||
|
dy = slingshot.y - self.y
|
||||||
|
self.velocity.magnitude = int(hypot(dx, dy)/2)
|
||||||
|
if self.velocity.magnitude > 80:
|
||||||
|
self.velocity.magnitude = 80
|
||||||
|
self.velocity.angle = pi/2 + atan2(dy, dx)
|
||||||
|
|
||||||
|
def unload(self):
|
||||||
|
self.loaded = False
|
||||||
|
|
||||||
|
def project_path(self):
|
||||||
|
if self.loaded:
|
||||||
|
path = []
|
||||||
|
ball = Pig(self.x, self.y, self.r, self.velocity, self.type)
|
||||||
|
for i in range(30):
|
||||||
|
ball.move()
|
||||||
|
if i%5 == 0:
|
||||||
|
path.append((ball.x, ball.y))
|
||||||
|
|
||||||
|
for point in path:
|
||||||
|
pygame.draw.ellipse(display, self.color, (point[0], point[1], 2, 2))
|
||||||
|
|
||||||
|
|
||||||
|
class Block:
|
||||||
|
def __init__(self, x, y, r, v=None, color=( 120, 40, 31 ), colorBoundary = ( 28, 40, 51 )):
|
||||||
|
self.r = 50
|
||||||
|
self.w = 100
|
||||||
|
self.h = 100
|
||||||
|
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
self.block_image = pygame.image.load("Images/block1.png")
|
||||||
|
self.block_destroyed_image = pygame.image.load("Images/block_destroyed1.png")
|
||||||
|
|
||||||
|
self.image = self.block_image
|
||||||
|
|
||||||
|
if v == None:
|
||||||
|
self.velocity = Vector()
|
||||||
|
else:
|
||||||
|
self.velocity = v
|
||||||
|
|
||||||
|
self.color = color
|
||||||
|
self.colorDestroyed = ( 100, 30, 22 )
|
||||||
|
self.colorBoundary = colorBoundary
|
||||||
|
self.rotateAngle = radians(0)
|
||||||
|
self.anchor = (self.r/2, self.r/2)
|
||||||
|
|
||||||
|
self.isDestroyed = False
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0)):
|
||||||
|
corr = 0
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord):
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.transform.rotate(self.image, self.rotateAngle)
|
||||||
|
display.blit(self.image, (self.x - self.w/2, self.y))
|
||||||
|
|
||||||
|
def destroy(self):
|
||||||
|
self.isDestroyed = True
|
||||||
|
self.image = self.block_destroyed_image
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.velocity = add_vectors(self.velocity, gravity)
|
||||||
|
|
||||||
|
self.x += self.velocity.magnitude*sin(self.velocity.angle)
|
||||||
|
self.y -= self.velocity.magnitude*cos(self.velocity.angle)
|
||||||
|
|
||||||
|
self.velocity.magnitude *= inverse_friction
|
||||||
|
|
||||||
|
if self.x > width - self.w:
|
||||||
|
self.x = 2*(width - self.w) - self.x
|
||||||
|
self.velocity.angle *= -1
|
||||||
|
self.rotateAngle = - self.velocity.angle
|
||||||
|
self.velocity.magnitude *= block_elasticity
|
||||||
|
elif self.x < self.w:
|
||||||
|
self.x = 2*self.w - self.x
|
||||||
|
self.velocity.angle *= -1
|
||||||
|
self.rotateAngle = - self.velocity.angle
|
||||||
|
self.velocity.magnitude *= block_elasticity
|
||||||
|
|
||||||
|
if self.y > height - self.h:
|
||||||
|
self.y = 2*(height - self.h) - self.y
|
||||||
|
self.velocity.angle = pi - self.velocity.angle
|
||||||
|
self.rotateAngle = pi - self.velocity.angle
|
||||||
|
self.velocity.magnitude *= block_elasticity
|
||||||
|
elif self.y < self.h:
|
||||||
|
self.y = 2*self.h - self.y
|
||||||
|
self.velocity.angle = pi - self.velocity.angle
|
||||||
|
self.rotateAngle = pi - self.velocity.angle
|
||||||
|
self.velocity.magnitude *= block_elasticity
|
||||||
|
|
||||||
|
|
||||||
|
class Slingshot:
|
||||||
|
def __init__(self, x, y, w, h, color=( 66, 73, 73 )):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.color = color
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0)):
|
||||||
|
corr = 0
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord):
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
|
||||||
|
def draw(self, loaded=None):
|
||||||
|
pygame.draw.rect(display, self.color, (self.x, self.y + self.h*1/3, self.w, self.h*2/3))
|
||||||
|
|
||||||
|
if (not loaded == None) and loaded.loaded:
|
||||||
|
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)
|
||||||
|
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)
|
||||||
|
|
||||||
|
pygame.draw.rect(display, self.color, (self.x - self.w/4, self.y, self.w/2, self.h/3), 5)
|
||||||
|
pygame.draw.rect(display, self.color, (self.x + self.w - self.w/4, self.y, self.w/2, self.h/3), 5)
|
||||||
|
|
||||||
|
def collision_handler(b_1, b_2, type):
|
||||||
|
collision = False
|
||||||
|
if type == "BALL":
|
||||||
|
dx = b_1.x - b_2.x
|
||||||
|
dy = b_1.y - b_2.y
|
||||||
|
|
||||||
|
dist = hypot(dx, dy)
|
||||||
|
if dist < b_1.r + b_2.r:
|
||||||
|
tangent = atan2(dy, dx)
|
||||||
|
angle = 0.5*pi + tangent
|
||||||
|
|
||||||
|
angle1 = 2*tangent - b_1.velocity.angle
|
||||||
|
angle2 = 2*tangent - b_2.velocity.angle
|
||||||
|
|
||||||
|
magnitude1 = b_2.velocity.magnitude
|
||||||
|
magnitude2 = b_1.velocity.magnitude
|
||||||
|
|
||||||
|
b_1.velocity = Vector(magnitude1, angle1)
|
||||||
|
b_2.velocity = Vector(magnitude2, angle2)
|
||||||
|
|
||||||
|
b_1.velocity.magnitude *= elasticity
|
||||||
|
b_2.velocity.magnitude *= elasticity
|
||||||
|
|
||||||
|
overlap = 0.5*(b_1.r + b_2.r - dist + 1)
|
||||||
|
b_1.x += sin(angle)*overlap
|
||||||
|
b_1.y -= cos(angle)*overlap
|
||||||
|
b_2.x -= sin(angle)*overlap
|
||||||
|
b_2.y += cos(angle)*overlap
|
||||||
|
collision = True
|
||||||
|
#print(collision)
|
||||||
|
|
||||||
|
#print(collision)
|
||||||
|
return b_1, b_2, collision
|
||||||
|
elif type == "BALL_N_BLOCK":
|
||||||
|
dx = b_1.x - b_2.x
|
||||||
|
dy = b_1.y - b_2.y
|
||||||
|
|
||||||
|
dist = hypot(dx, dy)
|
||||||
|
if dist < b_1.r + b_2.w:
|
||||||
|
tangent = atan2(dy, dx)
|
||||||
|
angle = 0.5*pi + tangent
|
||||||
|
|
||||||
|
angle1 = 2*tangent - b_1.velocity.angle
|
||||||
|
angle2 = 2*tangent - b_2.velocity.angle
|
||||||
|
|
||||||
|
magnitude1 = b_2.velocity.magnitude
|
||||||
|
magnitude2 = b_1.velocity.magnitude
|
||||||
|
|
||||||
|
b_1.velocity = Vector(magnitude1, angle1)
|
||||||
|
b_2.velocity = Vector(magnitude2, angle2)
|
||||||
|
|
||||||
|
b_1.velocity.magnitude *= elasticity
|
||||||
|
b_2.velocity.magnitude *= block_elasticity
|
||||||
|
|
||||||
|
overlap = 0.5*(b_1.r + b_2.w - dist + 1)
|
||||||
|
b_1.x += sin(angle)*overlap
|
||||||
|
b_1.y -= cos(angle)*overlap
|
||||||
|
b_2.x -= sin(angle)*overlap
|
||||||
|
b_2.y += cos(angle)*overlap
|
||||||
|
collision = True
|
||||||
|
|
||||||
|
return b_1, b_2, collision
|
||||||
|
|
||||||
|
def block_collision_handler(block, block2):
|
||||||
|
collision = False
|
||||||
|
if (block.y + block.h > block2.y) and (block.y < block2.y + block2.h):
|
||||||
|
if (block.x < block2.x + block2.w) and (block.x + block.w > block2.x + block2.w):
|
||||||
|
block.x = 2*(block2.x + block2.w) - block.x
|
||||||
|
block.velocity.angle = - block.velocity.angle
|
||||||
|
block.rotateAngle = - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= block_elasticity
|
||||||
|
|
||||||
|
block2.velocity.angle = - block2.velocity.angle
|
||||||
|
block2.rotateAngle = - block2.velocity.angle
|
||||||
|
block2.velocity.magnitude *= block_elasticity
|
||||||
|
collision = True
|
||||||
|
|
||||||
|
elif block.x + block.w > block2.x and (block.x < block2.x):
|
||||||
|
block.x = 2*(block2.x - block.w) - block.x
|
||||||
|
block.velocity.angle = - block.velocity.angle
|
||||||
|
block.rotateAngle = - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= block_elasticity
|
||||||
|
|
||||||
|
block2.velocity.angle = - block2.velocity.angle
|
||||||
|
block2.rotateAngle = - block2.velocity.angle
|
||||||
|
block2.velocity.magnitude *= block_elasticity
|
||||||
|
collision = True
|
||||||
|
|
||||||
|
if (block.x + block.w > block2.x) and (block.x < block2.x + block2.w):
|
||||||
|
if block.y + block.h > block2.y and block.y < block2.y:
|
||||||
|
block.y = 2*(block2.y - block.h) - block.y
|
||||||
|
block.velocity.angle = pi - block.velocity.angle
|
||||||
|
block.rotateAngle = pi - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= block_elasticity
|
||||||
|
|
||||||
|
block2.velocity.angle = pi - block2.velocity.angle
|
||||||
|
block2.rotateAngle = pi - block2.velocity.angle
|
||||||
|
block2.velocity.magnitude *= block_elasticity
|
||||||
|
collision = True
|
||||||
|
|
||||||
|
elif (block.y < block2.y + block2.h) and (block.y + block.h > block2.y + block2.h):
|
||||||
|
block.y = 2*(block2.y + block2.h) - block.y
|
||||||
|
block.velocity.angle = pi - block.velocity.angle
|
||||||
|
block.rotateAngle = pi - block.velocity.angle
|
||||||
|
block.velocity.magnitude *= block_elasticity
|
||||||
|
|
||||||
|
block2.velocity.angle = pi - block2.velocity.angle
|
||||||
|
block2.rotateAngle = pi - block2.velocity.angle
|
||||||
|
block2.velocity.magnitude *= block_elasticity
|
||||||
|
collision = True
|
||||||
|
|
||||||
|
return block, block2, collision
|
189
BalloonShooter/BalloonShooter.py
Normal file
@ -0,0 +1,189 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Balloon Shooter
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, random, math
|
||||||
|
#
|
||||||
|
# Controls - Mouse
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
from math import *
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 500
|
||||||
|
height = 500
|
||||||
|
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption("Balloon Shooter")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
margin = 100
|
||||||
|
lowerBound = 100
|
||||||
|
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
white = (230, 230, 230)
|
||||||
|
lightBlue = (174, 214, 241)
|
||||||
|
red = (231, 76, 60)
|
||||||
|
lightGreen = (25, 111, 61)
|
||||||
|
darkGray = (40, 55, 71)
|
||||||
|
darkBlue = (21, 67, 96)
|
||||||
|
green = (35, 155, 86)
|
||||||
|
yellow = (244, 208, 63)
|
||||||
|
blue = (46, 134, 193)
|
||||||
|
purple = (155, 89, 182)
|
||||||
|
orange = (243, 156, 18)
|
||||||
|
|
||||||
|
font = pygame.font.SysFont("Snap ITC", 25)
|
||||||
|
|
||||||
|
# Balloon Class
|
||||||
|
class Balloon:
|
||||||
|
def __init__(self, speed):
|
||||||
|
self.a = random.randint(30, 40)
|
||||||
|
self.b = self.a + random.randint(0, 10)
|
||||||
|
self.x = random.randrange(margin, width - self.a - margin)
|
||||||
|
self.y = height - lowerBound
|
||||||
|
self.angle = 90
|
||||||
|
self.speed = -speed
|
||||||
|
self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]
|
||||||
|
self.length = random.randint(50, 100)
|
||||||
|
self.color = random.choice([red, green, purple, orange, yellow, blue])
|
||||||
|
|
||||||
|
# Move balloon around the Screen
|
||||||
|
def move(self):
|
||||||
|
direct = random.choice(self.probPool)
|
||||||
|
|
||||||
|
if direct == -1:
|
||||||
|
self.angle += -10
|
||||||
|
elif direct == 0:
|
||||||
|
self.angle += 0
|
||||||
|
else:
|
||||||
|
self.angle += 10
|
||||||
|
|
||||||
|
self.y += self.speed*sin(radians(self.angle))
|
||||||
|
self.x += self.speed*cos(radians(self.angle))
|
||||||
|
|
||||||
|
if (self.x + self.a > width) or (self.x < 0):
|
||||||
|
if self.y > height/5:
|
||||||
|
self.x -= self.speed*cos(radians(self.angle))
|
||||||
|
else:
|
||||||
|
self.reset()
|
||||||
|
if self.y + self.b < 0 or self.y > height + 30:
|
||||||
|
self.reset()
|
||||||
|
|
||||||
|
# Show/Draw the balloon
|
||||||
|
def show(self):
|
||||||
|
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))
|
||||||
|
pygame.draw.ellipse(display, self.color, (self.x, self.y, self.a, self.b))
|
||||||
|
pygame.draw.ellipse(display, self.color, (self.x + self.a/2 - 5, self.y + self.b - 3, 10, 10))
|
||||||
|
|
||||||
|
# Check if Balloon is bursted
|
||||||
|
def burst(self):
|
||||||
|
global score
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
if onBalloon(self.x, self.y, self.a, self.b, pos):
|
||||||
|
score += 1
|
||||||
|
self.reset()
|
||||||
|
|
||||||
|
# Reset the Balloon
|
||||||
|
def reset(self):
|
||||||
|
self.a = random.randint(30, 40)
|
||||||
|
self.b = self.a + random.randint(0, 10)
|
||||||
|
self.x = random.randrange(margin, width - self.a - margin)
|
||||||
|
self.y = height - lowerBound
|
||||||
|
self.angle = 90
|
||||||
|
self.speed -= 0.002
|
||||||
|
self.probPool = [-1, -1, -1, 0, 0, 0, 0, 1, 1, 1]
|
||||||
|
self.length = random.randint(50, 100)
|
||||||
|
self.color = random.choice([red, green, purple, orange, yellow, blue])
|
||||||
|
|
||||||
|
balloons = []
|
||||||
|
noBalloon = 10
|
||||||
|
for i in range(noBalloon):
|
||||||
|
obj = Balloon(random.choice([1, 1, 2, 2, 2, 2, 3, 3, 3, 4]))
|
||||||
|
balloons.append(obj)
|
||||||
|
|
||||||
|
def onBalloon(x, y, a, b, pos):
|
||||||
|
if (x < pos[0] < x + a) and (y < pos[1] < y + b):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
# show the location of Mouse
|
||||||
|
def pointer():
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
r = 25
|
||||||
|
l = 20
|
||||||
|
color = lightGreen
|
||||||
|
for i in range(noBalloon):
|
||||||
|
if onBalloon(balloons[i].x, balloons[i].y, balloons[i].a, balloons[i].b, pos):
|
||||||
|
color = red
|
||||||
|
pygame.draw.ellipse(display, color, (pos[0] - r/2, pos[1] - r/2, r, r), 4)
|
||||||
|
pygame.draw.line(display, color, (pos[0], pos[1] - l/2), (pos[0], pos[1] - l), 4)
|
||||||
|
pygame.draw.line(display, color, (pos[0] + l/2, pos[1]), (pos[0] + l, pos[1]), 4)
|
||||||
|
pygame.draw.line(display, color, (pos[0], pos[1] + l/2), (pos[0], pos[1] + l), 4)
|
||||||
|
pygame.draw.line(display, color, (pos[0] - l/2, pos[1]), (pos[0] - l, pos[1]), 4)
|
||||||
|
|
||||||
|
def lowerPlatform():
|
||||||
|
pygame.draw.rect(display, darkGray, (0, height - lowerBound, width, lowerBound))
|
||||||
|
|
||||||
|
def showScore():
|
||||||
|
scoreText = font.render("Balloons Bursted : " + str(score), True, white)
|
||||||
|
display.blit(scoreText, (150, height - lowerBound + 50))
|
||||||
|
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
def game():
|
||||||
|
global score
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
score = 0
|
||||||
|
game()
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
for i in range(noBalloon):
|
||||||
|
balloons[i].burst()
|
||||||
|
|
||||||
|
display.fill(lightBlue)
|
||||||
|
|
||||||
|
for i in range(noBalloon):
|
||||||
|
balloons[i].show()
|
||||||
|
|
||||||
|
pointer()
|
||||||
|
|
||||||
|
for i in range(noBalloon):
|
||||||
|
balloons[i].move()
|
||||||
|
|
||||||
|
|
||||||
|
lowerPlatform()
|
||||||
|
showScore()
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
|
||||||
|
game()
|
BIN
Battles/Battles - Game.rar
Normal file
133
Battles/Code/Battles.py
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
"""
|
||||||
|
~~~~~~~~~~~~~~
|
||||||
|
Battles.py
|
||||||
|
~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
By - JATIN KUMAR MANDAV
|
||||||
|
|
||||||
|
BATTLES is inspired by mobile strategy video game Clash of Clans
|
||||||
|
which is developed and published by Finnish game developer Supercell.
|
||||||
|
|
||||||
|
This game is developed using PYGAME library of PYTHON 2.7
|
||||||
|
|
||||||
|
Lines - 127
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
from GUI import *
|
||||||
|
from Maps import *
|
||||||
|
from Structures import *
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 1260
|
||||||
|
height = 720
|
||||||
|
|
||||||
|
margin = 25
|
||||||
|
|
||||||
|
groundW = width - 150 - 2*margin
|
||||||
|
groundH = height - 2*margin
|
||||||
|
|
||||||
|
display = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
initGUI(display, (width, height))
|
||||||
|
initStruct(display, (groundW, groundH), margin)
|
||||||
|
initMaps(display, (width, height), margin, 100)
|
||||||
|
|
||||||
|
|
||||||
|
white = (255, 255, 255)
|
||||||
|
black = (0, 0, 0)
|
||||||
|
yellow = (241, 196, 15)
|
||||||
|
red = (203, 67, 53)
|
||||||
|
green = (30, 132, 73)
|
||||||
|
blue = (36, 113, 163)
|
||||||
|
gray = (113, 125, 126)
|
||||||
|
|
||||||
|
activeButton = 0
|
||||||
|
|
||||||
|
|
||||||
|
def drawBorder():
|
||||||
|
pygame.draw.rect(display, black, (margin, margin, groundW, groundH), 2)
|
||||||
|
|
||||||
|
def createMaps(arg=0):
|
||||||
|
maps = Maps()
|
||||||
|
maps.createMap()
|
||||||
|
|
||||||
|
def adventure(arg=0):
|
||||||
|
maps = Maps()
|
||||||
|
maps.openMap([0, "adventure"])
|
||||||
|
|
||||||
|
def customMap(arg=0):
|
||||||
|
maps = Maps()
|
||||||
|
maps.openMap([0, "custom"])
|
||||||
|
|
||||||
|
def close(arg=0):
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
def game():
|
||||||
|
loop = True
|
||||||
|
font = pygame.font.SysFont("Agency FB", 50)
|
||||||
|
font.set_bold(True)
|
||||||
|
mandav = font.render("MANDAV", True, (71, 71, 71))
|
||||||
|
|
||||||
|
font2 = pygame.font.SysFont("Agency FB", 200)
|
||||||
|
font2.set_bold(True)
|
||||||
|
title = font2.render("BATTLES", True, (200, 200, 200))
|
||||||
|
titlePos = title.get_rect()
|
||||||
|
titlePos.center = [width/2, height/2 - 200]
|
||||||
|
|
||||||
|
tank = pygame.image.load("Images/Tank.png")
|
||||||
|
tankPos = tank.get_rect()
|
||||||
|
tankPos.center = [width/2, height/2 + 40]
|
||||||
|
|
||||||
|
heli = pygame.image.load("Images/Helicopter.png")
|
||||||
|
heliPos = heli.get_rect()
|
||||||
|
heliPos.center = [width/2 - 70, height/2 - 100]
|
||||||
|
|
||||||
|
quit = Button(width - 300 - 10, height/2 + 200, 300, 100, blue, blue, close)
|
||||||
|
quit.addText("QUIT", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
|
||||||
|
|
||||||
|
create = Button(width - 600 - 20, height/2 + 200, 300, 100, blue, blue, createMaps)
|
||||||
|
create.addText("CREATE MAPS", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
|
||||||
|
|
||||||
|
adven = Button(width - 900 - 30, height/2 + 200, 300, 100, blue, blue, adventure)
|
||||||
|
adven.addText("ADVENTURE", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
|
||||||
|
|
||||||
|
custom = Button(width - 1200 - 40, height/2 + 200, 300, 100, blue, blue, customMap)
|
||||||
|
custom.addText("CUSTOM BASES", (0, 0), 30, "Showcard Gothic", (150, 150, 150))
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
quit.select()
|
||||||
|
create.select()
|
||||||
|
adven.select()
|
||||||
|
custom.select()
|
||||||
|
|
||||||
|
display.fill((51,51,51))
|
||||||
|
|
||||||
|
display.blit(title, titlePos)
|
||||||
|
display.blit(heli, heliPos)
|
||||||
|
display.blit(tank, tankPos)
|
||||||
|
|
||||||
|
display.blit(mandav, (width - 200, height - 60))
|
||||||
|
|
||||||
|
quit.draw()
|
||||||
|
create.draw()
|
||||||
|
adven.draw()
|
||||||
|
custom.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
game()
|
2
Battles/Code/Data/customMaps
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
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/
|
||||||
|
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/
|
1
Battles/Code/Data/mapReached
Normal file
@ -0,0 +1 @@
|
|||||||
|
8
|
24
Battles/Code/Data/maps
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
465.0,271.0,1/|||565,352,1/|637,425/
|
||||||
|
451.0,270.0,1/613.0,452.0,1/|||525,361,1/|659,309/448,421/
|
||||||
|
386.0,327.0,1/517.0,453.0,1/||605.0,297.0,1/|513,351,1/|676,387/463,229/394,441/
|
||||||
|
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/
|
||||||
|
453.0,253.0,1/657.0,255.0,1/||553.0,451.0,1/|553,339,2/|710,389/437,381/
|
||||||
|
437.0,453.0,1/635.0,458.0,1/||533.0,531.0,1/|541,359,2/|429,306/542,241/655,336/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/|
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
||||||
|
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/
|
135
Battles/Code/GUI.py
Normal file
@ -0,0 +1,135 @@
|
|||||||
|
"""
|
||||||
|
~~~~~~
|
||||||
|
GUI.py
|
||||||
|
~~~~~~
|
||||||
|
|
||||||
|
By - JATIN KUMAR MANDAV
|
||||||
|
|
||||||
|
|
||||||
|
A small library for Button widget for the game
|
||||||
|
This is completely coded using PYGAME library of PYTHON 2.7
|
||||||
|
|
||||||
|
Lines - 136
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Imports
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# Initialize Pygame
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 0
|
||||||
|
height = 0
|
||||||
|
display = None
|
||||||
|
|
||||||
|
# Initialize display, width, margin, and height of pygame window
|
||||||
|
def initGUI(screen, size):
|
||||||
|
global display, width, height
|
||||||
|
display = screen
|
||||||
|
width = size[0]
|
||||||
|
height = size[1]
|
||||||
|
|
||||||
|
# Button Class
|
||||||
|
class Button:
|
||||||
|
def __init__(self, x, y, w, h, color, activeColor, action=None, border=None, borderWidth = 2):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.color = color
|
||||||
|
self.activeColor = activeColor
|
||||||
|
self.arg = 0
|
||||||
|
self.borderWidth = borderWidth
|
||||||
|
self.action = action
|
||||||
|
|
||||||
|
if border:
|
||||||
|
self.border = border
|
||||||
|
else:
|
||||||
|
self.border = color
|
||||||
|
|
||||||
|
self.image = None
|
||||||
|
self.imagePos = None
|
||||||
|
|
||||||
|
self.text = None
|
||||||
|
self.textPos = None
|
||||||
|
self.align = "center"
|
||||||
|
self.font = "Times New Roman"
|
||||||
|
self.fontSize = 11
|
||||||
|
self.fontColor = (0, 0, 0)
|
||||||
|
|
||||||
|
self.image = None
|
||||||
|
self.imagePos = None
|
||||||
|
|
||||||
|
def select(self):
|
||||||
|
if isActive(self.x, self.y, self.w, self.h) and self.action != None:
|
||||||
|
self.action(self.arg)
|
||||||
|
|
||||||
|
def draw(self, index=0):
|
||||||
|
if index == self.arg:
|
||||||
|
color = self.activeColor
|
||||||
|
else:
|
||||||
|
color = self.color
|
||||||
|
pygame.draw.rect(display, color, (self.x, self.y, self.w, self.h))
|
||||||
|
pygame.draw.rect(display, self.border, (self.x, self.y, self.w, self.h), self.borderWidth)
|
||||||
|
if (self.text):
|
||||||
|
display.blit(self.text, self.textPos)
|
||||||
|
if not (self.image == None):
|
||||||
|
display.blit(self.image, self.imagePos)
|
||||||
|
|
||||||
|
def addImage(self, image, pos):
|
||||||
|
size = image.get_size()
|
||||||
|
self.image = image
|
||||||
|
self.imagePos = (self.x + pos[0] - size[0]/2, self.y + pos[1] - size[1]/2)
|
||||||
|
|
||||||
|
def addText(self, text, pos, size=25, font="Times New Roman", color=(0, 0 ,0), align="center"):
|
||||||
|
self.font = font
|
||||||
|
self.fontSize = size
|
||||||
|
self.fontColor = color
|
||||||
|
self.align = align
|
||||||
|
font = pygame.font.SysFont(font, size)
|
||||||
|
|
||||||
|
self.text = font.render(text, True, color)
|
||||||
|
self.textPos = getRect(self.text)
|
||||||
|
if align == "center":
|
||||||
|
self.textPos.center = (self.x + self.w/2 + pos[0], self.y + self.h/2 + pos[1])
|
||||||
|
else:
|
||||||
|
self.textPos = (self.x + 10 + pos[0], self.y + pos[1])
|
||||||
|
|
||||||
|
def updateText(self, text, pos):
|
||||||
|
font = pygame.font.SysFont(self.font, self.fontSize)
|
||||||
|
self.text = font.render(text, True, self.fontColor)
|
||||||
|
self.textPos = getRect(self.text)
|
||||||
|
if self.align == "center":
|
||||||
|
self.textPos.center = (self.x + self.w/2 + pos[0], self.y + self.h/2 + pos[1])
|
||||||
|
else:
|
||||||
|
self.textPos = (self.x + 10 + pos[0], self.y + pos[1])
|
||||||
|
|
||||||
|
|
||||||
|
def getRect(font):
|
||||||
|
return font.get_rect()
|
||||||
|
|
||||||
|
# Check if mouse is over the button
|
||||||
|
def isActive(x, y, w, h):
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
if (x < pos[0] < x + w) and (y < pos[1] < y + h):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
screen = pygame.display.set_mode((200, 100))
|
||||||
|
initGUI(screen, (200, 100))
|
||||||
|
newButton = Button(50, 25, 100, 50, (255, 255, 255))
|
||||||
|
newButton.addText("Text", (0, 0))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
display.fill(0)
|
||||||
|
newButton.draw()
|
||||||
|
pygame.display.update()
|
BIN
Battles/Code/Images/1-stars.png
Normal file
After Width: | Height: | Size: 74 KiB |
BIN
Battles/Code/Images/2-stars.png
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
Battles/Code/Images/3-stars.png
Normal file
After Width: | Height: | Size: 82 KiB |
BIN
Battles/Code/Images/Helicopter.png
Normal file
After Width: | Height: | Size: 85 KiB |
BIN
Battles/Code/Images/Tank.png
Normal file
After Width: | Height: | Size: 122 KiB |
805
Battles/Code/Maps.py
Normal file
@ -0,0 +1,805 @@
|
|||||||
|
"""
|
||||||
|
~~~~~~~
|
||||||
|
Maps.py
|
||||||
|
~~~~~~~
|
||||||
|
|
||||||
|
By - JATIN KUMAR MANDAV
|
||||||
|
|
||||||
|
This library contains the MAPS class which contains major;y two function, openMap() and createMap()
|
||||||
|
|
||||||
|
The user can open the predefined or their saved custom maps using openMap() or
|
||||||
|
Can save their oen designed custom maps using createMap()
|
||||||
|
|
||||||
|
This is completely coded using PYGAME library of PYTHON 2.7
|
||||||
|
|
||||||
|
Lines - 809
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Imports
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
from GUI import *
|
||||||
|
from Structures import *
|
||||||
|
from Troops import *
|
||||||
|
|
||||||
|
width = 0
|
||||||
|
height = 0
|
||||||
|
display = None
|
||||||
|
margin = 0
|
||||||
|
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
groundW = 0
|
||||||
|
groundH = 0
|
||||||
|
validW = 0
|
||||||
|
validH = 0
|
||||||
|
|
||||||
|
margin2 = 0
|
||||||
|
|
||||||
|
white = (255, 255, 255)
|
||||||
|
black = (0, 0, 0)
|
||||||
|
yellow = (241, 196, 15)
|
||||||
|
red = (203, 67, 53)
|
||||||
|
green = (30, 132, 73)
|
||||||
|
blue = (36, 113, 163)
|
||||||
|
gray = (113, 125, 126)
|
||||||
|
darkGray = (23, 32, 42)
|
||||||
|
|
||||||
|
activeButton = 1
|
||||||
|
presentLevel = 1
|
||||||
|
|
||||||
|
victory = False
|
||||||
|
stars = 0
|
||||||
|
|
||||||
|
canInfo = {"w":10, "h":33, "size":33, "range":200, "baseR":25, "shotR":7,
|
||||||
|
"shotSpeed":5}
|
||||||
|
mortarInfo = {"w": 25, "h":25, "r":50, "range":300, "shotR":7, "shotSpeed":7}
|
||||||
|
towerInfo = {"w": 5, "h":30, "size":20, "range":300, "baseR":25, "shotR":7,
|
||||||
|
"shotSpeed":10}
|
||||||
|
hqInfo = {"r":50}
|
||||||
|
|
||||||
|
def initMaps(screen, size, border=0, border2=0):
|
||||||
|
global width, height, display, margin, groundW, groundH, validW, validH, margin2
|
||||||
|
display = screen
|
||||||
|
width = size[0]
|
||||||
|
height = size[1]
|
||||||
|
margin = border
|
||||||
|
|
||||||
|
groundW = width - 150 - 2*margin
|
||||||
|
groundH = height - 2*margin
|
||||||
|
|
||||||
|
margin2 = border2
|
||||||
|
|
||||||
|
validW = width - 2*margin2 - 150
|
||||||
|
validH = height - 2*margin2
|
||||||
|
|
||||||
|
initGUI(display, (width, height))
|
||||||
|
initStruct(display, (groundW, groundH), margin)
|
||||||
|
initTroops(display, (groundW, groundH), margin)
|
||||||
|
|
||||||
|
|
||||||
|
def active(num):
|
||||||
|
global activeButton
|
||||||
|
activeButton = num
|
||||||
|
|
||||||
|
def levelActive(num):
|
||||||
|
global presentLevel
|
||||||
|
presentLevel = num
|
||||||
|
|
||||||
|
|
||||||
|
# Saves the custom maps created by user or "guest"
|
||||||
|
def saveMap(defenceStruct, user="guest"):
|
||||||
|
global structure, noCannon, noMortar, noTower, noHquart, noResource
|
||||||
|
|
||||||
|
if len(defenceStruct) > 0:
|
||||||
|
noCannon = 0
|
||||||
|
noMortar = 0
|
||||||
|
noTower = 0
|
||||||
|
noHquart = 0
|
||||||
|
noResource = 0
|
||||||
|
|
||||||
|
cannons = []
|
||||||
|
mortars = []
|
||||||
|
towers = []
|
||||||
|
headquarter = []
|
||||||
|
resources = []
|
||||||
|
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if struct.type == "CANNON":
|
||||||
|
cannons.append(struct)
|
||||||
|
elif struct.type == "MORTAR":
|
||||||
|
mortars.append(struct)
|
||||||
|
elif struct.type == "TOWER":
|
||||||
|
towers.append(struct)
|
||||||
|
elif struct.type == "HEADQUARTERS":
|
||||||
|
headquarter.append(struct)
|
||||||
|
elif struct.type == "RESOURCE":
|
||||||
|
resources.append(struct)
|
||||||
|
|
||||||
|
if user == "admin":
|
||||||
|
fmap = open("Data/maps", "a")
|
||||||
|
else:
|
||||||
|
fmap = open("Data/customMaps", "a")
|
||||||
|
for struct in cannons:
|
||||||
|
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level) + "/")
|
||||||
|
fmap.write("|")
|
||||||
|
for struct in mortars:
|
||||||
|
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level) + "/")
|
||||||
|
fmap.write("|")
|
||||||
|
for struct in towers:
|
||||||
|
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level) + "/")
|
||||||
|
fmap.write("|")
|
||||||
|
for struct in headquarter:
|
||||||
|
fmap.write(str(struct.x) + "," + str(struct.y) + "," + str(struct.level - 1) + "/")
|
||||||
|
fmap.write("|")
|
||||||
|
for struct in resources:
|
||||||
|
fmap.write(str(struct.x) + "," + str(struct.y) + "/")
|
||||||
|
fmap.write("\n")
|
||||||
|
|
||||||
|
fmap.close()
|
||||||
|
structure = []
|
||||||
|
|
||||||
|
|
||||||
|
# Button to show the damage progress and stars earned
|
||||||
|
damageButton = Button(10, 10, 300, 130, (51, 51, 51), (51, 51, 51))
|
||||||
|
|
||||||
|
star1 = pygame.image.load("Images/1-stars.png")
|
||||||
|
size = star1.get_size()
|
||||||
|
star01 = pygame.transform.scale(star1, (size[0]/5, size[1]/5))
|
||||||
|
|
||||||
|
star2 = pygame.image.load("Images/2-stars.png")
|
||||||
|
size = star2.get_size()
|
||||||
|
star02 = pygame.transform.scale(star2, (size[0]/5, size[1]/5))
|
||||||
|
|
||||||
|
star3 = pygame.image.load("Images/3-stars.png")
|
||||||
|
size = star3.get_size()
|
||||||
|
star03 = pygame.transform.scale(star3, (size[0]/5, size[1]/5))
|
||||||
|
|
||||||
|
def damageDone(fraction, structs):
|
||||||
|
global victory, stars
|
||||||
|
hqThere = False
|
||||||
|
for struct in structs:
|
||||||
|
if struct.type == "HEADQUARTERS":
|
||||||
|
hqThere = True
|
||||||
|
break
|
||||||
|
|
||||||
|
percent = int(float(fraction)*100)
|
||||||
|
if percent == 100:
|
||||||
|
damageButton.addImage(star03, (damageButton.w/2, damageButton.h/2 - 30))
|
||||||
|
victory = True
|
||||||
|
stars = 3
|
||||||
|
elif not hqThere and percent >= 50:
|
||||||
|
damageButton.addImage(star02, (damageButton.w/2, damageButton.h/2 - 30))
|
||||||
|
victory = True
|
||||||
|
stars = 2
|
||||||
|
elif (percent >= 50) or (not hqThere and percent < 50):
|
||||||
|
damageButton.addImage(star01, (damageButton.w/2, damageButton.h/2 - 30))
|
||||||
|
victory = True
|
||||||
|
stars = 1
|
||||||
|
elif percent < 50 and hqThere:
|
||||||
|
damageButton.image = None
|
||||||
|
victory = False
|
||||||
|
stars = 0
|
||||||
|
damageButton.draw()
|
||||||
|
pygame.draw.rect(display, (208, 211, 212), (damageButton.x + 50, damageButton.y + damageButton.h - 50, 200, 30))
|
||||||
|
w = 2*percent
|
||||||
|
pygame.draw.rect(display, (142, 68, 173), (damageButton.x + 50 + 1, damageButton.y + damageButton.h - 50 + 1, w - 2, 30 - 2))
|
||||||
|
|
||||||
|
def drawBorder():
|
||||||
|
pygame.draw.rect(display, black, (margin, margin, groundW, groundH), 2)
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# Pause the Game
|
||||||
|
def pause():
|
||||||
|
font = pygame.font.SysFont("Showcard Gothic", 70)
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_ESCAPE:
|
||||||
|
return
|
||||||
|
if event.key == pygame.K_m:
|
||||||
|
global loop
|
||||||
|
loop = False
|
||||||
|
return
|
||||||
|
|
||||||
|
pauseText = font.render("PAUSED", True, (255, 255, 200))
|
||||||
|
resume = font.render("\'ESCAPE\' to RESUME", True, (140, 200, 50))
|
||||||
|
main = font.render("\'M\' to MAIN MENU", True, (200, 200, 255))
|
||||||
|
quit = font.render("\'Q\' to QUIT", True, (200, 255, 200))
|
||||||
|
|
||||||
|
display.blit(pauseText, (200, height/2 - 150))
|
||||||
|
display.blit(resume, (200, height/2 - 50))
|
||||||
|
display.blit(main, (200, height/2 + 50))
|
||||||
|
display.blit(quit, (200, height/2 + 150))
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
# Maps class
|
||||||
|
class Maps:
|
||||||
|
def __init__(self):
|
||||||
|
fmap = open("Data/maps", "r")
|
||||||
|
data = fmap.read().split("\n")
|
||||||
|
fmap .close()
|
||||||
|
data.pop(-1)
|
||||||
|
|
||||||
|
for i in range(len(data)):
|
||||||
|
data[i] = data[i].split("|")
|
||||||
|
for j in range(len(data[i])):
|
||||||
|
data[i][j] = data[i][j].split("/")
|
||||||
|
if len(data[i][j]) > 1:
|
||||||
|
data[i][j].pop(-1)
|
||||||
|
if not (data[i][j] == ['']):
|
||||||
|
for k in range(len(data[i][j])):
|
||||||
|
data[i][j][k] = [int(float(x)) for x in data[i][j][k].split(",")]
|
||||||
|
|
||||||
|
self.maps = data[:]
|
||||||
|
|
||||||
|
fp = open("Data/mapReached", "rb")
|
||||||
|
self.mapNum = int(float(fp.read()))
|
||||||
|
fp.close()
|
||||||
|
|
||||||
|
fmap = open("Data/customMaps", "r")
|
||||||
|
data = fmap.read().split("\n")
|
||||||
|
fmap.close()
|
||||||
|
data.pop(-1)
|
||||||
|
|
||||||
|
for i in range(len(data)):
|
||||||
|
data[i] = data[i].split("|")
|
||||||
|
for j in range(len(data[i])):
|
||||||
|
data[i][j] = data[i][j].split("/")
|
||||||
|
if len(data[i][j]) > 1:
|
||||||
|
data[i][j].pop(-1)
|
||||||
|
if not (data[i][j] == ['']):
|
||||||
|
for k in range(len(data[i][j])):
|
||||||
|
data[i][j][k] = [int(float(x)) for x in data[i][j][k].split(",")]
|
||||||
|
|
||||||
|
self.customMaps = data[:]
|
||||||
|
|
||||||
|
self.mode = "adventure"
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 100)
|
||||||
|
|
||||||
|
def createMap(self):
|
||||||
|
global presentLevel, loop
|
||||||
|
global structure, noCannon, noMortar, noTower , noHquart, noResource
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
maxCannon = 4
|
||||||
|
maxMortar = 2
|
||||||
|
maxTower = 4
|
||||||
|
maxHquart = 1
|
||||||
|
maxResource = 10
|
||||||
|
|
||||||
|
noCannon = 0
|
||||||
|
noMortar = 0
|
||||||
|
noTower = 0
|
||||||
|
noHquart = 0
|
||||||
|
noResource = 0
|
||||||
|
|
||||||
|
canMaxLevel = 4
|
||||||
|
morMaxLevel = 3
|
||||||
|
towMaxLevel = 4
|
||||||
|
hqMaxLevel = 5
|
||||||
|
|
||||||
|
structure = []
|
||||||
|
|
||||||
|
level = []
|
||||||
|
for i in range(5):
|
||||||
|
h = 40
|
||||||
|
w = 40
|
||||||
|
x = 100*(i + 1) + w*i
|
||||||
|
y = margin + 20
|
||||||
|
newButton = Button(x, y, w, h, blue, (93, 173, 226), levelActive)
|
||||||
|
newButton.arg = i + 1
|
||||||
|
newButton.addText(str(i+1), (0, 0), 20)
|
||||||
|
level.append(newButton)
|
||||||
|
|
||||||
|
inventory = []
|
||||||
|
|
||||||
|
for i in range(5):
|
||||||
|
h = 100
|
||||||
|
x = width - 140
|
||||||
|
y = 10*(i + 1) + h*i
|
||||||
|
newButton = Button(x, y, 130, h, blue, (93, 173, 226), active)
|
||||||
|
inventory.append(newButton)
|
||||||
|
|
||||||
|
save = Button(width - 140, height - 70, 130, 50, blue, (93, 173, 226), saveMap)
|
||||||
|
save.addText("SAVE MAP", (0, 0), 20)
|
||||||
|
|
||||||
|
inventory[0].addText("CANNONS" + ": " + str(maxCannon), (0, 0), 20)
|
||||||
|
inventory[1].addText("MORTARS" + ": " + str(maxMortar), (0, 0), 20)
|
||||||
|
inventory[2].addText("TOWERS" + ": " + str(maxTower), (0, 0), 20)
|
||||||
|
inventory[3].addText("HEADQUARTERS" + ": " + str(maxHquart), (0, 0), 20)
|
||||||
|
inventory[4].addText("RESOURCE" + ": " + str(maxResource), (0, 0), 20)
|
||||||
|
|
||||||
|
inventory[0].arg = 1
|
||||||
|
inventory[1].arg = 2
|
||||||
|
inventory[2].arg = 3
|
||||||
|
inventory[3].arg = 4
|
||||||
|
inventory[4].arg = 5
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
inventory[0].updateText("CANNONS" + ": " + str(maxCannon - noCannon), (0, 0))
|
||||||
|
inventory[1].updateText("MORTARS" + ": " + str(maxMortar - noMortar), (0, 0))
|
||||||
|
inventory[2].updateText("TOWERS" + ": " + str(maxTower - noTower), (0, 0))
|
||||||
|
inventory[3].updateText("H.Q." + ": " + str(maxHquart - noHquart), (0, 0))
|
||||||
|
inventory[4].updateText("RESOURCE" + ": " + str(maxResource - noResource), (0, 0))
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_ESCAPE or event.key == pygame.K_SPACE or pygame.key == pygame.K_p:
|
||||||
|
pause()
|
||||||
|
if event.key == pygame.K_z and len(structure):
|
||||||
|
type = structure[len(structure) - 1].type
|
||||||
|
if type == "CANNON":
|
||||||
|
structure.pop(-1)
|
||||||
|
noCannon -= 1
|
||||||
|
elif type == "MORTAR":
|
||||||
|
structure.pop(-1)
|
||||||
|
noMortar -= 1
|
||||||
|
elif type == "TOWER":
|
||||||
|
structure.pop(-1)
|
||||||
|
noTower -= 1
|
||||||
|
elif type == "H.Q. ":
|
||||||
|
structure.pop(-1)
|
||||||
|
noHquart -= 1
|
||||||
|
elif type == "RESOURCE":
|
||||||
|
structure.pop(-1)
|
||||||
|
noResource -= 1
|
||||||
|
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
inventory[0].select()
|
||||||
|
inventory[1].select()
|
||||||
|
inventory[2].select()
|
||||||
|
inventory[3].select()
|
||||||
|
inventory[4].select()
|
||||||
|
|
||||||
|
level[0].select()
|
||||||
|
level[1].select()
|
||||||
|
level[2].select()
|
||||||
|
level[3].select()
|
||||||
|
level[4].select()
|
||||||
|
|
||||||
|
save.arg = structure
|
||||||
|
save.select()
|
||||||
|
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
if (margin < pos[0] < groundW + margin) and (margin < pos[1] < groundH + margin):
|
||||||
|
if (margin2 < pos[0] < validW + margin2) and (margin2 < pos[1] < validH + margin2):
|
||||||
|
if activeButton == 1 and noCannon < maxCannon:
|
||||||
|
if presentLevel > canMaxLevel:
|
||||||
|
presentLevel = canMaxLevel
|
||||||
|
newCannon = Cannon(pos[0], pos[1], canInfo["w"], canInfo["h"], canInfo["size"], canInfo["range"], canInfo["baseR"],
|
||||||
|
presentLevel, canInfo["shotR"], canInfo["shotSpeed"])
|
||||||
|
structure.append(newCannon)
|
||||||
|
noCannon += 1
|
||||||
|
if activeButton == 2 and noMortar < maxMortar:
|
||||||
|
if presentLevel > morMaxLevel:
|
||||||
|
presentLevel = morMaxLevel
|
||||||
|
newMortar = Mortar(pos[0], pos[1], mortarInfo["w"], mortarInfo["h"], mortarInfo["r"], mortarInfo["range"],
|
||||||
|
presentLevel, mortarInfo["shotR"],
|
||||||
|
mortarInfo["shotSpeed"])
|
||||||
|
structure.append(newMortar)
|
||||||
|
noMortar += 1
|
||||||
|
if activeButton == 3 and noTower < maxTower:
|
||||||
|
if presentLevel > towMaxLevel:
|
||||||
|
presentLevel = towMaxLevel
|
||||||
|
newTower = Tower(pos[0], pos[1], towerInfo["w"], towerInfo["h"], towerInfo["size"], towerInfo["range"],
|
||||||
|
towerInfo["baseR"], presentLevel, towerInfo["shotR"], towerInfo["shotSpeed"])
|
||||||
|
structure.append(newTower)
|
||||||
|
noTower += 1
|
||||||
|
if activeButton == 4 and noHquart < maxHquart:
|
||||||
|
if presentLevel > hqMaxLevel:
|
||||||
|
presentLevel = hqMaxLevel
|
||||||
|
newHQ = HeadQuarters(pos[0], pos[1], hqInfo["r"], presentLevel)
|
||||||
|
structure.append(newHQ)
|
||||||
|
noHquart += 1
|
||||||
|
if activeButton == 5 and noResource < maxResource:
|
||||||
|
newResource = Resource(pos[0], pos[1])
|
||||||
|
structure.append(newResource)
|
||||||
|
noResource += 1
|
||||||
|
|
||||||
|
display.fill((154,102,64))
|
||||||
|
pygame.draw.rect(display, (195,151,98), (margin, margin, groundW, groundH))
|
||||||
|
pygame.draw.rect(display, (154,102,64), (margin2, margin2, validW, validH))
|
||||||
|
|
||||||
|
drawBorder()
|
||||||
|
pygame.draw.rect(display, gray, (width - 150, 0, 150, height))
|
||||||
|
|
||||||
|
for struct in structure:
|
||||||
|
if struct.type == "HEADQUARTERS" or struct.type == "RESOURCE":
|
||||||
|
struct.draw()
|
||||||
|
else:
|
||||||
|
struct.draw([])
|
||||||
|
struct.shoot()
|
||||||
|
|
||||||
|
for item in inventory:
|
||||||
|
item.draw(activeButton)
|
||||||
|
|
||||||
|
if activeButton == 1:
|
||||||
|
for i in range(canMaxLevel):
|
||||||
|
level[i].draw(presentLevel)
|
||||||
|
elif activeButton == 2:
|
||||||
|
for i in range(morMaxLevel):
|
||||||
|
level[i].draw(presentLevel)
|
||||||
|
elif activeButton == 3:
|
||||||
|
for i in range(towMaxLevel):
|
||||||
|
level[i].draw(presentLevel)
|
||||||
|
elif activeButton == 4:
|
||||||
|
for i in range(hqMaxLevel):
|
||||||
|
level[i].draw(presentLevel)
|
||||||
|
|
||||||
|
save.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
def showResult(self):
|
||||||
|
def quitGame(event=0):
|
||||||
|
global loop
|
||||||
|
loop = False
|
||||||
|
if victory:
|
||||||
|
if self.mode == "adventure":
|
||||||
|
fp = open("Data/mapReached", "wb")
|
||||||
|
fp.write(str(self.mapNum + 1))
|
||||||
|
fp.close()
|
||||||
|
fp = open("Data/mapReached", "rb")
|
||||||
|
print(fp.read())
|
||||||
|
fp.close()
|
||||||
|
text = self.font.render("VICTORY", True, (255, 255, 255))
|
||||||
|
else:
|
||||||
|
text = self.font.render("LOSS", True, (255, 255, 255))
|
||||||
|
|
||||||
|
last = False
|
||||||
|
if self.mode == "adventure":
|
||||||
|
if victory and self.mapNum == len(self.maps) - 1:
|
||||||
|
endReached = self.font.render("All Bases Conquered!", True, (200, 200, 200))
|
||||||
|
pos2 = endReached.get_rect()
|
||||||
|
pos2.center = [width/2, 200]
|
||||||
|
last = True
|
||||||
|
|
||||||
|
if stars == 1:
|
||||||
|
pos = star1.get_rect()
|
||||||
|
pos.center = [width/2, height/2]
|
||||||
|
display.blit(star1, pos)
|
||||||
|
elif stars == 2:
|
||||||
|
pos = star2.get_rect()
|
||||||
|
pos.center = [width/2, height/2]
|
||||||
|
display.blit(star2, pos)
|
||||||
|
elif stars == 3:
|
||||||
|
pos = star3.get_rect()
|
||||||
|
pos.center = [width/2, height/2]
|
||||||
|
display.blit(star3, pos)
|
||||||
|
|
||||||
|
pos = text.get_rect()
|
||||||
|
pos.center = [width/2, height/2]
|
||||||
|
|
||||||
|
if victory:
|
||||||
|
next = Button(width/2 - 100 - 150, height/2 + 200, 200, 100, blue, blue, self.openMap)
|
||||||
|
if last:
|
||||||
|
next.addText("START AGAIN", (0, 0), 30)
|
||||||
|
else:
|
||||||
|
next.addText("NEXT MAP", (0, 0), 30)
|
||||||
|
next.arg = [1, self.mode]
|
||||||
|
else:
|
||||||
|
next = Button(width/2 - 100 - 150, height/2 + 200, 200, 100, blue, blue, self.openMap)
|
||||||
|
next.addText("ATTACK AGAIN", (0, 0), 30)
|
||||||
|
next.arg = [0, self.mode]
|
||||||
|
|
||||||
|
quit = Button(width/2 - 100 + 150, height/2 + 200, 200, 100, blue, blue, quitGame)
|
||||||
|
quit.addText("QUIT", (0, 0), 30)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
next.select()
|
||||||
|
quit.select()
|
||||||
|
|
||||||
|
display.blit(text, pos)
|
||||||
|
|
||||||
|
if last:
|
||||||
|
display.blit(endReached, pos2)
|
||||||
|
|
||||||
|
next.draw()
|
||||||
|
quit.draw()
|
||||||
|
|
||||||
|
if not loop:
|
||||||
|
return
|
||||||
|
|
||||||
|
clock.tick(60)
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
def openMap(self, arg):
|
||||||
|
global loop
|
||||||
|
self.mode = arg[1]
|
||||||
|
|
||||||
|
loop = True
|
||||||
|
global presentLevel
|
||||||
|
|
||||||
|
defenceStruct = []
|
||||||
|
|
||||||
|
if self.mode == "adventure":
|
||||||
|
self.mapNum = (self.mapNum + arg[0])%len(self.maps)
|
||||||
|
else:
|
||||||
|
self.mapNum = (self.mapNum + arg[0])%len(self.customMaps)
|
||||||
|
|
||||||
|
if self.mode == "adventure":
|
||||||
|
map = self.maps[self.mapNum]
|
||||||
|
else:
|
||||||
|
map = self.customMaps[self.mapNum]
|
||||||
|
|
||||||
|
cannonCoord = map[0]
|
||||||
|
mortarCoord = map[1]
|
||||||
|
towerCoord = map[2]
|
||||||
|
hqCoord = map[3]
|
||||||
|
resourceCoord = map[4]
|
||||||
|
|
||||||
|
if not (cannonCoord == [""]):
|
||||||
|
for pos in cannonCoord:
|
||||||
|
newCannon = Cannon(pos[0], pos[1], canInfo["w"], canInfo["h"], canInfo["size"],
|
||||||
|
canInfo["range"], canInfo["baseR"], pos[2],
|
||||||
|
canInfo["shotR"], canInfo["shotSpeed"])
|
||||||
|
defenceStruct.append(newCannon)
|
||||||
|
if not (mortarCoord == [""]):
|
||||||
|
for pos in mortarCoord:
|
||||||
|
newMortar = Mortar(pos[0], pos[1], mortarInfo["w"], mortarInfo["h"], mortarInfo["r"],
|
||||||
|
mortarInfo["range"], pos[2], mortarInfo["shotR"],
|
||||||
|
mortarInfo["shotSpeed"])
|
||||||
|
defenceStruct.append(newMortar)
|
||||||
|
|
||||||
|
if not (towerCoord == [""]):
|
||||||
|
for pos in towerCoord:
|
||||||
|
newTower = Tower(pos[0], pos[1], towerInfo["w"], towerInfo["h"], towerInfo["size"],
|
||||||
|
towerInfo["range"], towerInfo["baseR"], pos[2],
|
||||||
|
towerInfo["shotR"], towerInfo["shotSpeed"])
|
||||||
|
defenceStruct.append(newTower)
|
||||||
|
|
||||||
|
if not (hqCoord == [""]):
|
||||||
|
for pos in hqCoord:
|
||||||
|
newHQ = HeadQuarters(pos[0], pos[1], hqInfo["r"], pos[2])
|
||||||
|
defenceStruct.append(newHQ)
|
||||||
|
|
||||||
|
if not (resourceCoord == [""]):
|
||||||
|
for pos in resourceCoord:
|
||||||
|
newResource = Resource(pos[0], pos[1])
|
||||||
|
defenceStruct.append(newResource)
|
||||||
|
|
||||||
|
if self.mode == "custom":
|
||||||
|
level = []
|
||||||
|
for i in range(5):
|
||||||
|
h = 40
|
||||||
|
w = 40
|
||||||
|
x = width - 75 - 20
|
||||||
|
y = height/2 + (h + 10)*i
|
||||||
|
newButton = Button(x, y, w, h, blue, (93, 173, 226), levelActive)
|
||||||
|
newButton.arg = i + 1
|
||||||
|
newButton.addText(str(i+1), (0, 0), 20)
|
||||||
|
level.append(newButton)
|
||||||
|
|
||||||
|
troops = []
|
||||||
|
|
||||||
|
inventory = []
|
||||||
|
for i in range(3):
|
||||||
|
h = 100
|
||||||
|
x = width - 140
|
||||||
|
y = 10*(i + 1) + h*i
|
||||||
|
newButton = Button(x, y, 130, h, blue, (93, 173, 226), active)
|
||||||
|
inventory.append(newButton)
|
||||||
|
|
||||||
|
if self.mode == "custom":
|
||||||
|
maxShooter = 20
|
||||||
|
maxTank = 5
|
||||||
|
maxHeli = 3
|
||||||
|
shooterMaxLevel = 5
|
||||||
|
tankMaxLevel = 4
|
||||||
|
heliMaxLevel = 3
|
||||||
|
else:
|
||||||
|
if self.mapNum < 4:
|
||||||
|
maxShooter = 12
|
||||||
|
maxTank = 0
|
||||||
|
maxHeli = 0
|
||||||
|
shooterMaxLevel = 1
|
||||||
|
tankMaxLevel = 0
|
||||||
|
heliMaxLevel = 0
|
||||||
|
elif self.mapNum < 8:
|
||||||
|
maxShooter = 15
|
||||||
|
maxTank = 1
|
||||||
|
maxHeli = 0
|
||||||
|
shooterMaxLevel = 2
|
||||||
|
tankMaxLevel = 1
|
||||||
|
heliMaxLevel = 0
|
||||||
|
elif self.mapNum < 12:
|
||||||
|
maxShooter = 15
|
||||||
|
maxTank = 2
|
||||||
|
maxHeli = 1
|
||||||
|
shooterMaxLevel = 3
|
||||||
|
tankMaxLevel = 2
|
||||||
|
heliMaxLevel = 1
|
||||||
|
elif self.mapNum < 16:
|
||||||
|
maxShooter = 18
|
||||||
|
maxTank = 3
|
||||||
|
maxHeli = 2
|
||||||
|
shooterMaxLevel = 4
|
||||||
|
tankMaxLevel = 3
|
||||||
|
heliMaxLevel = 2
|
||||||
|
else:
|
||||||
|
maxShooter = 20
|
||||||
|
maxTank = 5
|
||||||
|
maxHeli = 3
|
||||||
|
shooterMaxLevel = 5
|
||||||
|
tankMaxLevel = 4
|
||||||
|
heliMaxLevel = 3
|
||||||
|
|
||||||
|
if self.mode == "custom":
|
||||||
|
next = Button(width - 130, height - 100, 100, 40, blue, blue, self.openMap)
|
||||||
|
next.addText("NEXT MAP", (0, 0), 20)
|
||||||
|
next.arg = [1, self.mode]
|
||||||
|
|
||||||
|
noShooter = 0
|
||||||
|
noTank = 0
|
||||||
|
noHeli = 0
|
||||||
|
|
||||||
|
inventory[0].addText("SHOOTER" + ": " + str(maxShooter), (0, 0), 20)
|
||||||
|
inventory[1].addText("TANK" + ": " + str(maxTank), (0, 0), 20)
|
||||||
|
inventory[2].addText("HELICOPTER" + ": " + str(maxHeli), (0, 0), 20)
|
||||||
|
|
||||||
|
inventory[0].arg = 1
|
||||||
|
inventory[1].arg = 2
|
||||||
|
inventory[2].arg = 3
|
||||||
|
|
||||||
|
initStruct = len(defenceStruct)
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
inventory[0].updateText("SHOOTER" + ": " + str(maxShooter - noShooter), (0, 0))
|
||||||
|
inventory[1].updateText("TANK" + ": " + str(maxTank - noTank), (0, 0))
|
||||||
|
inventory[2].updateText("HELICOPTER" + ": " + str(maxHeli - noHeli), (0, 0))
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_ESCAPE or event.key == pygame.K_SPACE or pygame.key == pygame.K_p:
|
||||||
|
pause()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
inventory[0].select()
|
||||||
|
inventory[1].select()
|
||||||
|
inventory[2].select()
|
||||||
|
|
||||||
|
if self.mode == "custom":
|
||||||
|
level[0].select()
|
||||||
|
level[1].select()
|
||||||
|
level[2].select()
|
||||||
|
level[3].select()
|
||||||
|
level[4].select()
|
||||||
|
next.select()
|
||||||
|
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
if (margin < pos[0] < groundW + margin) and (margin < pos[1] < groundH + margin):
|
||||||
|
if not ((margin2 < pos[0] < margin2 + validW) and (margin2 < pos[1] < margin2 + validH)):
|
||||||
|
if activeButton == 1 and noShooter < maxShooter:
|
||||||
|
if self.mode == "custom":
|
||||||
|
if presentLevel > shooterMaxLevel:
|
||||||
|
presentLevel = shooterMaxLevel
|
||||||
|
newShooter = Shooter(pos[0], pos[1], 5, 1.2, 180, presentLevel, 5, 2)
|
||||||
|
else:
|
||||||
|
newShooter = Shooter(pos[0], pos[1], 5, 1.2, 180, shooterMaxLevel, 5, 2)
|
||||||
|
troops.insert(0, newShooter)
|
||||||
|
noShooter += 1
|
||||||
|
if activeButton == 2 and noTank < maxTank:
|
||||||
|
if self.mode == "custom":
|
||||||
|
if presentLevel > tankMaxLevel:
|
||||||
|
presentLevel = tankMaxLevel
|
||||||
|
newTank = Tank(pos[0], pos[1], 20, 1, 200, presentLevel, 6, 2)
|
||||||
|
else:
|
||||||
|
newTank = Tank(pos[0], pos[1], 20, 1, 200, tankMaxLevel, 6, 2)
|
||||||
|
troops.insert(0, newTank)
|
||||||
|
noTank += 1
|
||||||
|
if activeButton == 3 and noHeli < maxHeli:
|
||||||
|
if self.mode == "custom":
|
||||||
|
if presentLevel > heliMaxLevel:
|
||||||
|
presentLevel = heliMaxLevel
|
||||||
|
newHeli = Helicopter(pos[0], pos[1], 30, 0.7, 200, presentLevel, 7, 10)
|
||||||
|
else:
|
||||||
|
newHeli = Helicopter(pos[0], pos[1], 30, 0.7, 200, heliMaxLevel, 7, 10)
|
||||||
|
troops.append(newHeli)
|
||||||
|
noHeli += 1
|
||||||
|
|
||||||
|
display.fill((154,102,64))
|
||||||
|
pygame.draw.rect(display, (195,151,98), (margin, margin, groundW, groundH))
|
||||||
|
pygame.draw.rect(display, (154,102,64), (margin2, margin2, validW, validH))
|
||||||
|
|
||||||
|
drawBorder()
|
||||||
|
pygame.draw.rect(display, gray, (width - 150, 0, 150, height))
|
||||||
|
for item in inventory:
|
||||||
|
item.draw(activeButton)
|
||||||
|
|
||||||
|
for troop in troops:
|
||||||
|
troop.move(defenceStruct)
|
||||||
|
troop.draw()
|
||||||
|
troop.shoot()
|
||||||
|
|
||||||
|
troop.updateHealth(defenceStruct)
|
||||||
|
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE"):
|
||||||
|
struct.removeHit()
|
||||||
|
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if struct.type == "HEADQUARTERS":
|
||||||
|
struct.draw()
|
||||||
|
elif struct.type == "RESOURCE":
|
||||||
|
struct.draw()
|
||||||
|
else:
|
||||||
|
struct.draw(troops)
|
||||||
|
struct.shoot()
|
||||||
|
|
||||||
|
struct.updateHealth(troops)
|
||||||
|
|
||||||
|
for troop in troops:
|
||||||
|
troop.removeHit()
|
||||||
|
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if struct.health <= 0:
|
||||||
|
defenceStruct.remove(struct)
|
||||||
|
|
||||||
|
for troop in troops:
|
||||||
|
if troop.health <= 0:
|
||||||
|
troops.remove(troop)
|
||||||
|
|
||||||
|
if self.mode == "custom":
|
||||||
|
if activeButton == 1:
|
||||||
|
for i in range(shooterMaxLevel):
|
||||||
|
level[i].draw(presentLevel)
|
||||||
|
elif activeButton == 2:
|
||||||
|
for i in range(tankMaxLevel):
|
||||||
|
level[i].draw(presentLevel)
|
||||||
|
if activeButton == 3 :
|
||||||
|
for i in range(heliMaxLevel):
|
||||||
|
level[i].draw(presentLevel)
|
||||||
|
|
||||||
|
|
||||||
|
if self.mode == "custom" and noShooter == 0 and noTank == 0 and noHeli == 0:
|
||||||
|
next.draw()
|
||||||
|
|
||||||
|
damageDone(1.0 - float(len(defenceStruct))/initStruct, defenceStruct)
|
||||||
|
if (defenceStruct == []) or (noShooter == maxShooter and noTank == maxTank and noHeli == maxHeli and troops == []):
|
||||||
|
self.showResult()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
clock.tick(90)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
width = 1260
|
||||||
|
height = 720
|
||||||
|
|
||||||
|
screen = pygame.display.set_mode((width, height))
|
||||||
|
|
||||||
|
initMaps(screen, (width, height), 25, 100)
|
||||||
|
|
||||||
|
newMap = Maps()
|
||||||
|
newMap.openMap([0, "adventure"])
|
||||||
|
#newMap.createMap()
|
||||||
|
close()
|
716
Battles/Code/Structures.py
Normal file
@ -0,0 +1,716 @@
|
|||||||
|
"""
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
Structures.py
|
||||||
|
~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
By - JATIN KUMAR MANDAV
|
||||||
|
|
||||||
|
This is a library for all structures used in Game
|
||||||
|
Structures: 1. CANNONS
|
||||||
|
2. MORTARS
|
||||||
|
3. TOWERS
|
||||||
|
4. HEADQUARTERS
|
||||||
|
5. RESOURCE BUILDINGS
|
||||||
|
|
||||||
|
This is completely coded using PYGAME library of PYTHON 2.7
|
||||||
|
|
||||||
|
Lines - 717
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Imports
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
import random
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
display = None
|
||||||
|
width = 0
|
||||||
|
height = 0
|
||||||
|
margin = 0
|
||||||
|
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
def initStruct(screen, size, border=0):
|
||||||
|
global display, width, height, margin
|
||||||
|
display = screen
|
||||||
|
width = size[0]
|
||||||
|
height = size[1]
|
||||||
|
margin = border
|
||||||
|
|
||||||
|
def getCoordPoly(x, y, r, n, rotate=0, anchor=(0, 0)):
|
||||||
|
coords = []
|
||||||
|
for i in range(n):
|
||||||
|
coords.append([x - anchor[0] + r*cos(2*pi*i/n + rotate), y - anchor[1] + r*sin(2*pi*i/n + rotate)])
|
||||||
|
|
||||||
|
return coords[:]
|
||||||
|
|
||||||
|
class Shots:
|
||||||
|
def __init__(self, x, y, r1, speed, angle, color=(0, 0, 0), type="circle", r2=0):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.r1 = r1
|
||||||
|
if r2 == 0:
|
||||||
|
self.r2 = r1
|
||||||
|
else:
|
||||||
|
self.r2 = r2
|
||||||
|
|
||||||
|
self.type = type
|
||||||
|
|
||||||
|
self.speed = speed
|
||||||
|
self.angle = angle
|
||||||
|
self.color = color
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
if self.type == "circle":
|
||||||
|
pygame.draw.ellipse(display, self.color, (self.x - self.r1, self.y - self.r2, self.r1*2, self.r2*2))
|
||||||
|
elif self.type == "line":
|
||||||
|
x2 = self.x + self.r1*cos(self.angle)
|
||||||
|
y2 = self.y + self.r1*sin(self.angle)
|
||||||
|
pygame.draw.line(display, self.color, (self.x, self.y), (x2, y2), 2)
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.x += self.speed*cos(self.angle)
|
||||||
|
self.y += self.speed*sin(self.angle)
|
||||||
|
|
||||||
|
class Cannon:
|
||||||
|
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)):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
self.xOld = self.x
|
||||||
|
self.yOld = self.y
|
||||||
|
self.angle = 0
|
||||||
|
|
||||||
|
self.r = size
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.type = "CANNON"
|
||||||
|
self.attackType = "GROUND"
|
||||||
|
self.baseR = baseR
|
||||||
|
|
||||||
|
self.radius = radius
|
||||||
|
self.speed = speed
|
||||||
|
|
||||||
|
self.range = range
|
||||||
|
self.coord = getCoordPoly(self.x, self.y, self.r, 4)
|
||||||
|
self.rectCoord = []
|
||||||
|
|
||||||
|
self.shots = []
|
||||||
|
|
||||||
|
self.recoil = 3
|
||||||
|
self.rebound = 0.1
|
||||||
|
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
self.colorBarrel = (23, 32, 42)
|
||||||
|
self.colorHead = (39, 55, 70)
|
||||||
|
self.colorBase = (123, 125, 125)
|
||||||
|
self.hitPoint = 420
|
||||||
|
self.damage = 8
|
||||||
|
elif self.level == 2:
|
||||||
|
self.colorBarrel = (11, 83, 69)
|
||||||
|
self.colorHead = (30, 132, 73)
|
||||||
|
self.colorBase = (23, 32, 42)
|
||||||
|
self.hitPoint = 640
|
||||||
|
self.damage = 33
|
||||||
|
self.headCover = (69, 179, 157)
|
||||||
|
elif self.level == 3:
|
||||||
|
self.colorBarrel = (21, 67, 96)
|
||||||
|
self.colorHead = (40, 116, 166)
|
||||||
|
self.colorBase = (144, 148, 151)
|
||||||
|
self.hitPoint = 830
|
||||||
|
self.damage = 32
|
||||||
|
self.rectCoord2 = []
|
||||||
|
elif self.level == 4:
|
||||||
|
self.colorBarrel = (23, 32, 42)
|
||||||
|
self.colorHead = (46, 64, 83)
|
||||||
|
self.colorBase = (144, 148, 151)
|
||||||
|
self.hitPoint = 1200
|
||||||
|
self.damage = 54
|
||||||
|
self.rectCoord2 = []
|
||||||
|
self.headCover = (133, 146, 158)
|
||||||
|
|
||||||
|
self.health = self.hitPoint
|
||||||
|
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 20)
|
||||||
|
|
||||||
|
def updateHealth(self, troops):
|
||||||
|
error = self.baseR
|
||||||
|
for troop in troops:
|
||||||
|
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in troop.shots:
|
||||||
|
if troop.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= troop.damage
|
||||||
|
|
||||||
|
def isHit(self, coord):
|
||||||
|
error = self.baseR
|
||||||
|
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
|
||||||
|
if dist < error:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def removeHit(self):
|
||||||
|
tempList = self.shots[:]
|
||||||
|
for shot in self.shots:
|
||||||
|
if self.isHit((shot.x, shot.y)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0)):
|
||||||
|
corr = 270
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord):
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
|
||||||
|
def shoot(self):
|
||||||
|
error = 0.05
|
||||||
|
|
||||||
|
if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)):
|
||||||
|
self.x += self.rebound*cos(self.angle)
|
||||||
|
self.y += self.rebound*sin(self.angle)
|
||||||
|
else:
|
||||||
|
pos = self.nearestPos
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
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")
|
||||||
|
self.shots.append(shot)
|
||||||
|
if self.level >= 3:
|
||||||
|
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")
|
||||||
|
self.shots.append(shot)
|
||||||
|
self.x -= self.recoil*cos(self.angle)
|
||||||
|
self.y -= self.recoil*sin(self.angle)
|
||||||
|
|
||||||
|
tempList = self.shots[:]
|
||||||
|
|
||||||
|
for shot in self.shots:
|
||||||
|
shot.move()
|
||||||
|
shot.draw()
|
||||||
|
if not ((margin < shot.x < width) and (margin < shot.y < height)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def draw(self, troops):
|
||||||
|
if len(troops):
|
||||||
|
nearestPos = 0
|
||||||
|
lowestDist = float("inf")
|
||||||
|
for struct in troops:
|
||||||
|
if struct.type == "GROUND":
|
||||||
|
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
|
||||||
|
if lowestDist > dist:
|
||||||
|
self.nearestPos = [struct.x, struct.y]
|
||||||
|
lowestDist = dist
|
||||||
|
else:
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
pos = self.nearestPos
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
points = [(0, 0), (0, self.h), (self.w, self.h), (self.w, 0)]
|
||||||
|
|
||||||
|
self.coord = getCoordPoly(self.x, self.y, self.r/2, 6, rotate)
|
||||||
|
|
||||||
|
if self.level <= 3:
|
||||||
|
self.rectCoord = []
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.rectCoord.append(self.translate(self.rotate(point, rotate, (self.w/2, 0))))
|
||||||
|
if self.level >= 3:
|
||||||
|
w = self.w/2
|
||||||
|
h = self.h
|
||||||
|
self.rectCoord = []
|
||||||
|
self.rectCoord2 = []
|
||||||
|
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2 + w, 0))))
|
||||||
|
self.rectCoord2.append(self.translate(self.rotate(point, rotate, (w/2 - w, 0))))
|
||||||
|
|
||||||
|
|
||||||
|
baseCoord = getCoordPoly(self.xOld, self.yOld, self.baseR, 4, radians(45))
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.baseR*2, 40, 8))
|
||||||
|
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))
|
||||||
|
|
||||||
|
pygame.draw.polygon(display, self.colorBase, baseCoord)
|
||||||
|
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord)
|
||||||
|
if self.level >= 3:
|
||||||
|
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord2)
|
||||||
|
pygame.draw.polygon(display, self.colorHead, self.coord)
|
||||||
|
|
||||||
|
if self.level == 2 or self.level == 4:
|
||||||
|
pygame.draw.ellipse(display, self.headCover, (self.x - self.baseR/3, self.y - self.baseR/3, self.baseR*2/3, self.baseR*2/3))
|
||||||
|
|
||||||
|
#pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1)
|
||||||
|
|
||||||
|
class Tower:
|
||||||
|
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)):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
self.xOld = self.x
|
||||||
|
self.yOld = self.y
|
||||||
|
self.angle = 0
|
||||||
|
|
||||||
|
self.r = size
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.type = "TOWER"
|
||||||
|
self.baseR = baseR
|
||||||
|
|
||||||
|
self.radius = radius
|
||||||
|
self.speed = speed
|
||||||
|
|
||||||
|
self.range = range
|
||||||
|
self.coord = getCoordPoly(self.x, self.y, self.r, 4)
|
||||||
|
self.rectCoord = []
|
||||||
|
|
||||||
|
self.attackType = "GROUND AND AIR"
|
||||||
|
|
||||||
|
self.shots = []
|
||||||
|
|
||||||
|
self.recoil = 5
|
||||||
|
self.rebound = 0.25
|
||||||
|
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
self.colorBarrel = (100, 30, 22)
|
||||||
|
self.colorHead = (100, 30, 22)
|
||||||
|
self.colorBase = (26, 82, 118)
|
||||||
|
self.hitPoint = 380
|
||||||
|
self.damage = 10
|
||||||
|
elif self.level == 2:
|
||||||
|
self.colorBarrel = (100, 30, 22)
|
||||||
|
self.colorHead = (100, 30, 22)
|
||||||
|
self.colorBase = (125, 102, 8)
|
||||||
|
self.hitPoint = 580
|
||||||
|
self.damage = 30
|
||||||
|
elif self.level == 3:
|
||||||
|
self.colorBarrel = (183, 149, 11)
|
||||||
|
self.colorHead = (185, 119, 14)
|
||||||
|
self.colorBase = (11, 83, 69)
|
||||||
|
self.colorBase2 = (39, 174, 96)
|
||||||
|
self.hitPoint = 810
|
||||||
|
self.damage = 60
|
||||||
|
elif self.level == 4:
|
||||||
|
self.colorBarrel = (23, 32, 42)
|
||||||
|
self.colorHead = (52, 73, 94)
|
||||||
|
self.colorBase = (21, 67, 96)
|
||||||
|
self.colorBase2 = (98, 101, 103)
|
||||||
|
self.hitPoint = 1230
|
||||||
|
self.damage = 90
|
||||||
|
|
||||||
|
self.health = self.hitPoint
|
||||||
|
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 20)
|
||||||
|
|
||||||
|
def updateHealth(self, troops):
|
||||||
|
error = self.r
|
||||||
|
for troop in troops:
|
||||||
|
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in troop.shots:
|
||||||
|
if troop.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= troop.damage
|
||||||
|
|
||||||
|
def isHit(self, coord):
|
||||||
|
error = self.r
|
||||||
|
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
|
||||||
|
if dist < error:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def removeHit(self):
|
||||||
|
tempList = self.shots[:]
|
||||||
|
for shot in self.shots:
|
||||||
|
if self.isHit((shot.x, shot.y)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0)):
|
||||||
|
corr = 270
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord):
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
|
||||||
|
def shoot(self):
|
||||||
|
error = 0.1
|
||||||
|
|
||||||
|
if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)):
|
||||||
|
self.x += self.rebound*cos(self.angle)
|
||||||
|
self.y += self.rebound*sin(self.angle)
|
||||||
|
else:
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
pos = self.nearestPos
|
||||||
|
dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
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")
|
||||||
|
self.shots.append(shot)
|
||||||
|
self.x -= self.recoil*cos(self.angle)
|
||||||
|
self.y -= self.recoil*sin(self.angle)
|
||||||
|
|
||||||
|
tempList = self.shots[:]
|
||||||
|
|
||||||
|
for shot in self.shots:
|
||||||
|
shot.move()
|
||||||
|
shot.draw()
|
||||||
|
if not ((margin < shot.x < width) and (margin < shot.y < height)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def draw(self, troops):
|
||||||
|
if len(troops):
|
||||||
|
nearestPos = 0
|
||||||
|
lowestDist = float("inf")
|
||||||
|
for struct in troops:
|
||||||
|
if struct.type == "AIR" or struct.type == "GROUND":
|
||||||
|
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
|
||||||
|
if lowestDist > dist:
|
||||||
|
self.nearestPos = [struct.x, struct.y]
|
||||||
|
lowestDist = dist
|
||||||
|
else:
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
else:
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
pos = self.nearestPos
|
||||||
|
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
if self.level == 4:
|
||||||
|
w = self.w*2
|
||||||
|
h = self.h
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
self.rectCoord = []
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0))))
|
||||||
|
else:
|
||||||
|
w = self.w
|
||||||
|
h = self.h
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
self.rectCoord = []
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.rectCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0))))
|
||||||
|
|
||||||
|
baseCoord = getCoordPoly(self.xOld, self.yOld, self.baseR, 6)
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.baseR*2, 40, 8))
|
||||||
|
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))
|
||||||
|
|
||||||
|
pygame.draw.polygon(display, self.colorBase, baseCoord)
|
||||||
|
|
||||||
|
if self.level >= 3:
|
||||||
|
baseCoord2 = getCoordPoly(self.xOld, self.yOld, self.baseR*3/4, 6)
|
||||||
|
pygame.draw.polygon(display, self.colorBase2, baseCoord2)
|
||||||
|
|
||||||
|
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord)
|
||||||
|
#pygame.draw.polygon(display, self.colorHead, self.coord)
|
||||||
|
pygame.draw.ellipse(display, self.colorHead, (self.x - self.r/2, self.y - self.r/2, self.r, self.r ))
|
||||||
|
|
||||||
|
#pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1)
|
||||||
|
|
||||||
|
class Mortar:
|
||||||
|
def __init__(self, x, y, w, h, r, range, level, radius, speed, colorBarrel=(0, 0, 0), colorHead=(0, 0, 0), colorBase=(255, 0, 0)):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
self.xOld = self.x
|
||||||
|
self.yOld = self.y
|
||||||
|
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.r = r
|
||||||
|
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
self.colorBarrel = (95, 106, 106)
|
||||||
|
self.colorHead = (46, 64, 83)
|
||||||
|
self.colorBase = (146, 43, 33)
|
||||||
|
self.range = range
|
||||||
|
self.hitPoint = 400
|
||||||
|
self.damage = 37
|
||||||
|
elif self.level == 2:
|
||||||
|
self.colorBarrel = (125, 102, 8)
|
||||||
|
self.colorHead = (11, 83, 69)
|
||||||
|
self.colorBase = (166, 172, 175)
|
||||||
|
self.colorHeadCover = (34, 153, 84)
|
||||||
|
self.range = range
|
||||||
|
self.hitPoint = 650
|
||||||
|
self.damage = 70
|
||||||
|
elif self.level == 3:
|
||||||
|
self.colorBarrel = (21, 67, 96)
|
||||||
|
self.colorHead = (36, 113, 163)
|
||||||
|
self.colorBase = (113, 125, 126)
|
||||||
|
self.colorBarrelHead = (46, 64, 83)
|
||||||
|
self.range = range
|
||||||
|
self.hitPoint = 850
|
||||||
|
self.damage = 130
|
||||||
|
|
||||||
|
self.radius = radius
|
||||||
|
self.speed = speed
|
||||||
|
|
||||||
|
self.type = "MORTAR"
|
||||||
|
self.attackType = "GROUND"
|
||||||
|
|
||||||
|
self.coord = []
|
||||||
|
self.rectCoord = []
|
||||||
|
|
||||||
|
self.shots = []
|
||||||
|
|
||||||
|
self.angle = 0
|
||||||
|
self.recoil = 7
|
||||||
|
self.rebound = 0.1
|
||||||
|
|
||||||
|
self.health = self.hitPoint
|
||||||
|
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 20)
|
||||||
|
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
def updateHealth(self, troops):
|
||||||
|
error = 15
|
||||||
|
for troop in troops:
|
||||||
|
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in troop.shots:
|
||||||
|
if troop.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= troop.damage
|
||||||
|
|
||||||
|
def isHit(self, coord):
|
||||||
|
error = 50
|
||||||
|
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
|
||||||
|
if dist < self.r:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def removeHit(self):
|
||||||
|
tempList = self.shots[:]
|
||||||
|
for shot in self.shots:
|
||||||
|
if self.isHit((shot.x, shot.y)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def shoot(self):
|
||||||
|
error = 0.05
|
||||||
|
|
||||||
|
if not ((abs(self.x - self.xOld) < error) and (abs(self.y - self.yOld) < error)):
|
||||||
|
self.x += self.rebound*cos(self.angle)
|
||||||
|
self.y += self.rebound*sin(self.angle)
|
||||||
|
else:
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
pos = self.nearestPos
|
||||||
|
dist = ((self.xOld - pos[0])**2 + (self.yOld - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.yOld, pos[0] - self.xOld)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
self.x -= self.recoil*cos(self.angle)
|
||||||
|
self.y -= self.recoil*sin(self.angle)
|
||||||
|
|
||||||
|
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)
|
||||||
|
self.shots.append(shot)
|
||||||
|
|
||||||
|
tempList = self.shots[:]
|
||||||
|
|
||||||
|
for shot in self.shots:
|
||||||
|
shot.move()
|
||||||
|
shot.draw()
|
||||||
|
if not ((margin < shot.x < width) and (margin < shot.y < height)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0)):
|
||||||
|
corr = 270
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord):
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
|
||||||
|
def draw(self, troops):
|
||||||
|
if len(troops):
|
||||||
|
nearestPos = 0
|
||||||
|
lowestDist = float("inf")
|
||||||
|
for struct in troops:
|
||||||
|
if struct.type == "GROUND":
|
||||||
|
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
|
||||||
|
if lowestDist > dist:
|
||||||
|
self.nearestPos = [struct.x, struct.y]
|
||||||
|
lowestDist = dist
|
||||||
|
else:
|
||||||
|
self.neasestPos = pygame.mouse.get_pos()
|
||||||
|
else:
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
pos = self.nearestPos
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
points = [(0, 0), (0, self.h), (self.w, self.h), (self.w, 0)]
|
||||||
|
|
||||||
|
self.coord = getCoordPoly(self.x, self.y, self.w*5/8, 8, rotate)
|
||||||
|
|
||||||
|
self.rectCoord = []
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.rectCoord.append(self.translate(self.rotate(point, rotate, (self.w/2, 0))))
|
||||||
|
|
||||||
|
baseCoord = getCoordPoly(self.xOld, self.yOld, self.r*2/5, 10)
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r, 40, 8))
|
||||||
|
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))
|
||||||
|
|
||||||
|
pygame.draw.polygon(display, self.colorBase, baseCoord)
|
||||||
|
pygame.draw.polygon(display, self.colorBarrel, self.rectCoord)
|
||||||
|
pygame.draw.polygon(display, self.colorHead, self.coord)
|
||||||
|
if self.level == 2:
|
||||||
|
coord = getCoordPoly(self.x, self.y, self.w*3/8, 8, rotate)
|
||||||
|
pygame.draw.polygon(display, self.colorHeadCover, coord)
|
||||||
|
if self.level == 3:
|
||||||
|
coord = []
|
||||||
|
w = self.w*1.2
|
||||||
|
h = self.h/5
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
coord.append(self.translate(self.rotate(point, rotate, (w/2, h/2 - self.h))))
|
||||||
|
pygame.draw.polygon(display, self.colorBarrelHead, coord)
|
||||||
|
#pygame.draw.ellipse(display, (0, 0, 0), (self.xOld - self.range/2, self.yOld - self.range/2, self.range, self.range), 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class HeadQuarters:
|
||||||
|
def __init__(self, x, y, r, level, color1=(0, 0, 0), color2=(255, 255, 0)):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.r = r
|
||||||
|
self.type = "HEADQUARTERS"
|
||||||
|
self.color1 = (23, 32, 42)
|
||||||
|
self.color2 = (113, 125, 126)
|
||||||
|
|
||||||
|
self.level = level + 1
|
||||||
|
if self.level == 2:
|
||||||
|
self.hitPoint = 570
|
||||||
|
elif self.level == 3:
|
||||||
|
self.hitPoint = 1500
|
||||||
|
elif self.level == 4:
|
||||||
|
self.hitPoint = 3000
|
||||||
|
elif self.level == 5:
|
||||||
|
self.hitPoint = 4500
|
||||||
|
elif self.level == 6:
|
||||||
|
self.hitPoint = 6000
|
||||||
|
|
||||||
|
self.health = self.hitPoint
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 20)
|
||||||
|
|
||||||
|
def updateHealth(self, troops):
|
||||||
|
error = 15
|
||||||
|
for troop in troops:
|
||||||
|
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in troop.shots:
|
||||||
|
if troop.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= troop.damage
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*1.5, 40, 8))
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
|
outer = []
|
||||||
|
inner = []
|
||||||
|
|
||||||
|
for i in range(self.level):
|
||||||
|
coord = getCoordPoly(self.x, self.y, self.r - (2.0/3)*self.r*(i)/(self.level), 5)
|
||||||
|
if i == 0:
|
||||||
|
outer = coord
|
||||||
|
elif i == self.level - 1:
|
||||||
|
inner = coord
|
||||||
|
if i%2 == 0:
|
||||||
|
pygame.draw.polygon(display, self.color1, coord)
|
||||||
|
else:
|
||||||
|
pygame.draw.polygon(display, self.color2, coord)
|
||||||
|
|
||||||
|
for i in range(5):
|
||||||
|
pygame.draw.line(display, self.color2, outer[i], inner[i], 3)
|
||||||
|
|
||||||
|
gray = (121, 125, 127)
|
||||||
|
darkGray = (28, 40, 51)
|
||||||
|
yellow = (212, 172, 13)
|
||||||
|
red = (203, 67, 53)
|
||||||
|
orange = (202, 111, 30)
|
||||||
|
blue = (36, 113, 163)
|
||||||
|
green = (17, 122, 101)
|
||||||
|
violet = (125, 60, 152)
|
||||||
|
|
||||||
|
class Resource:
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.r = random.randrange(20, 40)
|
||||||
|
self.type = "RESOURCE"
|
||||||
|
colors = [gray, darkGray, yellow, red, orange, blue, green, violet]
|
||||||
|
self.color1 = random.choice(colors)
|
||||||
|
self.color2 = random.choice(colors)
|
||||||
|
|
||||||
|
self.sides = random.randrange(4, 10)
|
||||||
|
|
||||||
|
self.hitPoint = random.randrange(250, 700)
|
||||||
|
self.health = self.hitPoint
|
||||||
|
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 20)
|
||||||
|
|
||||||
|
def updateHealth(self, troops):
|
||||||
|
error = 15
|
||||||
|
for troop in troops:
|
||||||
|
if (abs(troop.nearestPos[0] - self.x) < error) and (abs(troop.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in troop.shots:
|
||||||
|
if troop.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= troop.damage
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*1.5, 40, 8))
|
||||||
|
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))
|
||||||
|
|
||||||
|
outer = []
|
||||||
|
inner = []
|
||||||
|
|
||||||
|
level = self.sides
|
||||||
|
for i in range(level):
|
||||||
|
coord = getCoordPoly(self.x, self.y, self.r - (2.0/3)*self.r*(i)/(self.sides), self.sides)
|
||||||
|
if i == 0:
|
||||||
|
outer = coord
|
||||||
|
elif i == level - 1:
|
||||||
|
inner = coord
|
||||||
|
if i%2 == 0:
|
||||||
|
pygame.draw.polygon(display, self.color1, coord)
|
||||||
|
else:
|
||||||
|
pygame.draw.polygon(display, self.color2, coord)
|
||||||
|
|
||||||
|
for i in range(level):
|
||||||
|
pygame.draw.line(display, self.color2, outer[i], inner[i], 3)
|
776
Battles/Code/Troops.py
Normal file
@ -0,0 +1,776 @@
|
|||||||
|
"""
|
||||||
|
~~~~~~~~~
|
||||||
|
Troops.py
|
||||||
|
~~~~~~~~~
|
||||||
|
|
||||||
|
By - JATIN KUMAR MANDAV
|
||||||
|
|
||||||
|
This library contains different Troops classes for the game
|
||||||
|
Troops: 1. SHOOTERS
|
||||||
|
2. TANKS
|
||||||
|
3. HELICOPTERS
|
||||||
|
|
||||||
|
Also contains funcions to draw different shapes
|
||||||
|
|
||||||
|
This is completely coded in using PYGAME library of PYTHON 2.7
|
||||||
|
|
||||||
|
Lines - 777
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Imports
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
|
||||||
|
# Initialize PyGame
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
margin = 0
|
||||||
|
width = 400
|
||||||
|
height = 400
|
||||||
|
display = None
|
||||||
|
|
||||||
|
def initTroops(screen, size, m):
|
||||||
|
global display, width, height, margin
|
||||||
|
display = screen
|
||||||
|
width = size[0]
|
||||||
|
height = size[1]
|
||||||
|
margin = m
|
||||||
|
|
||||||
|
|
||||||
|
def getCoordPoly(x, y, r, n, rotate=0, anchor=(0, 0)):
|
||||||
|
coords = []
|
||||||
|
for i in range(n):
|
||||||
|
coords.append([x - anchor[0] + r*cos(2*pi*i/n + rotate), y - anchor[1] + r*sin(2*pi*i/n + rotate)])
|
||||||
|
|
||||||
|
#pygame.draw.polygon(display, black, coords, 1)
|
||||||
|
|
||||||
|
return coords[:]
|
||||||
|
|
||||||
|
class Shots:
|
||||||
|
def __init__(self, x, y, r1, speed, angle, color=(0, 0, 0), type="circle", r2=0):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.r1 = r1
|
||||||
|
if r2 == 0:
|
||||||
|
self.r2 = r1
|
||||||
|
else:
|
||||||
|
self.r2 = r2
|
||||||
|
|
||||||
|
self.type = type
|
||||||
|
|
||||||
|
self.speed = speed
|
||||||
|
self.angle = angle
|
||||||
|
self.color = color
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
if self.type == "circle":
|
||||||
|
pygame.draw.ellipse(display, self.color, (self.x - self.r1, self.y - self.r2, self.r1*2, self.r2*2))
|
||||||
|
elif self.type == "line":
|
||||||
|
x2 = self.x + self.r1*cos(self.angle)
|
||||||
|
y2 = self.y + self.r1*sin(self.angle)
|
||||||
|
pygame.draw.line(display, self.color, (self.x, self.y), (x2, y2), 2)
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.x += self.speed*cos(self.angle)
|
||||||
|
self.y += self.speed*sin(self.angle)
|
||||||
|
|
||||||
|
|
||||||
|
class Shooter:
|
||||||
|
def __init__(self, x, y, r, speed, range, level, shotSpeed, radius):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
self.range = range
|
||||||
|
self.shotSpeed = shotSpeed
|
||||||
|
self.radius = radius
|
||||||
|
|
||||||
|
self.r = r
|
||||||
|
self.speed = speed
|
||||||
|
self.speedMemory = speed
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
self.slow = 20
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
self.colorBase = (125, 102, 8)
|
||||||
|
self.colorHead = (212, 172, 13)
|
||||||
|
self.colorGun = (40, 55, 71)
|
||||||
|
self.hitPoint = 45
|
||||||
|
self.health = self.hitPoint
|
||||||
|
self.damage = 8
|
||||||
|
elif self.level == 2:
|
||||||
|
self.colorBase = (19, 141, 117)
|
||||||
|
self.colorHead = (11, 83, 69)
|
||||||
|
self.colorGun = (52, 73, 94)
|
||||||
|
self.hitPoint = 60
|
||||||
|
self.health = self.hitPoint
|
||||||
|
self.damage = 14
|
||||||
|
elif self.level == 3:
|
||||||
|
self.colorBase = (46, 134, 193)
|
||||||
|
self.colorHead = (21, 67, 96)
|
||||||
|
self.colorGun = (33, 47, 60)
|
||||||
|
self.hitPoint = 80
|
||||||
|
self.health = self.hitPoint
|
||||||
|
self.damage = 23
|
||||||
|
elif self.level == 4:
|
||||||
|
self.colorBase = (26, 82, 118)
|
||||||
|
self.colorHead = (20, 143, 119)
|
||||||
|
self.colorGun = (23, 32, 42)
|
||||||
|
self.hitPoint = 100
|
||||||
|
self.health = self.hitPoint
|
||||||
|
self.damage = 25
|
||||||
|
self.slow = 30
|
||||||
|
elif self.level == 5:
|
||||||
|
self.colorBase = (69, 179, 157)
|
||||||
|
self.colorHead = (244, 208, 63)
|
||||||
|
self.colorGun = (23, 32, 42)
|
||||||
|
self.hitPoint = 120
|
||||||
|
self.health = self.hitPoint
|
||||||
|
self.damage = 35
|
||||||
|
self.slow = 30
|
||||||
|
|
||||||
|
self.angle = 0
|
||||||
|
self.delay = 0
|
||||||
|
|
||||||
|
self.gunCoord = []
|
||||||
|
self.gunCoord2 = []
|
||||||
|
self.shots = []
|
||||||
|
|
||||||
|
self.nearestPos = None
|
||||||
|
|
||||||
|
self.type = "GROUND"
|
||||||
|
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 15)
|
||||||
|
|
||||||
|
def updateHealth(self, defenceStruct):
|
||||||
|
error = self.r
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE") and (struct.attackType == "GROUND" or struct.attackType == "GROUND AND AIR"):
|
||||||
|
if (abs(struct.nearestPos[0] - self.x) < error) and (abs(struct.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in struct.shots:
|
||||||
|
if struct.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= struct.damage
|
||||||
|
|
||||||
|
def isHit(self, coord):
|
||||||
|
error = 30
|
||||||
|
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
|
||||||
|
if dist < error:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def removeHit(self):
|
||||||
|
tempList = self.shots[:]
|
||||||
|
for shot in self.shots:
|
||||||
|
if self.isHit((shot.x, shot.y)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def shoot(self):
|
||||||
|
self.delay = (self.delay + 1)%100
|
||||||
|
if self.delay%self.slow == 0:
|
||||||
|
pos = self.nearestPos
|
||||||
|
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle)
|
||||||
|
self.shots.append(shot)
|
||||||
|
if self.level >= 4:
|
||||||
|
shot = Shots((self.gunCoord2[1][0] + self.gunCoord2[2][0])/2, (self.gunCoord2[1][1] + self.gunCoord2[2][1])/2, self.radius, self.shotSpeed, self.angle)
|
||||||
|
self.shots.append(shot)
|
||||||
|
tempList = self.shots[:]
|
||||||
|
|
||||||
|
for shot in self.shots:
|
||||||
|
shot.move()
|
||||||
|
shot.draw()
|
||||||
|
if not ((margin < shot.x < width) and (margin < shot.y < height)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0), corr=270):
|
||||||
|
corr = corr
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord):
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
w = self.r*4/5
|
||||||
|
h = self.r*4
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
pos = self.nearestPos
|
||||||
|
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
rectCoord = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
baseCoord = []
|
||||||
|
|
||||||
|
for point in rectCoord:
|
||||||
|
baseCoord.append(self.translate(self.rotate(point, rotate, (w/2, h/2), 180)))
|
||||||
|
|
||||||
|
w = self.r/2
|
||||||
|
h = self.r*2
|
||||||
|
|
||||||
|
rectCoord = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
self.gunCoord = []
|
||||||
|
shift = w/2
|
||||||
|
if self.level >= 4:
|
||||||
|
shift = 2*w
|
||||||
|
self.gunCoord2 = []
|
||||||
|
for point in rectCoord:
|
||||||
|
self.gunCoord2.append(self.translate(self.rotate(point, rotate, (-2*w, 0), 270)))
|
||||||
|
for point in rectCoord:
|
||||||
|
self.gunCoord.append(self.translate(self.rotate(point, rotate, (shift, 0), 270)))
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*5, 40, 8))
|
||||||
|
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*5 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
|
||||||
|
|
||||||
|
|
||||||
|
if self.level >= 4:
|
||||||
|
pygame.draw.polygon(display, self.colorGun, self.gunCoord2)
|
||||||
|
pygame.draw.polygon(display, self.colorGun, self.gunCoord)
|
||||||
|
pygame.draw.polygon(display, self.colorBase, baseCoord)
|
||||||
|
pygame.draw.ellipse(display, self.colorHead, (self.x - self.r, self.y - self.r, self.r*2, self.r*2))
|
||||||
|
|
||||||
|
#pygame.draw.ellipse(display, (123, 125, 125), (self.x - self.range/2, self.y - self.range/2, self.range, self.range), 1)
|
||||||
|
|
||||||
|
def move(self, defenceStruct):
|
||||||
|
if len(defenceStruct):
|
||||||
|
nearestPos = 0
|
||||||
|
lowestDist = float("inf")
|
||||||
|
for struct in defenceStruct:
|
||||||
|
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
|
||||||
|
if lowestDist > dist:
|
||||||
|
self.nearestPos = [struct.x, struct.y]
|
||||||
|
lowestDist = dist
|
||||||
|
else:
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
angle = atan2(self.nearestPos[1] - self.y, self.nearestPos[0] - self.x)
|
||||||
|
self.x += self.speed*cos(angle)
|
||||||
|
self.y += self.speed*sin(angle)
|
||||||
|
dist = abs(self.nearestPos[1] - self.y) + abs(self.nearestPos[0] - self.x)
|
||||||
|
if dist < self.range/2:
|
||||||
|
self.speed = 0
|
||||||
|
else:
|
||||||
|
self.speed = self.speedMemory
|
||||||
|
|
||||||
|
class Tank:
|
||||||
|
def __init__(self, x, y, r, speed, range, level, shotSpeed, radius):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.barrelX = x
|
||||||
|
self.barrelY = y
|
||||||
|
|
||||||
|
self.range = range
|
||||||
|
self.shotSpeed = shotSpeed
|
||||||
|
self.radius = radius
|
||||||
|
|
||||||
|
self.r = r
|
||||||
|
self.speed = speed
|
||||||
|
self.speedMemory = speed
|
||||||
|
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
self.colorBase = (125, 102, 8)
|
||||||
|
self.colorWheel = (11, 83, 69)
|
||||||
|
self.colorGun = (14, 98, 81)
|
||||||
|
self.colorHead = (20, 90, 50)
|
||||||
|
self.hitPoint = 300
|
||||||
|
self.damage = 5
|
||||||
|
elif self.level == 2:
|
||||||
|
self.colorBase = (19, 141, 117)
|
||||||
|
self.colorWheel = (14, 98, 81)
|
||||||
|
self.colorGun = (11, 83, 69)
|
||||||
|
self.colorHead = (14, 98, 81)
|
||||||
|
self.hitPoint = 430
|
||||||
|
self.damage = 8
|
||||||
|
elif self.level == 3:
|
||||||
|
self.colorBase = (41, 128, 185)
|
||||||
|
self.colorWheel = (21, 67, 96)
|
||||||
|
self.colorGun = (36, 113, 163)
|
||||||
|
self.colorHead = (21, 67, 96)
|
||||||
|
self.hitPoint = 570
|
||||||
|
self.damage = 12
|
||||||
|
elif self.level == 4:
|
||||||
|
self.colorBase = (52, 73, 94)
|
||||||
|
self.colorWheel = (33, 47, 61)
|
||||||
|
self.colorGun = (23, 32, 42)
|
||||||
|
self.colorHead = (33, 47, 61)
|
||||||
|
self.hitPoint = 730
|
||||||
|
self.damage = 13
|
||||||
|
self.gunCoord2 = []
|
||||||
|
|
||||||
|
self.angle = 0
|
||||||
|
self.health = self.hitPoint
|
||||||
|
|
||||||
|
self.gunCoord = []
|
||||||
|
self.shots = []
|
||||||
|
|
||||||
|
self.type = "GROUND"
|
||||||
|
|
||||||
|
self.nearestPos = None
|
||||||
|
|
||||||
|
self.recoil = r/8
|
||||||
|
self.rebound = 0.2
|
||||||
|
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 15)
|
||||||
|
|
||||||
|
def updateHealth(self, defenceStruct):
|
||||||
|
error = self.r
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if (not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE")) and (struct.attackType == "GROUND" or struct.attackType == "GROUND AND AIR"):
|
||||||
|
if (abs(struct.nearestPos[0] - self.x) < error) and (abs(struct.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in struct.shots:
|
||||||
|
if struct.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= struct.damage
|
||||||
|
|
||||||
|
def isHit(self, coord):
|
||||||
|
error = self.r
|
||||||
|
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
|
||||||
|
if dist < error:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def removeHit(self):
|
||||||
|
tempList = self.shots[:]
|
||||||
|
for shot in self.shots:
|
||||||
|
if self.isHit((shot.x, shot.y)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def shoot(self):
|
||||||
|
error = 0.5
|
||||||
|
|
||||||
|
if not ((abs(self.barrelX - self.x) < error) and (abs(self.barrelY - self.y) < error)):
|
||||||
|
self.barrelX += self.rebound*cos(self.angle)
|
||||||
|
self.barrelY += self.rebound*sin(self.angle)
|
||||||
|
else:
|
||||||
|
pos = self.nearestPos
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
dist = ((self.barrelX - pos[0])**2 + (self.barrelY - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.barrelY, pos[0] - self.barrelX)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle)
|
||||||
|
self.shots.append(shot)
|
||||||
|
if self.level == 4:
|
||||||
|
shot = Shots((self.gunCoord2[1][0] + self.gunCoord2[2][0])/2, (self.gunCoord2[1][1] + self.gunCoord2[2][1])/2, self.radius, self.shotSpeed, self.angle)
|
||||||
|
self.shots.append(shot)
|
||||||
|
self.barrelX -= self.recoil*cos(self.angle)
|
||||||
|
self.barrelY -= self.recoil*sin(self.angle)
|
||||||
|
|
||||||
|
tempList = self.shots[:]
|
||||||
|
|
||||||
|
for shot in self.shots:
|
||||||
|
shot.move()
|
||||||
|
shot.draw()
|
||||||
|
if not ((margin < shot.x < width) and (margin < shot.y < height)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0), corr=270):
|
||||||
|
corr = corr
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord, point=-1):
|
||||||
|
if point == -1:
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
else:
|
||||||
|
return [coord[0] + point[0], coord[1] + point[1]]
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
pos = self.nearestPos
|
||||||
|
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
baseCoord = []
|
||||||
|
w = self.r
|
||||||
|
h = self.r*7/4
|
||||||
|
base = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
for point in base:
|
||||||
|
baseCoord.append(self.translate(self.rotate(point, rotate, (w/2, h/2))))
|
||||||
|
|
||||||
|
w = self.r/3
|
||||||
|
h = self.r*2
|
||||||
|
wheels = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
wheel1 = []
|
||||||
|
wheel2 = []
|
||||||
|
|
||||||
|
for point in wheels:
|
||||||
|
wheel1.append(self.translate(self.rotate(point, rotate, (w/2 - self.r/2 - w/2, h/2))))
|
||||||
|
wheel2.append(self.translate(self.rotate(point, rotate, (w/2 + self.r/2 + w/2, h/2))))
|
||||||
|
|
||||||
|
sides = 5
|
||||||
|
if self.level >= 3:
|
||||||
|
sides = 8
|
||||||
|
head = getCoordPoly(self.barrelX, self.barrelY , self.r/2, sides, rotate)
|
||||||
|
|
||||||
|
w = self.r/4
|
||||||
|
h = self.r*1.2
|
||||||
|
coord = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
self.gunCoord = []
|
||||||
|
self.gunCoord2 = []
|
||||||
|
|
||||||
|
if self.level == 4:
|
||||||
|
for point in coord:
|
||||||
|
self.gunCoord.append(self.translate(self.rotate(point, rotate, (w/2 + w, 0)), (self.barrelX, self.barrelY)))
|
||||||
|
self.gunCoord2.append(self.translate(self.rotate(point, rotate, (w/2 - w, 0)), (self.barrelX, self.barrelY)))
|
||||||
|
else:
|
||||||
|
for point in coord:
|
||||||
|
self.gunCoord.append(self.translate(self.rotate(point, rotate, (w/2, 0)), (self.barrelX, self.barrelY)))
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*2, 40, 8))
|
||||||
|
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
|
||||||
|
|
||||||
|
pygame.draw.polygon(display, self.colorBase, baseCoord)
|
||||||
|
pygame.draw.polygon(display, self.colorWheel, wheel1)
|
||||||
|
pygame.draw.polygon(display, self.colorWheel, wheel2)
|
||||||
|
pygame.draw.polygon(display, self.colorGun, self.gunCoord)
|
||||||
|
if self.level == 4:
|
||||||
|
pygame.draw.polygon(display, self.colorGun, self.gunCoord2)
|
||||||
|
pygame.draw.polygon(display, self.colorHead, head)
|
||||||
|
if self.level == 2:
|
||||||
|
pygame.draw.ellipse(display, self.colorBase, (self.barrelX - self.r/4, self.barrelY - self.r/4, self.r/2, self.r/2))
|
||||||
|
if self.level >= 3:
|
||||||
|
coord = getCoordPoly(self.barrelX, self.barrelY, self.r/3, 3, rotate)
|
||||||
|
pygame.draw.polygon(display, self.colorBase, coord)
|
||||||
|
|
||||||
|
#pygame.draw.ellipse(display, (123, 125, 125), (self.x - self.range/2, self.y - self.range/2, self.range, self.range), 1)
|
||||||
|
|
||||||
|
def move(self, defenceStruct):
|
||||||
|
if len(defenceStruct):
|
||||||
|
nearestPos = 0
|
||||||
|
lowestDist = float("inf")
|
||||||
|
for struct in defenceStruct:
|
||||||
|
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
|
||||||
|
if lowestDist > dist:
|
||||||
|
self.nearestPos = [struct.x, struct.y]
|
||||||
|
lowestDist = dist
|
||||||
|
else:
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
#self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
angle = atan2(self.nearestPos[1] - self.y, self.nearestPos[0] - self.x)
|
||||||
|
self.x += self.speed*cos(angle)
|
||||||
|
self.y += self.speed*sin(angle)
|
||||||
|
self.barrelX += self.speed*cos(angle)
|
||||||
|
self.barrelY += self.speed*sin(angle)
|
||||||
|
|
||||||
|
dist = abs(self.nearestPos[1] - self.y) + abs(self.nearestPos[0] - self.x)
|
||||||
|
if dist < self.range/2:
|
||||||
|
self.speed = 0
|
||||||
|
else:
|
||||||
|
self.speed = self.speedMemory
|
||||||
|
|
||||||
|
|
||||||
|
class Helicopter:
|
||||||
|
def __init__(self, x, y, r, speed, range, level, shotSpeed=5, radius=2):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.barrelX = x
|
||||||
|
self.barrelY = y
|
||||||
|
|
||||||
|
self.range = range
|
||||||
|
self.shotSpeed = shotSpeed
|
||||||
|
self.radius = radius
|
||||||
|
|
||||||
|
self.r = r
|
||||||
|
self.speed = speed
|
||||||
|
self.speedMemory = speed
|
||||||
|
|
||||||
|
self.level = level
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
self.colorFront = (26, 82, 118)
|
||||||
|
self.colorBody = (26, 82, 118)
|
||||||
|
self.colorTail = (26, 82, 118)
|
||||||
|
self.colorTailHead = (26, 82, 118)
|
||||||
|
self.colorFan = (23, 32, 42)
|
||||||
|
self.colorGun = (23, 32, 42)
|
||||||
|
self.hitPoint = 700
|
||||||
|
self.damage = 30
|
||||||
|
elif self.level == 2:
|
||||||
|
self.colorFront = (11, 83, 69)
|
||||||
|
self.colorBody = (22, 160, 133)
|
||||||
|
self.colorTail = (11, 83, 69)
|
||||||
|
self.colorTailHead = (11, 83, 69)
|
||||||
|
self.colorFan = (23, 32, 42)
|
||||||
|
self.colorGun = (23, 32, 42)
|
||||||
|
self.hitPoint = 900
|
||||||
|
self.damage = 50
|
||||||
|
elif self.level == 3:
|
||||||
|
self.colorFront = (28, 40, 51)
|
||||||
|
self.colorBody = (21, 67, 96)
|
||||||
|
self.colorTail = (28, 40, 51)
|
||||||
|
self.colorTailHead = (28, 40, 51)
|
||||||
|
self.colorFan = (23, 32, 42)
|
||||||
|
self.colorGun = (23, 32, 42)
|
||||||
|
self.hitPoint = 1300
|
||||||
|
self.damage = 52
|
||||||
|
|
||||||
|
self.angle = 0
|
||||||
|
self.rotAngle = 0
|
||||||
|
self.fanAngle = 0
|
||||||
|
|
||||||
|
self.type = "AIR"
|
||||||
|
|
||||||
|
self.gunCoord = []
|
||||||
|
self.gunCoord2 = []
|
||||||
|
self.shots = []
|
||||||
|
|
||||||
|
self.nearestPos = None
|
||||||
|
self.health = self.hitPoint
|
||||||
|
|
||||||
|
self.delay = 0
|
||||||
|
self.font = pygame.font.SysFont("Agency FB", 15)
|
||||||
|
|
||||||
|
def updateHealth(self, defenceStruct):
|
||||||
|
error = self.r
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if not (struct.type == "HEADQUARTERS" or struct.type == "RESOURCE") and (struct.attackType == "AIR" or struct.attackType == "GROUND AND AIR"):
|
||||||
|
if (abs(struct.nearestPos[0] - self.x) < error) and (abs(struct.nearestPos[1] - self.y) < error):
|
||||||
|
for shot in struct.shots:
|
||||||
|
if struct.isHit((shot.x, shot.y)):
|
||||||
|
self.health -= struct.damage
|
||||||
|
|
||||||
|
def isHit(self, coord):
|
||||||
|
error = self.r
|
||||||
|
dist = ((self.nearestPos[0] - coord[0])**2 + (self.nearestPos[1] - coord[1])**2)**0.5
|
||||||
|
if dist < error:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def removeHit(self):
|
||||||
|
tempList = self.shots[:]
|
||||||
|
for shot in self.shots:
|
||||||
|
if self.isHit((shot.x, shot.y)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
def shoot(self):
|
||||||
|
self.delay = (self.delay + 1)%100
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
if self.speed == 0 and self.rotAngle == -90 and self.delay%7 == 0:
|
||||||
|
pos = self.nearestPos
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
|
||||||
|
self.shots.append(shot)
|
||||||
|
if self.level == 2:
|
||||||
|
if self.delay%10 == 0:
|
||||||
|
pos = self.nearestPos
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
|
||||||
|
self.shots.append(shot)
|
||||||
|
|
||||||
|
elif self.level == 3:
|
||||||
|
if self.delay%10 == 0:
|
||||||
|
pos = self.nearestPos
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
dist = ((self.x - pos[0])**2 + (self.y - pos[1])**2)**0.5
|
||||||
|
self.angle = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
if dist < self.range/2:
|
||||||
|
shot = Shots((self.gunCoord[1][0] + self.gunCoord[2][0])/2, (self.gunCoord[1][1] + self.gunCoord[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
|
||||||
|
self.shots.append(shot)
|
||||||
|
shot = Shots((self.gunCoord2[1][0] + self.gunCoord2[2][0])/2, (self.gunCoord2[1][1] + self.gunCoord2[2][1])/2, self.radius, self.shotSpeed, self.angle, (0, 0, 0), "line")
|
||||||
|
self.shots.append(shot)
|
||||||
|
|
||||||
|
tempList = self.shots[:]
|
||||||
|
|
||||||
|
for shot in self.shots:
|
||||||
|
shot.move()
|
||||||
|
shot.draw()
|
||||||
|
if not ((margin < shot.x < width) and (margin < shot.y < height)):
|
||||||
|
tempList.remove(shot)
|
||||||
|
|
||||||
|
self.shots = tempList[:]
|
||||||
|
|
||||||
|
|
||||||
|
def rotate(self, coord, angle, anchor=(0, 0), corr=270):
|
||||||
|
corr = corr
|
||||||
|
return ((coord[0] - anchor[0])*cos(angle + radians(corr)) - (coord[1] - anchor[1])*sin(angle + radians(corr)),
|
||||||
|
(coord[0] - anchor[0])*sin(angle + radians(corr)) + (coord[1] - anchor[1])*cos(angle + radians(corr)))
|
||||||
|
|
||||||
|
def translate(self, coord, point=-1):
|
||||||
|
if point == -1:
|
||||||
|
return [coord[0] + self.x, coord[1] + self.y]
|
||||||
|
else:
|
||||||
|
return [coord[0] + point[0], coord[1] + point[1]]
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
#pos = pygame.mouse.get_pos()
|
||||||
|
pos = self.nearestPos
|
||||||
|
rotate = atan2(pos[1] - self.y, pos[0] - self.x)
|
||||||
|
|
||||||
|
self.fanAngle = (self.fanAngle + 46)%360
|
||||||
|
|
||||||
|
len = (self.r*2)/(3**0.5)
|
||||||
|
|
||||||
|
centre = (self.x, self.y)
|
||||||
|
top = (self.x + len*cos(radians(self.fanAngle)), self.y + len*sin(radians(self.fanAngle)))
|
||||||
|
left = (self.x + len*cos(radians(self.fanAngle - 120)), self.y + len*sin(radians(self.fanAngle - 120)))
|
||||||
|
right = (self.x + len*cos(radians(self.fanAngle + 120)), self.y + len*sin(radians(self.fanAngle + 120)))
|
||||||
|
|
||||||
|
w = self.r/2
|
||||||
|
h = self.r/2
|
||||||
|
|
||||||
|
if self.level >= 2:
|
||||||
|
w = self.r/4
|
||||||
|
h = self.r/2
|
||||||
|
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
base = []
|
||||||
|
|
||||||
|
if self.speed == 0 and self.rotAngle > -90 and self.level == 1:
|
||||||
|
self.rotAngle -= 3
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
base.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2, h/2))))
|
||||||
|
|
||||||
|
front = getCoordPoly((base[1][0] + base[2][0])/2, (base[1][1] + base[2][1])/2, w/2, 8, rotate + radians(self.rotAngle))
|
||||||
|
if self.level >= 2:
|
||||||
|
back = getCoordPoly((base[0][0] + base[3][0])/2, (base[0][1] + base[3][1])/2, w, 8, rotate + radians(self.rotAngle))
|
||||||
|
else:
|
||||||
|
back = getCoordPoly((base[0][0] + base[3][0])/2, (base[0][1] + base[3][1])/2, w/2, 8, rotate + radians(self.rotAngle))
|
||||||
|
tail = []
|
||||||
|
|
||||||
|
if self.level >= 2:
|
||||||
|
w = self.r/3
|
||||||
|
h = self.r/2
|
||||||
|
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
base2 = []
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
base2.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2, h*2/3))))
|
||||||
|
|
||||||
|
flap1 = []
|
||||||
|
flap2 = []
|
||||||
|
w = self.r/3
|
||||||
|
h = self.r/4
|
||||||
|
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
flap1.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2 + w, h))))
|
||||||
|
flap2.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2 - w, h))))
|
||||||
|
|
||||||
|
w = self.r/8
|
||||||
|
h = self.r*1.2
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
tail.append(self.translate(self.rotate(point, rotate + radians(self.rotAngle), (w/2, h))))
|
||||||
|
|
||||||
|
if self.level == 1:
|
||||||
|
self.gunCoord = []
|
||||||
|
w = self.r/15
|
||||||
|
h = self.r/2.5
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.gunCoord.append(self.translate(self.rotate(point, rotate + radians(90) + radians(self.rotAngle), (w/2, 0))))
|
||||||
|
elif self.level == 2:
|
||||||
|
self.gunCoord = []
|
||||||
|
w = self.r/15
|
||||||
|
h = self.r/2.5
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.gunCoord.append(self.translate(self.rotate(point, rotate + radians(180) + radians(self.rotAngle), (w/2, h*3/2))))
|
||||||
|
elif self.level == 3:
|
||||||
|
self.gunCoord = []
|
||||||
|
self.gunCoord2 = []
|
||||||
|
w = self.r/15
|
||||||
|
h = self.r/2.5
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
self.gunCoord.append(self.translate(self.rotate(point, rotate + radians(180) + radians(self.rotAngle), (w/2 + 5*w, h/2))))
|
||||||
|
self.gunCoord2.append(self.translate(self.rotate(point, rotate + radians(180) + radians(self.rotAngle), (w/2 - 5*w, h/2))))
|
||||||
|
|
||||||
|
tailHead = []
|
||||||
|
w = self.r/10
|
||||||
|
h = self.r/3
|
||||||
|
points = [(0, 0), (0, h), (w, h), (w, 0)]
|
||||||
|
|
||||||
|
for point in points:
|
||||||
|
tailHead.append(self.translate(self.rotate(point, rotate + radians(90) + radians(self.rotAngle), (self.r + w, h/2))))
|
||||||
|
|
||||||
|
|
||||||
|
pygame.draw.rect(display, (231, 76, 60), (self.x, self.y - self.r*2, 40, 8))
|
||||||
|
pygame.draw.rect(display, (0, 255, 0), (self.x + 1, self.y - self.r*2 + 1, int(.4*(float(self.health)/self.hitPoint)*100) - 2, 8 - 2))
|
||||||
|
|
||||||
|
pygame.draw.polygon(display, self.colorFront, front)
|
||||||
|
pygame.draw.polygon(display, self.colorGun, self.gunCoord)
|
||||||
|
pygame.draw.polygon(display, self.colorBody, back)
|
||||||
|
pygame.draw.polygon(display, self.colorBody, base)
|
||||||
|
if self.level >= 2:
|
||||||
|
pygame.draw.polygon(display, self.colorBody, base2)
|
||||||
|
pygame.draw.polygon(display, self.colorFront, flap1)
|
||||||
|
pygame.draw.polygon(display, self.colorFront, flap2)
|
||||||
|
if self.level == 3:
|
||||||
|
pygame.draw.polygon(display, self.colorGun, self.gunCoord2)
|
||||||
|
pygame.draw.polygon(display, self.colorTail, tail)
|
||||||
|
pygame.draw.polygon(display, self.colorTailHead, tailHead)
|
||||||
|
|
||||||
|
pygame.draw.line(display, self.colorFan, top, centre, 5)
|
||||||
|
pygame.draw.line(display, self.colorFan, left, centre, 5)
|
||||||
|
pygame.draw.line(display, self.colorFan, right, centre, 5)
|
||||||
|
|
||||||
|
def move(self, defenceStruct):
|
||||||
|
if len(defenceStruct):
|
||||||
|
nearestPos = 0
|
||||||
|
lowestDist = float("inf")
|
||||||
|
for struct in defenceStruct:
|
||||||
|
if struct.type == "HEADQUARTERS" or struct.type == "RESOURCE":
|
||||||
|
dist = (self.x - struct.x)**2 + (self.y - struct.y)**2
|
||||||
|
else:
|
||||||
|
dist = (self.x - struct.xOld)**2 + (self.y - struct.yOld)**2
|
||||||
|
if lowestDist > dist:
|
||||||
|
if struct.type == "HEADQUARTERS" or struct.type == "RESOURCE":
|
||||||
|
self.nearestPos = [struct.x, struct.y]
|
||||||
|
else:
|
||||||
|
self.nearestPos = [struct.xOld, struct.yOld]
|
||||||
|
lowestDist = dist
|
||||||
|
else:
|
||||||
|
self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
#self.nearestPos = pygame.mouse.get_pos()
|
||||||
|
angle = atan2(self.nearestPos[1] - self.y, self.nearestPos[0] - self.x)
|
||||||
|
self.x += self.speed*cos(angle)
|
||||||
|
self.y += self.speed*sin(angle)
|
||||||
|
|
||||||
|
dist = abs(self.nearestPos[1] - self.y) + abs(self.nearestPos[0] - self.x)
|
||||||
|
if dist < self.range/2:
|
||||||
|
self.speed = 0
|
||||||
|
else:
|
||||||
|
self.speed = self.speedMemory
|
||||||
|
self.rotAngle = 0
|
||||||
|
|
BIN
Battles/Code/__pycache__/GUI.cpython-310.pyc
Normal file
BIN
Battles/Code/__pycache__/Maps.cpython-310.pyc
Normal file
BIN
Battles/Code/__pycache__/Structures.cpython-310.pyc
Normal file
BIN
Battles/Code/__pycache__/Troops.cpython-310.pyc
Normal file
256
Chain_Reaction/Chain-Reaction.py
Normal file
@ -0,0 +1,256 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Chain Reaction
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, math
|
||||||
|
#
|
||||||
|
# Controls - Mouse Click
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
from math import *
|
||||||
|
|
||||||
|
# Initialization of Pygame
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 400
|
||||||
|
height = 400
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
background = (21, 67, 96)
|
||||||
|
border = (208, 211, 212)
|
||||||
|
red = (231, 76, 60)
|
||||||
|
white = (244, 246, 247)
|
||||||
|
violet = (136, 78, 160)
|
||||||
|
yellow = (244, 208, 63)
|
||||||
|
green = (88, 214, 141)
|
||||||
|
|
||||||
|
playerColor = [red, green, violet, yellow]
|
||||||
|
|
||||||
|
font = pygame.font.SysFont("Times New Roman", 30)
|
||||||
|
|
||||||
|
blocks = 40
|
||||||
|
noPlayers = 4
|
||||||
|
|
||||||
|
pygame.display.set_caption("Chain Reaction %d Player" % noPlayers)
|
||||||
|
|
||||||
|
score = []
|
||||||
|
for i in range(noPlayers):
|
||||||
|
score.append(0)
|
||||||
|
|
||||||
|
players = []
|
||||||
|
for i in range(noPlayers):
|
||||||
|
players.append(playerColor[i])
|
||||||
|
|
||||||
|
d = blocks//2 - 2
|
||||||
|
|
||||||
|
cols = int(width//blocks)
|
||||||
|
rows = int(height//blocks)
|
||||||
|
|
||||||
|
grid = []
|
||||||
|
|
||||||
|
# Quit or Close the Game Window
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# Class for Each Spot in Grid
|
||||||
|
class Spot():
|
||||||
|
def __init__(self):
|
||||||
|
self.color = border
|
||||||
|
self.neighbors = []
|
||||||
|
self.noAtoms = 0
|
||||||
|
|
||||||
|
def addNeighbors(self, i, j):
|
||||||
|
if i > 0:
|
||||||
|
self.neighbors.append(grid[i - 1][j])
|
||||||
|
if i < rows - 1:
|
||||||
|
self.neighbors.append(grid[i + 1][j])
|
||||||
|
if j < cols - 1:
|
||||||
|
self.neighbors.append(grid[i][j + 1])
|
||||||
|
if j > 0:
|
||||||
|
self.neighbors.append(grid[i][j - 1])
|
||||||
|
|
||||||
|
# Initializing the Grid with "Empty or 0"
|
||||||
|
def initializeGrid():
|
||||||
|
global grid, score, players
|
||||||
|
score = []
|
||||||
|
for i in range(noPlayers):
|
||||||
|
score.append(0)
|
||||||
|
|
||||||
|
players = []
|
||||||
|
for i in range(noPlayers):
|
||||||
|
players.append(playerColor[i])
|
||||||
|
|
||||||
|
grid = [[]for _ in range(cols)]
|
||||||
|
for i in range(cols):
|
||||||
|
for j in range(rows):
|
||||||
|
newObj = Spot()
|
||||||
|
grid[i].append(newObj)
|
||||||
|
for i in range(cols):
|
||||||
|
for j in range(rows):
|
||||||
|
grid[i][j].addNeighbors(i, j)
|
||||||
|
|
||||||
|
# Draw the Grid in Pygame Window
|
||||||
|
def drawGrid(currentIndex):
|
||||||
|
r = 0
|
||||||
|
c = 0
|
||||||
|
for i in range(width//blocks):
|
||||||
|
r += blocks
|
||||||
|
c += blocks
|
||||||
|
pygame.draw.line(display, players[currentIndex], (c, 0), (c, height))
|
||||||
|
pygame.draw.line(display, players[currentIndex], (0, r), (width, r))
|
||||||
|
|
||||||
|
# Draw the Present Situation of Grid
|
||||||
|
def showPresentGrid(vibrate = 1):
|
||||||
|
r = -blocks
|
||||||
|
c = -blocks
|
||||||
|
padding = 2
|
||||||
|
for i in range(cols):
|
||||||
|
r += blocks
|
||||||
|
c = -blocks
|
||||||
|
for j in range(rows):
|
||||||
|
c += blocks
|
||||||
|
if grid[i][j].noAtoms == 0:
|
||||||
|
grid[i][j].color = border
|
||||||
|
elif grid[i][j].noAtoms == 1:
|
||||||
|
pygame.draw.ellipse(display, grid[i][j].color, (r + blocks/2 - d/2 + vibrate, c + blocks/2 - d/2, d, d))
|
||||||
|
elif grid[i][j].noAtoms == 2:
|
||||||
|
pygame.draw.ellipse(display, grid[i][j].color, (r + 5, c + blocks/2 - d/2 - vibrate, d, d))
|
||||||
|
pygame.draw.ellipse(display, grid[i][j].color, (r + d/2 + blocks/2 - d/2 + vibrate, c + blocks/2 - d/2, d, d))
|
||||||
|
elif grid[i][j].noAtoms == 3:
|
||||||
|
angle = 90
|
||||||
|
x = r + (d/2)*cos(radians(angle)) + blocks/2 - d/2
|
||||||
|
y = c + (d/2)*sin(radians(angle)) + blocks/2 - d/2
|
||||||
|
pygame.draw.ellipse(display, grid[i][j].color, (x - vibrate, y, d, d))
|
||||||
|
x = r + (d/2)*cos(radians(angle + 90)) + blocks/2 - d/2
|
||||||
|
y = c + (d/2)*sin(radians(angle + 90)) + 5
|
||||||
|
pygame.draw.ellipse(display, grid[i][j].color, (x + vibrate, y, d, d))
|
||||||
|
x = r + (d/2)*cos(radians(angle - 90)) + blocks/2 - d/2
|
||||||
|
y = c + (d/2)*sin(radians(angle - 90)) + 5
|
||||||
|
pygame.draw.ellipse(display, grid[i][j].color, (x - vibrate, y, d, d))
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
# Increase the Atom when Clicked
|
||||||
|
def addAtom(i, j, color):
|
||||||
|
grid[i][j].noAtoms += 1
|
||||||
|
grid[i][j].color = color
|
||||||
|
if grid[i][j].noAtoms >= len(grid[i][j].neighbors):
|
||||||
|
overFlow(grid[i][j], color)
|
||||||
|
|
||||||
|
# Split the Atom when it Increases the "LIMIT"
|
||||||
|
def overFlow(cell, color):
|
||||||
|
showPresentGrid()
|
||||||
|
cell.noAtoms = 0
|
||||||
|
for m in range(len(cell.neighbors)):
|
||||||
|
cell.neighbors[m].noAtoms += 1
|
||||||
|
cell.neighbors[m].color = color
|
||||||
|
if cell.neighbors[m].noAtoms >= len(cell.neighbors[m].neighbors):
|
||||||
|
overFlow(cell.neighbors[m], color)
|
||||||
|
|
||||||
|
# Checking if Any Player has WON!
|
||||||
|
def isPlayerInGame():
|
||||||
|
global score
|
||||||
|
playerScore = []
|
||||||
|
for i in range(noPlayers):
|
||||||
|
playerScore.append(0)
|
||||||
|
for i in range(cols):
|
||||||
|
for j in range(rows):
|
||||||
|
for k in range(noPlayers):
|
||||||
|
if grid[i][j].color == players[k]:
|
||||||
|
playerScore[k] += grid[i][j].noAtoms
|
||||||
|
score = playerScore[:]
|
||||||
|
|
||||||
|
# GAME OVER
|
||||||
|
def gameOver(playerIndex):
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
gameLoop()
|
||||||
|
|
||||||
|
text = font.render("Player %d Won!" % (playerIndex + 1), True, white)
|
||||||
|
text2 = font.render("Press \'r\' to Reset!", True, white)
|
||||||
|
|
||||||
|
display.blit(text, (width/3, height/3))
|
||||||
|
display.blit(text2, (width/3, height/2 ))
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
|
||||||
|
def checkWon():
|
||||||
|
num = 0
|
||||||
|
for i in range(noPlayers):
|
||||||
|
if score[i] == 0:
|
||||||
|
num += 1
|
||||||
|
if num == noPlayers - 1:
|
||||||
|
for i in range(noPlayers):
|
||||||
|
if score[i]:
|
||||||
|
return i
|
||||||
|
|
||||||
|
return 9999
|
||||||
|
|
||||||
|
# Main Loop
|
||||||
|
def gameLoop():
|
||||||
|
initializeGrid()
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
turns = 0
|
||||||
|
|
||||||
|
currentPlayer = 0
|
||||||
|
|
||||||
|
vibrate = .5
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
x, y = pygame.mouse.get_pos()
|
||||||
|
i = x/blocks
|
||||||
|
j = y/blocks
|
||||||
|
if grid[i][j].color == players[currentPlayer] or grid[i][j].color == border:
|
||||||
|
turns += 1
|
||||||
|
addAtom(i, j, players[currentPlayer])
|
||||||
|
currentPlayer += 1
|
||||||
|
if currentPlayer >= noPlayers:
|
||||||
|
currentPlayer = 0
|
||||||
|
if turns >= noPlayers:
|
||||||
|
isPlayerInGame()
|
||||||
|
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
# Vibrate the Atoms in their Cells
|
||||||
|
vibrate *= -1
|
||||||
|
|
||||||
|
drawGrid(currentPlayer)
|
||||||
|
showPresentGrid(vibrate)
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
res = checkWon()
|
||||||
|
if res < 9999:
|
||||||
|
gameOver(res)
|
||||||
|
|
||||||
|
clock.tick(20)
|
||||||
|
|
||||||
|
gameLoop()
|
225
Dodge_The_Ball/DodgeTheBall.py
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Dodge The Ball!
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, random, math
|
||||||
|
#
|
||||||
|
# Controls - Mouse Movement
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
from math import *
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 400
|
||||||
|
height = 500
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption("Dodge The Ball!")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
background = (51, 51, 51)
|
||||||
|
playerColor = (249, 231, 159)
|
||||||
|
|
||||||
|
red = (203, 67, 53)
|
||||||
|
yellow = (241, 196, 15)
|
||||||
|
blue = (46, 134, 193)
|
||||||
|
green = (34, 153, 84)
|
||||||
|
purple = (136, 78, 160)
|
||||||
|
orange = (214, 137, 16)
|
||||||
|
|
||||||
|
colors = [red, yellow, blue, green, purple, orange]
|
||||||
|
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
|
||||||
|
class Ball:
|
||||||
|
def __init__(self, radius, speed):
|
||||||
|
self.x = 0
|
||||||
|
self.y = 0
|
||||||
|
self.r = radius
|
||||||
|
self.color = 0
|
||||||
|
self.speed = speed
|
||||||
|
self.angle = 0
|
||||||
|
|
||||||
|
def createBall(self):
|
||||||
|
self.x = width/2 - self.r
|
||||||
|
self.y = height/2 - self.r
|
||||||
|
self.color = random.choice(colors)
|
||||||
|
self.angle = random.randint(-180, 180)
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.x += self.speed*cos(radians(self.angle))
|
||||||
|
self.y += self.speed*sin(radians(self.angle))
|
||||||
|
|
||||||
|
if self.x < self.r or self.x + self.r > width:
|
||||||
|
self.angle = 180 - self.angle
|
||||||
|
if self.y < self.r or self.y + self.r > height:
|
||||||
|
self.angle *= -1
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.ellipse(display, self.color, (self.x - self.r, self.y - self.r, self.r*2, self.r*2))
|
||||||
|
|
||||||
|
def collision(self, radius):
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
|
dist = ((pos[0] - self.x)**2 + (pos[1] - self.y)**2)**0.5
|
||||||
|
|
||||||
|
if dist <= self.r + radius:
|
||||||
|
gameOver()
|
||||||
|
|
||||||
|
class Target:
|
||||||
|
def __init__(self):
|
||||||
|
self.x = 0
|
||||||
|
self.y = 0
|
||||||
|
self.w = 20
|
||||||
|
self.h = self.w
|
||||||
|
|
||||||
|
def generateNewCoord(self):
|
||||||
|
self.x = random.randint(self.w, width - self.w)
|
||||||
|
self.y = random.randint(self.h, height - self.h)
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
color = random.choice(colors)
|
||||||
|
|
||||||
|
pygame.draw.rect(display, color, (self.x, self.y, self.w, self.h))
|
||||||
|
|
||||||
|
|
||||||
|
def gameOver():
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
font = pygame.font.SysFont("Agency FB", 100)
|
||||||
|
text = font.render("Game Over!", True, (230, 230, 230))
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
gameLoop()
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
|
||||||
|
display.blit(text, (20, height/2 - 100))
|
||||||
|
displayScore()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick()
|
||||||
|
|
||||||
|
|
||||||
|
def checkCollision(target, d, objTarget):
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
dist = ((pos[0] - target[0] - objTarget.w)**2 + (pos[1] - target[1] - objTarget.h)**2)**0.5
|
||||||
|
|
||||||
|
if dist <= d + objTarget.w:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def drawPlayerPointer(pos, r):
|
||||||
|
pygame.draw.ellipse(display, playerColor, (pos[0] - r, pos[1] - r, 2*r, 2*r))
|
||||||
|
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
def displayScore():
|
||||||
|
font = pygame.font.SysFont("Forte", 30)
|
||||||
|
scoreText = font.render("Score: " + str(score), True, (230, 230, 230))
|
||||||
|
display.blit(scoreText, (10, 10))
|
||||||
|
|
||||||
|
|
||||||
|
def gameLoop():
|
||||||
|
global score
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
pRadius = 10
|
||||||
|
|
||||||
|
balls = []
|
||||||
|
|
||||||
|
for i in range(1):
|
||||||
|
newBall = Ball(pRadius + 2, 5)
|
||||||
|
newBall.createBall()
|
||||||
|
balls.append(newBall)
|
||||||
|
|
||||||
|
target = Target()
|
||||||
|
target.generateNewCoord()
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
gameLoop()
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
|
||||||
|
for i in range(len(balls)):
|
||||||
|
balls[i].move()
|
||||||
|
|
||||||
|
for i in range(len(balls)):
|
||||||
|
balls[i].draw()
|
||||||
|
|
||||||
|
for i in range(len(balls)):
|
||||||
|
balls[i].collision(pRadius)
|
||||||
|
|
||||||
|
playerPos = pygame.mouse.get_pos()
|
||||||
|
drawPlayerPointer((playerPos[0], playerPos[1]), pRadius)
|
||||||
|
|
||||||
|
collide = checkCollision((target.x, target.y), pRadius, target)
|
||||||
|
|
||||||
|
if collide:
|
||||||
|
score += 1
|
||||||
|
target.generateNewCoord()
|
||||||
|
elif score == 2 and len(balls) == 1:
|
||||||
|
newBall = Ball(pRadius + 2, 5)
|
||||||
|
newBall.createBall()
|
||||||
|
balls.append(newBall)
|
||||||
|
target.generateNewCoord()
|
||||||
|
elif score == 5 and len(balls) == 2:
|
||||||
|
newBall = Ball(pRadius + 2, 6)
|
||||||
|
newBall.createBall()
|
||||||
|
balls.append(newBall)
|
||||||
|
target.generateNewCoord()
|
||||||
|
elif score == 10 and len(balls) == 3:
|
||||||
|
newBall = Ball(pRadius + 2, 7)
|
||||||
|
newBall.createBall()
|
||||||
|
balls.append(newBall)
|
||||||
|
target.generateNewCoord()
|
||||||
|
elif score == 15 and len(balls) == 4:
|
||||||
|
newBall = Ball(pRadius + 2, 8)
|
||||||
|
newBall.createBall()
|
||||||
|
balls.append(newBall)
|
||||||
|
target.generateNewCoord()
|
||||||
|
elif score == 20 and len(balls) == 5:
|
||||||
|
newBall = Ball(pRadius + 2, 9)
|
||||||
|
newBall.createBall()
|
||||||
|
balls.append(newBall)
|
||||||
|
target.generateNewCoord()
|
||||||
|
|
||||||
|
target.draw()
|
||||||
|
displayScore()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
gameLoop()
|
158
Hangman/hangman.py
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
def game():
|
||||||
|
print_opening_message()
|
||||||
|
secret_word = upload_secret_word()
|
||||||
|
|
||||||
|
correct_letters = initializes_correct_letters(secret_word)
|
||||||
|
print(correct_letters)
|
||||||
|
|
||||||
|
hanged = False
|
||||||
|
right = False
|
||||||
|
failure = 0
|
||||||
|
|
||||||
|
while(not hanged and not right):
|
||||||
|
|
||||||
|
guess = ask_guess()
|
||||||
|
|
||||||
|
if(guess in secret_word):
|
||||||
|
correct_guess_mark(guess, correct_letters, secret_word)
|
||||||
|
else:
|
||||||
|
failure += 1
|
||||||
|
draw_hang(failure)
|
||||||
|
|
||||||
|
hanged = failure == 7
|
||||||
|
right = "_" not in correct_letters
|
||||||
|
|
||||||
|
print(correct_letters)
|
||||||
|
|
||||||
|
if(right):
|
||||||
|
prints_winner_message()
|
||||||
|
else:
|
||||||
|
prints_loser_message(secret_word)
|
||||||
|
|
||||||
|
|
||||||
|
def draw_hang(failure):
|
||||||
|
print(" _______ ")
|
||||||
|
print(" |/ | ")
|
||||||
|
|
||||||
|
if(failure == 1):
|
||||||
|
print (" | (_) ")
|
||||||
|
print (" | ")
|
||||||
|
print (" | ")
|
||||||
|
print (" | ")
|
||||||
|
|
||||||
|
if(failure == 2):
|
||||||
|
print (" | (_) ")
|
||||||
|
print (" | \ ")
|
||||||
|
print (" | ")
|
||||||
|
print (" | ")
|
||||||
|
|
||||||
|
if(failure == 3):
|
||||||
|
print (" | (_) ")
|
||||||
|
print (" | \| ")
|
||||||
|
print (" | ")
|
||||||
|
print (" | ")
|
||||||
|
|
||||||
|
if(failure == 4):
|
||||||
|
print (" | (_) ")
|
||||||
|
print (" | \|/ ")
|
||||||
|
print (" | ")
|
||||||
|
print (" | ")
|
||||||
|
|
||||||
|
if(failure == 5):
|
||||||
|
print (" | (_) ")
|
||||||
|
print (" | \|/ ")
|
||||||
|
print (" | | ")
|
||||||
|
print (" | ")
|
||||||
|
|
||||||
|
if(failure == 6):
|
||||||
|
print (" | (_) ")
|
||||||
|
print (" | \|/ ")
|
||||||
|
print (" | | ")
|
||||||
|
print (" | / ")
|
||||||
|
|
||||||
|
if (failure == 7):
|
||||||
|
print (" | (_) ")
|
||||||
|
print (" | \|/ ")
|
||||||
|
print (" | | ")
|
||||||
|
print (" | / \ ")
|
||||||
|
|
||||||
|
print(" | ")
|
||||||
|
print("_|___ ")
|
||||||
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def prints_winner_message():
|
||||||
|
print("Congratulations, you win!")
|
||||||
|
print(" ___________ ")
|
||||||
|
print(" '._==_==_=_.' ")
|
||||||
|
print(" .-\\: /-. ")
|
||||||
|
print(" | (|:. |) | ")
|
||||||
|
print(" '-|:. |-' ")
|
||||||
|
print(" \\::. / ")
|
||||||
|
print(" '::. .' ")
|
||||||
|
print(" ) ( ")
|
||||||
|
print(" _.' '._ ")
|
||||||
|
print(" '-------' ")
|
||||||
|
|
||||||
|
|
||||||
|
def prints_loser_message(secret_word):
|
||||||
|
print("Failed, you were hanged!")
|
||||||
|
print("The word was {}".format(secret_word))
|
||||||
|
print(" _______________ ")
|
||||||
|
print(" / \ ")
|
||||||
|
print(" / \ ")
|
||||||
|
print("// \/\ ")
|
||||||
|
print("\| XXXX XXXX | / ")
|
||||||
|
print(" | XXXX XXXX |/ ")
|
||||||
|
print(" | XXX XXX | ")
|
||||||
|
print(" | | ")
|
||||||
|
print(" \__ XXX __/ ")
|
||||||
|
print(" |\ XXX /| ")
|
||||||
|
print(" | | | | ")
|
||||||
|
print(" | I I I I I I I | ")
|
||||||
|
print(" | I I I I I I | ")
|
||||||
|
print(" \_ _/ ")
|
||||||
|
print(" \_ _/ ")
|
||||||
|
print(" \_______/ ")
|
||||||
|
|
||||||
|
def correct_guess_mark(guess, correct_letters, secret_word):
|
||||||
|
index = 0
|
||||||
|
for word in secret_word:
|
||||||
|
if (guess == word):
|
||||||
|
correct_letters[index] = word
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
def ask_guess():
|
||||||
|
guess = input("Which letter? ")
|
||||||
|
guess = guess.strip().upper()
|
||||||
|
return guess
|
||||||
|
|
||||||
|
def initializes_correct_letters(word):
|
||||||
|
return ["_" for word in word]
|
||||||
|
|
||||||
|
def print_opening_message():
|
||||||
|
print("*********************************")
|
||||||
|
print("**Welcome to the Hangman game!***")
|
||||||
|
print("*********************************")
|
||||||
|
|
||||||
|
def upload_secret_word():
|
||||||
|
archive = open("list.txt", "r")
|
||||||
|
word = []
|
||||||
|
|
||||||
|
for line in archive:
|
||||||
|
line = line.strip()
|
||||||
|
word.append(line)
|
||||||
|
|
||||||
|
archive.close()
|
||||||
|
|
||||||
|
number = random.randrange(0, len(word))
|
||||||
|
secret_word = word[number].upper()
|
||||||
|
return secret_word
|
||||||
|
|
||||||
|
|
||||||
|
if(__name__ == "__main__"):
|
||||||
|
game()
|
253
Minesweeper/Minesweeper.py
Normal file
@ -0,0 +1,253 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Minesweeper
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, random
|
||||||
|
#
|
||||||
|
# Controls - Single Mouse Click on any box, r to reset the grid
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 510
|
||||||
|
height = 510
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption("Minesweeper")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
background = (51, 51, 51)
|
||||||
|
white = (236, 240, 241)
|
||||||
|
darkWhite = (174, 182, 191)
|
||||||
|
gray = (52, 73, 94)
|
||||||
|
yellow = (244, 208, 63)
|
||||||
|
lightred = (236, 112, 99)
|
||||||
|
|
||||||
|
grid = []
|
||||||
|
size = 10
|
||||||
|
mineProb = 30
|
||||||
|
|
||||||
|
safeSpots = 0
|
||||||
|
revealedSpots = 0
|
||||||
|
|
||||||
|
numberFont = pygame.font.SysFont("Times New Roman", width/(size*2))
|
||||||
|
font = pygame.font.SysFont("Times New Roman", 35)
|
||||||
|
|
||||||
|
# Property of Each Cell on Grid
|
||||||
|
class Spot:
|
||||||
|
def __init__(self, x, y, w, h, mineState):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.isMine = mineState
|
||||||
|
self.neighbors = 0
|
||||||
|
self.reveal = False
|
||||||
|
|
||||||
|
# Draw Cells
|
||||||
|
def draw(self):
|
||||||
|
if not self.reveal:
|
||||||
|
pygame.draw.rect(display, white, (self.x, self.y, self.w, self.h))
|
||||||
|
else:
|
||||||
|
pygame.draw.rect(display, darkWhite, (self.x, self.y, self.w, self.h))
|
||||||
|
|
||||||
|
if self.reveal:
|
||||||
|
if self.isMine:
|
||||||
|
pygame.draw.ellipse(display, gray, (self.x + self.w/4, self.y + self.h/4, self.w/2, self.h/2))
|
||||||
|
else:
|
||||||
|
if not self.neighbors == 0:
|
||||||
|
num = numberFont.render(str(self.neighbors), True, gray)
|
||||||
|
display.blit(num, (self.x + self.w/4, self.y + self.h/4))
|
||||||
|
|
||||||
|
# Check if the Cell is a Mine and reveal others if Block has no Mine Surrounding
|
||||||
|
def checkForMine(self, i, j):
|
||||||
|
global revealedSpots
|
||||||
|
self.reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
|
||||||
|
if self.isMine:
|
||||||
|
for i in range(size):
|
||||||
|
for j in range(size):
|
||||||
|
grid[i][j].reveal = True
|
||||||
|
|
||||||
|
drawGrid()
|
||||||
|
pygame.display.update()
|
||||||
|
gameLost()
|
||||||
|
|
||||||
|
elif grid[i][j].neighbors == 0:
|
||||||
|
if i > 0:
|
||||||
|
if not grid[i-1][j].isMine:
|
||||||
|
grid[i-1][j].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
if j > 0:
|
||||||
|
if not grid[i][j-1].isMine:
|
||||||
|
grid[i][j-1].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
if i < size - 1:
|
||||||
|
if not grid[i+1][j].isMine:
|
||||||
|
grid[i+1][j].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
if j < size - 1:
|
||||||
|
if not grid[i][j+1].isMine:
|
||||||
|
grid[i][j+1].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
if i > 0 and j > 0:
|
||||||
|
if not grid[i-1][j-1].isMine:
|
||||||
|
grid[i-1][j-1].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
if i > 0 and j < size - 1:
|
||||||
|
if not grid[i-1][j+1].isMine:
|
||||||
|
grid[i-1][j+1].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
if i < size - 1 and j > 0:
|
||||||
|
if not grid[i+1][j-1].isMine:
|
||||||
|
grid[i+1][j-1].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
if i < size - 1 and j < size - 1:
|
||||||
|
if not grid[i+1][j+1].isMine:
|
||||||
|
grid[i+1][j+1].reveal = True
|
||||||
|
revealedSpots += 1
|
||||||
|
|
||||||
|
# Count Neighboring Mines
|
||||||
|
def countNeighborMines(self, i, j):
|
||||||
|
if not self.isMine:
|
||||||
|
if i > 0:
|
||||||
|
if grid[i-1][j].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
if j > 0:
|
||||||
|
if grid[i][j-1].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
if i < size - 1:
|
||||||
|
if grid[i+1][j].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
if j < size - 1:
|
||||||
|
if grid[i][j+1].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
if i > 0 and j > 0:
|
||||||
|
if grid[i-1][j-1].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
if i > 0 and j < size - 1:
|
||||||
|
if grid[i-1][j+1].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
if i < size - 1 and j > 0:
|
||||||
|
if grid[i+1][j-1].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
if i < size - 1 and j < size - 1:
|
||||||
|
if grid[i+1][j+1].isMine:
|
||||||
|
self.neighbors += 1
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize the Grid
|
||||||
|
def generateGrid():
|
||||||
|
global grid, safeSpots
|
||||||
|
grid = []
|
||||||
|
for i in range(size):
|
||||||
|
grid.append([])
|
||||||
|
for j in range(size):
|
||||||
|
prob = random.randint(1, 100)
|
||||||
|
if prob < mineProb:
|
||||||
|
newObj = Spot((width/size)*(j), (height/size)*(i), width/size - 3, height/size - 3, True)
|
||||||
|
else:
|
||||||
|
safeSpots += 1
|
||||||
|
newObj = Spot((width/size)*(j), (height/size)*(i), width/size - 3, height/size - 3, False)
|
||||||
|
|
||||||
|
grid[i].append(newObj)
|
||||||
|
|
||||||
|
for i in range(size):
|
||||||
|
for j in range(size):
|
||||||
|
grid[i][j].countNeighborMines(i, j)
|
||||||
|
|
||||||
|
# Check if Grid is solved
|
||||||
|
def gameWon():
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
reset()
|
||||||
|
|
||||||
|
font.set_bold(True)
|
||||||
|
text = font.render("You Won the Minesweeper!", True, yellow)
|
||||||
|
display.blit(text, (width/2 - 250, height/2))
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
# Check if Game is Lost
|
||||||
|
def gameLost():
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
reset()
|
||||||
|
|
||||||
|
font.set_bold(True)
|
||||||
|
text = font.render("You Lost the Game!", True, (236, 112, 99))
|
||||||
|
display.blit(text, (width/2 - 250, height/2))
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
# Draw the Grid
|
||||||
|
def drawGrid():
|
||||||
|
for i in range(size):
|
||||||
|
for j in range(size):
|
||||||
|
grid[i][j].draw()
|
||||||
|
|
||||||
|
# Reset the Grid
|
||||||
|
def reset():
|
||||||
|
minesweeper()
|
||||||
|
|
||||||
|
# Close the Game
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# The Game
|
||||||
|
def minesweeper():
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
generateGrid()
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
reset()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
pos = pygame.mouse.get_pos()
|
||||||
|
j = pos[0]/(width/size)
|
||||||
|
i = pos[1]/(width/size)
|
||||||
|
grid[i][j].checkForMine(i, j)
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
|
||||||
|
drawGrid()
|
||||||
|
|
||||||
|
if revealedSpots == safeSpots:
|
||||||
|
gameWon()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
minesweeper()
|
259
Pong/Pong.py
Normal file
@ -0,0 +1,259 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Pong
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, random, math
|
||||||
|
#
|
||||||
|
# Controls - Arrow Keys for Right Paddle and WASD Keys for Left Paddle
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
from math import *
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 600
|
||||||
|
height = 400
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption("Pong!")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
background = (27, 38, 49)
|
||||||
|
white = (236, 240, 241)
|
||||||
|
red = (203, 67, 53)
|
||||||
|
blue = (52, 152, 219)
|
||||||
|
yellow = (244, 208, 63)
|
||||||
|
|
||||||
|
top = white
|
||||||
|
bottom = white
|
||||||
|
left = white
|
||||||
|
right = white
|
||||||
|
|
||||||
|
margin = 4
|
||||||
|
|
||||||
|
scoreLeft = 0
|
||||||
|
scoreRight = 0
|
||||||
|
maxScore = 20
|
||||||
|
|
||||||
|
font = pygame.font.SysFont("Small Fonts", 30)
|
||||||
|
largeFont = pygame.font.SysFont("Small Fonts", 60)
|
||||||
|
|
||||||
|
# Draw the Boundary of Board
|
||||||
|
def boundary():
|
||||||
|
global top, bottom, left, right
|
||||||
|
pygame.draw.rect(display, left, (0, 0, margin, height))
|
||||||
|
pygame.draw.rect(display, top, (0, 0, width, margin))
|
||||||
|
pygame.draw.rect(display, right, (width-margin, 0, margin, height))
|
||||||
|
pygame.draw.rect(display, bottom, (0, height - margin, width, margin))
|
||||||
|
|
||||||
|
l = 25
|
||||||
|
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 10, margin, l))
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 60, margin, l))
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 110, margin, l))
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 160, margin, l))
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 210, margin, l))
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 260, margin, l))
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 310, margin, l))
|
||||||
|
pygame.draw.rect(display, white, (width/2-margin/2, 360, margin, l))
|
||||||
|
|
||||||
|
# Paddle Class
|
||||||
|
class Paddle:
|
||||||
|
def __init__(self, position):
|
||||||
|
self.w = 10
|
||||||
|
self.h = self.w*8
|
||||||
|
self.paddleSpeed = 6
|
||||||
|
|
||||||
|
if position == -1:
|
||||||
|
self.x = 1.5*margin
|
||||||
|
else:
|
||||||
|
self.x = width - 1.5*margin - self.w
|
||||||
|
|
||||||
|
self.y = height/2 - self.h/2
|
||||||
|
|
||||||
|
# Show the Paddle
|
||||||
|
def show(self):
|
||||||
|
pygame.draw.rect(display, white, (self.x, self.y, self.w, self.h))
|
||||||
|
|
||||||
|
# Move the Paddle
|
||||||
|
def move(self, ydir):
|
||||||
|
self.y += self.paddleSpeed*ydir
|
||||||
|
if self.y < 0:
|
||||||
|
self.y -= self.paddleSpeed*ydir
|
||||||
|
elif self.y + self.h> height:
|
||||||
|
self.y -= self.paddleSpeed*ydir
|
||||||
|
|
||||||
|
|
||||||
|
leftPaddle = Paddle(-1)
|
||||||
|
rightPaddle = Paddle(1)
|
||||||
|
|
||||||
|
# Ball Class
|
||||||
|
class Ball:
|
||||||
|
def __init__(self, color):
|
||||||
|
self.r = 20
|
||||||
|
self.x = width/2 - self.r/2
|
||||||
|
self.y = height/2 -self.r/2
|
||||||
|
self.color = color
|
||||||
|
self.angle = random.randint(-75, 75)
|
||||||
|
if random.randint(0, 1):
|
||||||
|
self.angle += 180
|
||||||
|
|
||||||
|
self.speed = 8
|
||||||
|
|
||||||
|
# Show the Ball
|
||||||
|
def show(self):
|
||||||
|
pygame.draw.ellipse(display, self.color, (self.x, self.y, self.r, self.r))
|
||||||
|
|
||||||
|
# Move the Ball
|
||||||
|
def move(self):
|
||||||
|
global scoreLeft, scoreRight
|
||||||
|
self.x += self.speed*cos(radians(self.angle))
|
||||||
|
self.y += self.speed*sin(radians(self.angle))
|
||||||
|
if self.x + self.r > width - margin:
|
||||||
|
scoreLeft += 1
|
||||||
|
self.angle = 180 - self.angle
|
||||||
|
if self.x < margin:
|
||||||
|
scoreRight += 1
|
||||||
|
self.angle = 180 - self.angle
|
||||||
|
if self.y < margin:
|
||||||
|
self.angle = - self.angle
|
||||||
|
if self.y + self.r >=height - margin:
|
||||||
|
self.angle = - self.angle
|
||||||
|
|
||||||
|
# Check and Reflect the Ball when it hits the padddle
|
||||||
|
def checkForPaddle(self):
|
||||||
|
if self.x < width/2:
|
||||||
|
if leftPaddle.x < self.x < leftPaddle.x + leftPaddle.w:
|
||||||
|
if leftPaddle.y < self.y < leftPaddle.y + 10 or leftPaddle.y < self.y + self.r< leftPaddle.y + 10:
|
||||||
|
self.angle = -45
|
||||||
|
if leftPaddle.y + 10 < self.y < leftPaddle.y + 20 or leftPaddle.y + 10 < self.y + self.r< leftPaddle.y + 20:
|
||||||
|
self.angle = -30
|
||||||
|
if leftPaddle.y + 20 < self.y < leftPaddle.y + 30 or leftPaddle.y + 20 < self.y + self.r< leftPaddle.y + 30:
|
||||||
|
self.angle = -15
|
||||||
|
if leftPaddle.y + 30 < self.y < leftPaddle.y + 40 or leftPaddle.y + 30 < self.y + self.r< leftPaddle.y + 40:
|
||||||
|
self.angle = -10
|
||||||
|
if leftPaddle.y + 40 < self.y < leftPaddle.y + 50 or leftPaddle.y + 40 < self.y + self.r< leftPaddle.y + 50:
|
||||||
|
self.angle = 10
|
||||||
|
if leftPaddle.y + 50 < self.y < leftPaddle.y + 60 or leftPaddle.y + 50 < self.y + self.r< leftPaddle.y + 60:
|
||||||
|
self.angle = 15
|
||||||
|
if leftPaddle.y + 60 < self.y < leftPaddle.y + 70 or leftPaddle.y + 60 < self.y + self.r< leftPaddle.y + 70:
|
||||||
|
self.angle = 30
|
||||||
|
if leftPaddle.y + 70 < self.y < leftPaddle.y + 80 or leftPaddle.y + 70 < self.y + self.r< leftPaddle.y + 80:
|
||||||
|
self.angle = 45
|
||||||
|
else:
|
||||||
|
if rightPaddle.x + rightPaddle.w > self.x + self.r > rightPaddle.x:
|
||||||
|
if rightPaddle.y < self.y < leftPaddle.y + 10 or leftPaddle.y < self.y + self.r< leftPaddle.y + 10:
|
||||||
|
self.angle = -135
|
||||||
|
if rightPaddle.y + 10 < self.y < rightPaddle.y + 20 or rightPaddle.y + 10 < self.y + self.r< rightPaddle.y + 20:
|
||||||
|
self.angle = -150
|
||||||
|
if rightPaddle.y + 20 < self.y < rightPaddle.y + 30 or rightPaddle.y + 20 < self.y + self.r< rightPaddle.y + 30:
|
||||||
|
self.angle = -165
|
||||||
|
if rightPaddle.y + 30 < self.y < rightPaddle.y + 40 or rightPaddle.y + 30 < self.y + self.r< rightPaddle.y + 40:
|
||||||
|
self.angle = 170
|
||||||
|
if rightPaddle.y + 40 < self.y < rightPaddle.y + 50 or rightPaddle.y + 40 < self.y + self.r< rightPaddle.y + 50:
|
||||||
|
self.angle = 190
|
||||||
|
if rightPaddle.y + 50 < self.y < rightPaddle.y + 60 or rightPaddle.y + 50 < self.y + self.r< rightPaddle.y + 60:
|
||||||
|
self.angle = 165
|
||||||
|
if rightPaddle.y + 60 < self.y < rightPaddle.y + 70 or rightPaddle.y + 60 < self.y + self.r< rightPaddle.y + 70:
|
||||||
|
self.angle = 150
|
||||||
|
if rightPaddle.y + 70 < self.y < rightPaddle.y + 80 or rightPaddle.y + 70 < self.y + self.r< rightPaddle.y + 80:
|
||||||
|
self.angle = 135
|
||||||
|
|
||||||
|
# Show the Score
|
||||||
|
def showScore():
|
||||||
|
leftScoreText = font.render("Score : " + str(scoreLeft), True, red)
|
||||||
|
rightScoreText = font.render("Score : " + str(scoreRight), True, blue)
|
||||||
|
|
||||||
|
display.blit(leftScoreText, (3*margin, 3*margin))
|
||||||
|
display.blit(rightScoreText, (width/2 + 3*margin, 3*margin))
|
||||||
|
|
||||||
|
# Game Over
|
||||||
|
def gameOver():
|
||||||
|
if scoreLeft == maxScore or scoreRight == maxScore:
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
reset()
|
||||||
|
if scoreLeft == maxScore:
|
||||||
|
playerWins = largeFont.render("Left Player Wins!", True, red)
|
||||||
|
elif scoreRight == maxScore:
|
||||||
|
playerWins = largeFont.render("Right Player Wins!", True, blue)
|
||||||
|
|
||||||
|
display.blit(playerWins, (width/2 - 100, height/2))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
def reset():
|
||||||
|
global scoreLeft, scoreRight
|
||||||
|
scoreLeft = 0
|
||||||
|
scoreRight = 0
|
||||||
|
board()
|
||||||
|
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
def board():
|
||||||
|
loop = True
|
||||||
|
leftChange = 0
|
||||||
|
rightChange = 0
|
||||||
|
ball = Ball(yellow)
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_SPACE or event.key == pygame.K_p:
|
||||||
|
Pause()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
reset()
|
||||||
|
if event.key == pygame.K_w:
|
||||||
|
leftChange = -1
|
||||||
|
if event.key == pygame.K_s:
|
||||||
|
leftChange = 1
|
||||||
|
if event.key == pygame.K_UP:
|
||||||
|
rightChange = -1
|
||||||
|
if event.key == pygame.K_DOWN:
|
||||||
|
rightChange = 1
|
||||||
|
if event.type == pygame.KEYUP:
|
||||||
|
leftChange = 0
|
||||||
|
rightChange = 0
|
||||||
|
|
||||||
|
leftPaddle.move(leftChange)
|
||||||
|
rightPaddle.move(rightChange)
|
||||||
|
ball.move()
|
||||||
|
ball.checkForPaddle()
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
showScore()
|
||||||
|
|
||||||
|
ball.show()
|
||||||
|
leftPaddle.show()
|
||||||
|
rightPaddle.show()
|
||||||
|
|
||||||
|
boundary()
|
||||||
|
|
||||||
|
gameOver()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
board()
|
42
README.md
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
# Gaming-in-Python
|
||||||
|
|
||||||
|
Here you can find the source code to all the Games I made using Python Programming Language
|
||||||
|
|
||||||
|
Details about all the Games can be Found on my Blog : https://jatinmandav.wordpress.com
|
||||||
|
|
||||||
|
## List of Games
|
||||||
|
- [Balloon Shooter](BalloonShooter/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U
|
||||||
|
- [Minesweeper](Minesweeper/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U
|
||||||
|
- [Pong!](Pong/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U
|
||||||
|
- [Snake 2D](Snake_2d/)
|
||||||
|
- More about Snake 2D : https://jatinmandav.wordpress.com/2017/08/10/snake-game-in-python/
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=ix1wyDwfuIU&t=41s
|
||||||
|
- [Space Invaders](Space_Invaders/)
|
||||||
|
- More about Space Invaders : https://jatinmandav.wordpress.com/2017/08/14/space-invaders-in-python/
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=XryDmOnWY8A&t=25s
|
||||||
|
- [Space Invaders 2](space_Invaders_2)
|
||||||
|
- [StopWatch](Stopwatch/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U
|
||||||
|
- [Tron 2D](Tron/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=2yxmoxSjv6U
|
||||||
|
- [Chain Reaction](Chain_Reaction/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=U0Hii6Jc9c8&t=1s
|
||||||
|
- More About Chain Reaction : https://jatinmandav.wordpress.com/2017/09/27/chain-reaction-in-python/
|
||||||
|
- [8 Ball Pool](8_Ball_Pool/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=K2sJSeN85eo&feature=youtu.be
|
||||||
|
- More About 8 Ball Pool : https://jatinmandav.wordpress.com/2017/10/13/8-ball-pool-in-python/
|
||||||
|
- [Stack Tower](Stack_Tower/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=SgtqhG28JnM&feature=youtu.be
|
||||||
|
- More About Stack Tower : https://jatinmandav.wordpress.com/2017/11/05/stacks-in-python-using-pygame/
|
||||||
|
- [Dodge The Ball!](Dodge_The_Ball/)
|
||||||
|
- Time Lapse Video on YouTube : https://www.youtube.com/watch?v=PD17M_Dwdf8&feature=youtu.be
|
||||||
|
- [Battles](Battles/)
|
||||||
|
Battles is a game inspired by popular mobile video game Clash of Clans
|
||||||
|
- Video on YouTube : https://www.youtube.com/watch?v=QmZWxahFvzw&t=0s
|
||||||
|
- More details on Blog : https://jatinmandav.wordpress.com/2018/01/03/battles-game-inspired-by-clash-of-clans/
|
||||||
|
- [Angry Birds](Angry_Birds/)
|
||||||
|
- Video on YouTube : [Angry Birds - 14 Hour Time Lapse](https://www.youtube.com/watch?v=6in-mdiumcA&feature=youtu.be)
|
||||||
|
- More on Blog : [Angry Birds in Python Using PyGame](https://jatinmandav.wordpress.com/2018/05/25/angry-birds-in-python-using-pygame/)
|
276
Snake_2d/snake.py
Normal file
@ -0,0 +1,276 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Snake - 2D
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, random, copy, time
|
||||||
|
#
|
||||||
|
# Controls - Arrow Keys
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import copy
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 500
|
||||||
|
height = 500
|
||||||
|
scale = 10
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
food_x = 10
|
||||||
|
food_y = 10
|
||||||
|
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption("Snake Game")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
background = (23, 32, 42)
|
||||||
|
snake_colour = (236, 240, 241)
|
||||||
|
food_colour = (148, 49, 38)
|
||||||
|
snake_head = (247, 220, 111)
|
||||||
|
|
||||||
|
|
||||||
|
# ----------- Snake Class ----------------
|
||||||
|
# self.history[0][0] is the location of the head of the snake
|
||||||
|
|
||||||
|
class Snake:
|
||||||
|
def __init__(self, x_start, y_start):
|
||||||
|
self.x = x_start
|
||||||
|
self.y = y_start
|
||||||
|
self.w = 10
|
||||||
|
self.h = 10
|
||||||
|
self.x_dir = 1
|
||||||
|
self.y_dir = 0
|
||||||
|
self.history = [[self.x, self.y]]
|
||||||
|
self.length = 1
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.x = width/2-scale
|
||||||
|
self.y = height/2-scale
|
||||||
|
self.w = 10
|
||||||
|
self.h = 10
|
||||||
|
self.x_dir = 1
|
||||||
|
self.y_dir = 0
|
||||||
|
self.history = [[self.x, self.y]]
|
||||||
|
self.length = 1
|
||||||
|
|
||||||
|
#function to show the body of snake
|
||||||
|
def show(self):
|
||||||
|
for i in range(self.length):
|
||||||
|
if not i == 0:
|
||||||
|
pygame.draw.rect(display, snake_colour, (self.history[i][0], self.history[i][1], self.w, self.h))
|
||||||
|
else:
|
||||||
|
pygame.draw.rect(display, snake_head, (self.history[i][0], self.history[i][1], self.w, self.h))
|
||||||
|
|
||||||
|
|
||||||
|
def check_eaten(self):
|
||||||
|
if abs(self.history[0][0] - food_x) < scale and abs(self.history[0][1] - food_y) < scale:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def grow(self):
|
||||||
|
self.length += 1
|
||||||
|
self.history.append(self.history[self.length-2])
|
||||||
|
|
||||||
|
def death(self):
|
||||||
|
i = self.length - 1
|
||||||
|
while i > 0:
|
||||||
|
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:
|
||||||
|
return True
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
i = self.length - 1
|
||||||
|
while i > 0:
|
||||||
|
self.history[i] = copy.deepcopy(self.history[i-1])
|
||||||
|
i -= 1
|
||||||
|
self.history[0][0] += self.x_dir*scale
|
||||||
|
self.history[0][1] += self.y_dir*scale
|
||||||
|
|
||||||
|
def autoplay(self):
|
||||||
|
|
||||||
|
if abs(food_x-self.history[0][0]) < 10 and abs(food_y-self.history[0][1]) < 10:
|
||||||
|
# if self.check_eaten():
|
||||||
|
# food.new_location()
|
||||||
|
# score += 1
|
||||||
|
# self.grow()
|
||||||
|
print("")
|
||||||
|
elif abs(food_x-self.history[0][0]) < 10:
|
||||||
|
|
||||||
|
# if self.y_dir==1 or self.y_dir==-1:
|
||||||
|
# self.y_dir=0
|
||||||
|
# self.x_dir=1
|
||||||
|
if self.x_dir==1 or self.x_dir==-1:
|
||||||
|
if food_y>self.history[0][1]:
|
||||||
|
self.y_dir=1
|
||||||
|
else:
|
||||||
|
self.y_dir=-1
|
||||||
|
self.x_dir=0
|
||||||
|
elif abs(food_y-self.history[0][1]) < 10 :
|
||||||
|
|
||||||
|
# if self.x_dir==1 or self.x_dir==-1:
|
||||||
|
# self.x_dir=0
|
||||||
|
# self.y_dir=1
|
||||||
|
if self.y_dir==1 or self.y_dir==-1:
|
||||||
|
self.y_dir=0
|
||||||
|
if food_x>self.history[0][0]:
|
||||||
|
self.x_dir=1
|
||||||
|
else:
|
||||||
|
self.x_dir=-1
|
||||||
|
|
||||||
|
|
||||||
|
elif food_x-self.history[0][0] >= 10 and food_y-self.history[0][1] >= 10:
|
||||||
|
|
||||||
|
if self.x_dir==-1:
|
||||||
|
self.y_dir=1
|
||||||
|
self.x_dir=0
|
||||||
|
elif self.y_dir==-1:
|
||||||
|
self.y_dir=0
|
||||||
|
self.x_dir=1
|
||||||
|
elif self.history[0][0]-food_x >= 10 and food_y-self.history[0][1] >= 10:
|
||||||
|
|
||||||
|
if self.x_dir==1:
|
||||||
|
self.y_dir=1
|
||||||
|
self.x_dir=0
|
||||||
|
elif self.y_dir==1:
|
||||||
|
self.y_dir=0
|
||||||
|
self.x_dir=-1
|
||||||
|
elif self.history[0][0]-food_x >= 10 and self.history[0][1]-food_y >= 10:
|
||||||
|
|
||||||
|
if self.x_dir==1:
|
||||||
|
self.y_dir=-1
|
||||||
|
self.x_dir=0
|
||||||
|
elif self.y_dir==1:
|
||||||
|
self.y_dir=0
|
||||||
|
self.x_dir=-1
|
||||||
|
|
||||||
|
elif food_x-self.history[0][0] >= 10 and self.history[0][1]-food_y >= 10:
|
||||||
|
|
||||||
|
if self.x_dir==-1:
|
||||||
|
self.y_dir=-1
|
||||||
|
self.x_dir=0
|
||||||
|
elif self.y_dir==1:
|
||||||
|
self.y_dir=0
|
||||||
|
self.x_dir=1
|
||||||
|
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# ----------- Food Class --------------
|
||||||
|
class Food:
|
||||||
|
def new_location(self):
|
||||||
|
global food_x, food_y
|
||||||
|
food_x = random.randrange(1, width/scale-1)*scale
|
||||||
|
food_y = random.randrange(1, height/scale-1)*scale
|
||||||
|
|
||||||
|
def show(self):
|
||||||
|
pygame.draw.rect(display, food_colour, (food_x, food_y, scale, scale))
|
||||||
|
|
||||||
|
|
||||||
|
def show_score():
|
||||||
|
font = pygame.font.SysFont("Copperplate Gothic Bold", 20)
|
||||||
|
text = font.render("Score: " + str(score), True, snake_colour)
|
||||||
|
display.blit(text, (scale, scale))
|
||||||
|
|
||||||
|
|
||||||
|
# ----------- Main Game Loop -------------
|
||||||
|
def gameLoop():
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
global score
|
||||||
|
|
||||||
|
snake = Snake(width/2, height/2) #starting from mid of grid
|
||||||
|
food = Food()
|
||||||
|
food.new_location()
|
||||||
|
ap=False
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
snake.show()
|
||||||
|
food.show()
|
||||||
|
show_score()
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
if event.key==pygame.K_SPACE: #autoplay start
|
||||||
|
ap=True
|
||||||
|
if event.key==pygame.K_TAB: #autoplay end
|
||||||
|
ap=False
|
||||||
|
else:
|
||||||
|
if snake.y_dir == 0:
|
||||||
|
if event.key == pygame.K_UP:
|
||||||
|
snake.x_dir = 0
|
||||||
|
snake.y_dir = -1
|
||||||
|
if event.key == pygame.K_DOWN:
|
||||||
|
snake.x_dir = 0
|
||||||
|
snake.y_dir = 1
|
||||||
|
|
||||||
|
if snake.x_dir == 0:
|
||||||
|
if event.key == pygame.K_LEFT:
|
||||||
|
snake.x_dir = -1
|
||||||
|
snake.y_dir = 0
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
snake.x_dir = 1
|
||||||
|
snake.y_dir = 0
|
||||||
|
|
||||||
|
|
||||||
|
if ap:
|
||||||
|
snake.autoplay()
|
||||||
|
else:
|
||||||
|
snake.update()
|
||||||
|
|
||||||
|
|
||||||
|
if snake.check_eaten():
|
||||||
|
food.new_location()
|
||||||
|
score += 1
|
||||||
|
snake.grow()
|
||||||
|
|
||||||
|
|
||||||
|
if snake.death():
|
||||||
|
score = 0
|
||||||
|
font = pygame.font.SysFont("Copperplate Gothic Bold", 50)
|
||||||
|
text = font.render("Game Over!", True, snake_colour)
|
||||||
|
display.blit(text, (width/2-50, height/2))
|
||||||
|
pygame.display.update()
|
||||||
|
time.sleep(3)
|
||||||
|
snake.reset()
|
||||||
|
|
||||||
|
|
||||||
|
#updating the values if snake goes out of board
|
||||||
|
if snake.history[0][0] > width:
|
||||||
|
snake.history[0][0] = 0
|
||||||
|
if snake.history[0][0] < 0:
|
||||||
|
snake.history[0][0] = width
|
||||||
|
|
||||||
|
if snake.history[0][1] > height:
|
||||||
|
snake.history[0][1] = 0
|
||||||
|
if snake.history[0][1] < 0:
|
||||||
|
snake.history[0][1] = height
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(10) #at most 10 frames should pass in 1 sec, it is used to control the speed of snake
|
||||||
|
|
||||||
|
gameLoop()
|
223
Space_Invaders/space invaders.py
Normal file
@ -0,0 +1,223 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Space Invaders
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, time
|
||||||
|
#
|
||||||
|
# Controls - Left and Right Keys to Move, Space to shoot
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
# -------------- Initialization ------------
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 700
|
||||||
|
height = 500
|
||||||
|
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.display.set_caption("Space Invaders")
|
||||||
|
|
||||||
|
ship_width = 40
|
||||||
|
ship_height = 30
|
||||||
|
|
||||||
|
# -------------- Colours -----------------
|
||||||
|
background = (74, 35, 90)
|
||||||
|
white = (244, 246, 247)
|
||||||
|
yellow = (241, 196, 15)
|
||||||
|
orange = (186, 74, 0)
|
||||||
|
green = (35, 155, 86)
|
||||||
|
white1 = (253, 254, 254)
|
||||||
|
dark_gray = (23, 32, 42)
|
||||||
|
|
||||||
|
|
||||||
|
# -------------- Space-Ship Class --------------
|
||||||
|
class SpaceShip:
|
||||||
|
def __init__(self, x, y, w, h, colour):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = w
|
||||||
|
self.h = h
|
||||||
|
self.colour = colour
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.rect(display, yellow, (self.x + self.w/2 - 8, self.y - 10, 16, 10))
|
||||||
|
pygame.draw.rect(display, self.colour, (self.x, self.y, self.w, self.h))
|
||||||
|
pygame.draw.rect(display, dark_gray, (self.x + 5, self.y + 6, 10, self.h - 10))
|
||||||
|
pygame.draw.rect(display, dark_gray, (self.x + self.w - 15, self.y + 6, 10, self.h - 10))
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------- Bullet Class -------------
|
||||||
|
class Bullet:
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.d = 10
|
||||||
|
self.speed = -5
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.ellipse(display, orange, (self.x, self.y, self.d, self.d))
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.y += self.speed
|
||||||
|
|
||||||
|
def hit(self, x, y, d):
|
||||||
|
if x < self.x < x + d:
|
||||||
|
if y + d > self.y > y:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------ Alien Class ---------------
|
||||||
|
class Alien:
|
||||||
|
def __init__(self, x, y, d):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.d = d
|
||||||
|
self.x_dir = 1
|
||||||
|
self.speed = 3
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.ellipse(display, green, (self.x, self.y, self.d, self.d))
|
||||||
|
pygame.draw.ellipse(display, dark_gray, (self.x + 10, self.y + self.d/3, 8, 8), 2)
|
||||||
|
pygame.draw.ellipse(display, dark_gray, (self.x + self.d - 20, self.y + self.d/3, 8, 8), 2)
|
||||||
|
pygame.draw.rect(display, dark_gray, (self.x, self.y+self.d-20, 50, 7))
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.x += self.x_dir*self.speed
|
||||||
|
|
||||||
|
def shift_down(self):
|
||||||
|
self.y += self.d
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------- Saved ------------------
|
||||||
|
def saved():
|
||||||
|
font = pygame.font.SysFont("Wide Latin", 22)
|
||||||
|
font_large = pygame.font.SysFont("Wide Latin", 43)
|
||||||
|
text2 = font_large.render("Congratulations!", True, white1)
|
||||||
|
text = font.render("You Prevented the Alien Invasion!", True, white1)
|
||||||
|
display.blit(text2, (60, height/2))
|
||||||
|
display.blit(text, (45, height/2 + 100))
|
||||||
|
pygame.display.update()
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------- Death ----------------
|
||||||
|
def GameOver():
|
||||||
|
font = pygame.font.SysFont("Chiller", 50)
|
||||||
|
font_large = pygame.font.SysFont("Chiller", 100)
|
||||||
|
text2 = font_large.render("Game Over!", True, white1)
|
||||||
|
text = font.render("You Could not Prevent the Alien Invasion!", True, white1)
|
||||||
|
display.blit(text2, (180, height/2-50))
|
||||||
|
display.blit(text, (45, height/2 + 100))
|
||||||
|
|
||||||
|
|
||||||
|
# --------------------- The Game ------------------
|
||||||
|
def game():
|
||||||
|
invasion = False
|
||||||
|
ship = SpaceShip(width/2-ship_width/2, height-ship_height - 10, ship_width, ship_height, white)
|
||||||
|
|
||||||
|
bullets = []
|
||||||
|
num_bullet = 0
|
||||||
|
for i in range(num_bullet):
|
||||||
|
i = Bullet(width/2 - 5, height - ship_height - 20)
|
||||||
|
bullets.append(i)
|
||||||
|
|
||||||
|
x_move = 0
|
||||||
|
|
||||||
|
aliens = []
|
||||||
|
num_aliens = 8
|
||||||
|
d = 50
|
||||||
|
for i in range(num_aliens):
|
||||||
|
i = Alien((i+1)*d + i*20, d+20, d)
|
||||||
|
aliens.append(i)
|
||||||
|
|
||||||
|
while not invasion:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
x_move = 5
|
||||||
|
|
||||||
|
if event.key == pygame.K_LEFT:
|
||||||
|
x_move = -5
|
||||||
|
|
||||||
|
if event.key == pygame.K_SPACE:
|
||||||
|
num_bullet += 1
|
||||||
|
i = Bullet(ship.x + ship_width/2 - 5, ship.y)
|
||||||
|
bullets.append(i)
|
||||||
|
|
||||||
|
if event.type == pygame.KEYUP:
|
||||||
|
x_move = 0
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
|
||||||
|
for i in range(num_bullet):
|
||||||
|
bullets[i].draw()
|
||||||
|
bullets[i].move()
|
||||||
|
|
||||||
|
for alien in list(aliens):
|
||||||
|
alien.draw()
|
||||||
|
alien.move()
|
||||||
|
for item in list(bullets):
|
||||||
|
if item.hit(alien.x, alien.y, alien.d):
|
||||||
|
bullets.remove(item)
|
||||||
|
num_bullet -= 1
|
||||||
|
aliens.remove(alien)
|
||||||
|
num_aliens -= 1
|
||||||
|
|
||||||
|
if num_aliens == 0:
|
||||||
|
saved()
|
||||||
|
invasion = True
|
||||||
|
|
||||||
|
for i in range(num_aliens):
|
||||||
|
if aliens[i].x + d >= width:
|
||||||
|
for j in range(num_aliens):
|
||||||
|
aliens[j].x_dir = -1
|
||||||
|
aliens[j].shift_down()
|
||||||
|
|
||||||
|
if aliens[i].x <= 0:
|
||||||
|
for j in range(num_aliens):
|
||||||
|
aliens[j].x_dir = 1
|
||||||
|
aliens[j].shift_down()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if aliens[0].y + d > height:
|
||||||
|
GameOver()
|
||||||
|
pygame.display.update()
|
||||||
|
time.sleep(3)
|
||||||
|
invasion = True
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
ship.x += x_move
|
||||||
|
|
||||||
|
if ship.x < 0:
|
||||||
|
ship.x -= x_move
|
||||||
|
if ship.x + ship_width > width:
|
||||||
|
ship.x -= x_move
|
||||||
|
|
||||||
|
ship.draw()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
# ----------------- Calling the Game Function ---------------------
|
||||||
|
game()
|
220
Stack_Tower/Stacks.py
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Stack Tower
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys, random
|
||||||
|
#
|
||||||
|
# Controls - Mouse Click
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import random
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 400
|
||||||
|
height = 500
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
background = (23, 32, 42)
|
||||||
|
|
||||||
|
white = (236, 240, 241)
|
||||||
|
|
||||||
|
# Color Codes
|
||||||
|
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),
|
||||||
|
(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),
|
||||||
|
(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),
|
||||||
|
(232, 246, 243), (162, 217, 206), (162, 217, 206),
|
||||||
|
(115, 198, 182), (69, 179, 157), (22, 160, 133),
|
||||||
|
(19, 141, 117), (17, 122, 101), (14, 102, 85),
|
||||||
|
(11, 83, 69),
|
||||||
|
(21, 67, 96), (26, 82, 118), (31, 97, 141),
|
||||||
|
(36, 113, 163), (41, 128, 185), (84, 153, 199),
|
||||||
|
(127, 179, 213), (169, 204, 227), (212, 230, 241),
|
||||||
|
(234, 242, 248),
|
||||||
|
(251, 238, 230), (246, 221, 204), (237, 187, 153),
|
||||||
|
(229, 152, 102), (220, 118, 51), (211, 84, 0),
|
||||||
|
(186, 74, 0), (160, 64, 0), (135, 54, 0),
|
||||||
|
(110, 44, 0)
|
||||||
|
]
|
||||||
|
|
||||||
|
colorIndex = 0
|
||||||
|
|
||||||
|
brickH = 10
|
||||||
|
brickW = 100
|
||||||
|
|
||||||
|
score = 0
|
||||||
|
speed = 3
|
||||||
|
|
||||||
|
|
||||||
|
# Single Brick Class
|
||||||
|
class Brick:
|
||||||
|
def __init__(self, x, y, color, speed):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
self.w = brickW
|
||||||
|
self.h = brickH
|
||||||
|
self.color = color
|
||||||
|
self.speed = speed
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.rect(display, self.color, (self.x, self.y, self.w, self.h))
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
self.x += self.speed
|
||||||
|
if self.x > width:
|
||||||
|
self.speed *= -1
|
||||||
|
if self.x + self.w < 1:
|
||||||
|
self.speed *= -1
|
||||||
|
|
||||||
|
|
||||||
|
# Complete Stack
|
||||||
|
class Stack:
|
||||||
|
def __init__(self):
|
||||||
|
global colorIndex
|
||||||
|
self.stack = []
|
||||||
|
self.initSize = 25
|
||||||
|
for i in range(self.initSize):
|
||||||
|
newBrick = Brick(width/2 - brickW/2, height - (i + 1)*brickH, color[colorIndex], 0)
|
||||||
|
colorIndex += 1
|
||||||
|
self.stack.append(newBrick)
|
||||||
|
|
||||||
|
def show(self):
|
||||||
|
for i in range(self.initSize):
|
||||||
|
self.stack[i].draw()
|
||||||
|
|
||||||
|
def move(self):
|
||||||
|
for i in range(self.initSize):
|
||||||
|
self.stack[i].move()
|
||||||
|
|
||||||
|
def addNewBrick(self):
|
||||||
|
global colorIndex, speed
|
||||||
|
|
||||||
|
if colorIndex >= len(color):
|
||||||
|
colorIndex = 0
|
||||||
|
|
||||||
|
y = self.peek().y
|
||||||
|
if score > 50:
|
||||||
|
speed += 0
|
||||||
|
elif score%5 == 0:
|
||||||
|
speed += 1
|
||||||
|
|
||||||
|
newBrick = Brick(width, y - brickH, color[colorIndex], speed)
|
||||||
|
colorIndex += 1
|
||||||
|
self.initSize += 1
|
||||||
|
self.stack.append(newBrick)
|
||||||
|
|
||||||
|
def peek(self):
|
||||||
|
return self.stack[self.initSize - 1]
|
||||||
|
|
||||||
|
def pushToStack(self):
|
||||||
|
global brickW, score
|
||||||
|
b = self.stack[self.initSize - 2]
|
||||||
|
b2 = self.stack[self.initSize - 1]
|
||||||
|
if b2.x <= b.x and not (b2.x + b2.w < b.x):
|
||||||
|
self.stack[self.initSize - 1].w = self.stack[self.initSize - 1].x + self.stack[self.initSize - 1].w - b.x
|
||||||
|
self.stack[self.initSize - 1].x = b.x
|
||||||
|
if self.stack[self.initSize - 1].w > b.w:
|
||||||
|
self.stack[self.initSize - 1].w = b.w
|
||||||
|
self.stack[self.initSize - 1].speed = 0
|
||||||
|
score += 1
|
||||||
|
elif b.x <= b2.x <= b.x + b.w:
|
||||||
|
self.stack[self.initSize - 1].w = b.x + b.w - b2.x
|
||||||
|
self.stack[self.initSize - 1].speed = 0
|
||||||
|
score += 1
|
||||||
|
else:
|
||||||
|
gameOver()
|
||||||
|
for i in range(self.initSize):
|
||||||
|
self.stack[i].y += brickH
|
||||||
|
|
||||||
|
brickW = self.stack[self.initSize - 1].w
|
||||||
|
|
||||||
|
# Game Over
|
||||||
|
def gameOver():
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
font = pygame.font.SysFont("Agency FB", 60)
|
||||||
|
text = font.render("Game Over!", True, white)
|
||||||
|
|
||||||
|
textRect = text.get_rect()
|
||||||
|
textRect.center = (width/2, height/2 - 80)
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
gameLoop()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
gameLoop()
|
||||||
|
display.blit(text, textRect)
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick()
|
||||||
|
|
||||||
|
# Displaying the Score on Screen
|
||||||
|
def showScore():
|
||||||
|
font = pygame.font.SysFont("Forte", 30)
|
||||||
|
text = font.render("Score: " + str(score), True, white)
|
||||||
|
display.blit(text, (10, 10))
|
||||||
|
|
||||||
|
|
||||||
|
# Close the Window
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
# The Main Game Loop
|
||||||
|
def gameLoop():
|
||||||
|
global brickW, brickH, score, colorIndex, speed
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
brickH = 10
|
||||||
|
brickW = 100
|
||||||
|
colorIndex = 0
|
||||||
|
speed = 3
|
||||||
|
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
stack = Stack()
|
||||||
|
stack.addNewBrick()
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
gameLoop()
|
||||||
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
stack.pushToStack()
|
||||||
|
stack.addNewBrick()
|
||||||
|
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
|
||||||
|
stack.move()
|
||||||
|
stack.show()
|
||||||
|
|
||||||
|
showScore()
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
gameLoop()
|
108
Stopwatch/Stopwatch.py
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
# -------------------------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Stopwatch Using Pygame
|
||||||
|
# Pause or Unpause : Space Bar or 'p'
|
||||||
|
# Reset : 'r'
|
||||||
|
# Quit : 'q'
|
||||||
|
#
|
||||||
|
# Website : https://jatinmandav.wordpress.com
|
||||||
|
# YouTube : https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ?view_as=subscriber
|
||||||
|
#
|
||||||
|
# Facebook : facebook.com/jatinmandav
|
||||||
|
# Twitter : @jatinmandav
|
||||||
|
# Gmail : jatinmandav3@gmail.com
|
||||||
|
#
|
||||||
|
# -------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
# Initializing of Pygame
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 200
|
||||||
|
height = 100
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption(" ")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
dark_gray = (23, 32, 42)
|
||||||
|
white = (230, 230, 230)
|
||||||
|
|
||||||
|
seconds = 0
|
||||||
|
pause = False
|
||||||
|
|
||||||
|
# Font and Size
|
||||||
|
font = pygame.font.SysFont("Times New Roman", 24)
|
||||||
|
|
||||||
|
# Close the Window
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# Blit time and text to Pygame Window
|
||||||
|
def showTime():
|
||||||
|
hours = seconds/3600
|
||||||
|
minutes = (seconds/60)%60
|
||||||
|
sec = seconds%60
|
||||||
|
|
||||||
|
text = font.render("HH MM SS", True, white)
|
||||||
|
time = font.render(str(hours).zfill(2) + " " + str(minutes).zfill(2) + " " + str(sec).zfill(2), True, white)
|
||||||
|
display.blit(text, (10, 10))
|
||||||
|
display.blit(time, (13, 40))
|
||||||
|
|
||||||
|
# Pause the Stopwatch
|
||||||
|
def Pause():
|
||||||
|
while pause:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_SPACE or pygame.key == pygame.K_p:
|
||||||
|
stopWatch()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
reset()
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
|
||||||
|
pauseText = font.render("Paused!", True, white)
|
||||||
|
display.blit(pauseText, (10, height - 35))
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
# Reset StopWatch
|
||||||
|
def reset():
|
||||||
|
global seconds
|
||||||
|
seconds = 0
|
||||||
|
|
||||||
|
# StopWatch
|
||||||
|
def stopWatch():
|
||||||
|
tick = True
|
||||||
|
global seconds, pause
|
||||||
|
pause = False
|
||||||
|
while tick:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_SPACE or event.key == pygame.K_p:
|
||||||
|
pause = True
|
||||||
|
Pause()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
reset()
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
|
||||||
|
display.fill(dark_gray)
|
||||||
|
showTime()
|
||||||
|
seconds += 1
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(1)
|
||||||
|
|
||||||
|
stopWatch()
|
||||||
|
|
112
Tic Tac Toe .py
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
# Implementation of Two Player Tic-Tac-Toe game in Python.
|
||||||
|
|
||||||
|
''' We will make the board using dictionary
|
||||||
|
in which keys will be the location(i.e : top-left,mid-right,etc.)
|
||||||
|
and initialliy it's values will be empty space and then after every move
|
||||||
|
we will change the value according to player's choice of move. '''
|
||||||
|
|
||||||
|
theBoard = {'1': ' ', '2': ' ', '3': ' ',
|
||||||
|
'4': ' ', '5': ' ', '6': ' ',
|
||||||
|
'7': ' ', '8': ' ', '9': ' '}
|
||||||
|
|
||||||
|
board_keys = []
|
||||||
|
|
||||||
|
for key in theBoard:
|
||||||
|
board_keys.append(key)
|
||||||
|
|
||||||
|
''' We will have to print the updated board after every move in the game and
|
||||||
|
thus we will make a function in which we'll define the printBoard function
|
||||||
|
so that we can easily print the board everytime by calling this function. '''
|
||||||
|
|
||||||
|
|
||||||
|
def printBoard(board):
|
||||||
|
print(board['1'] + '|' + board['2'] + '|' + board['3'])
|
||||||
|
print('-+-+-')
|
||||||
|
print(board['4'] + '|' + board['5'] + '|' + board['6'])
|
||||||
|
print('-+-+-')
|
||||||
|
print(board['7'] + '|' + board['8'] + '|' + board['9'])
|
||||||
|
|
||||||
|
|
||||||
|
# Now we'll write the main function which has all the gameplay functionality.
|
||||||
|
def game():
|
||||||
|
turn = 'X'
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("It's your turn," + turn + ".Move to which place?")
|
||||||
|
|
||||||
|
move = input()
|
||||||
|
|
||||||
|
if theBoard[move] == ' ':
|
||||||
|
theBoard[move] = turn
|
||||||
|
count += 1
|
||||||
|
else:
|
||||||
|
print("That place is already filled.\nMove to which place?")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Now we will check if player X or O has won,for every move after 5 moves.
|
||||||
|
if count >= 5:
|
||||||
|
if theBoard['1'] == theBoard['2'] == theBoard['3'] != ' ': # across the top
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
elif theBoard['4'] == theBoard['5'] == theBoard['6'] != ' ': # across the middle
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
elif theBoard['7'] == theBoard['8'] == theBoard['9'] != ' ': # across the bottom
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
elif theBoard['7'] == theBoard['4'] == theBoard['1'] != ' ': # down the left side
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
elif theBoard['8'] == theBoard['5'] == theBoard['2'] != ' ': # down the middle
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
elif theBoard['9'] == theBoard['6'] == theBoard['3'] != ' ': # down the right side
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
elif theBoard['1'] == theBoard['5'] == theBoard['9'] != ' ': # diagonal
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
elif theBoard['7'] == theBoard['5'] == theBoard['3'] != ' ': # diagonal
|
||||||
|
printBoard(theBoard)
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print(" **** " + turn + " won. ****")
|
||||||
|
break
|
||||||
|
|
||||||
|
# If neither X nor O wins and the board is full, we'll declare the result as 'tie'.
|
||||||
|
if count == 9:
|
||||||
|
print("\nGame Over.\n")
|
||||||
|
print("It's a Tie!!")
|
||||||
|
|
||||||
|
# Now we have to change the player after every move.
|
||||||
|
if turn == 'X':
|
||||||
|
turn = 'O'
|
||||||
|
else:
|
||||||
|
turn = 'X'
|
||||||
|
|
||||||
|
# Now we will ask if player wants to restart the game or not.
|
||||||
|
restart = input("Do want to play Again?(y/n)")
|
||||||
|
if restart == "y" or restart == "Y":
|
||||||
|
for key in board_keys:
|
||||||
|
theBoard[key] = " "
|
||||||
|
|
||||||
|
game()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
game()
|
181
Tron/Tron.py
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# Tron
|
||||||
|
# Language - Python
|
||||||
|
# Modules - pygame, sys
|
||||||
|
#
|
||||||
|
# Controls - Arrow Keys for Player 2(Yellow) and WASD Keys for Player 1(Red)
|
||||||
|
#
|
||||||
|
# By - Jatin Kumar Mandav
|
||||||
|
#
|
||||||
|
# Website - https://jatinmandav.wordpress.com
|
||||||
|
#
|
||||||
|
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
|
||||||
|
# Twitter - @jatinmandav
|
||||||
|
#
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
import sys
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
width = 600
|
||||||
|
height = 600
|
||||||
|
display = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption("Tron 2D")
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
background = (27, 79, 114)
|
||||||
|
white = (236, 240, 241)
|
||||||
|
yellow = (241, 196, 15)
|
||||||
|
darkYellow = (247, 220, 111)
|
||||||
|
red = (231, 76, 60)
|
||||||
|
darkRed = (241, 148, 138)
|
||||||
|
darkBlue = (40, 116, 166)
|
||||||
|
|
||||||
|
font = pygame.font.SysFont("Agency FB", 65)
|
||||||
|
|
||||||
|
w = 10
|
||||||
|
|
||||||
|
# Tron Bike Class
|
||||||
|
class tronBike:
|
||||||
|
def __init__(self, number, color, darkColor, side):
|
||||||
|
self.w = w
|
||||||
|
self.h = w
|
||||||
|
self.x = abs(side - 100)
|
||||||
|
self.y = height/2 - self.h
|
||||||
|
self.speed = 10
|
||||||
|
self.color = color
|
||||||
|
self.darkColor = darkColor
|
||||||
|
self.history = [[self.x, self.y]]
|
||||||
|
self.number = number
|
||||||
|
self.length = 1
|
||||||
|
|
||||||
|
# Draw / Show the Bike
|
||||||
|
def draw(self):
|
||||||
|
for i in range(len(self.history)):
|
||||||
|
if i == self.length - 1:
|
||||||
|
pygame.draw.rect(display, self.darkColor, (self.history[i][0], self.history[i][1], self.w, self.h))
|
||||||
|
else:
|
||||||
|
pygame.draw.rect(display, self.color, (self.history[i][0], self.history[i][1], self.w, self.h))
|
||||||
|
|
||||||
|
# Move the Bike
|
||||||
|
def move(self, xdir, ydir):
|
||||||
|
self.x += xdir*self.speed
|
||||||
|
self.y += ydir*self.speed
|
||||||
|
self.history.append([self.x, self.y])
|
||||||
|
self.length += 1
|
||||||
|
if self.x < 0 or self.x > width or self.y < 0 or self.y > height:
|
||||||
|
gameOver(self.number)
|
||||||
|
|
||||||
|
# Check if Bike Collides with Route
|
||||||
|
def checkIfHit(self, opponent):
|
||||||
|
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:
|
||||||
|
gameOver(0)
|
||||||
|
for i in range(opponent.length):
|
||||||
|
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:
|
||||||
|
gameOver(self.number)
|
||||||
|
|
||||||
|
for i in range(len(self.history) - 1):
|
||||||
|
if abs(self.history[i][0] - self.x) < self.w and abs(self.history[i][1] - self.y) < self.h and self.length > 2:
|
||||||
|
gameOver(self.number)
|
||||||
|
|
||||||
|
def gameOver(number):
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_r:
|
||||||
|
tron()
|
||||||
|
if number == 0:
|
||||||
|
text = font.render("Both the Players Collided!", True, white)
|
||||||
|
else:
|
||||||
|
text = font.render("Player %d Lost the Tron!" %(number), True, white)
|
||||||
|
|
||||||
|
display.blit(text, (50, height/2))
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60)
|
||||||
|
|
||||||
|
def drawGrid():
|
||||||
|
squares = 50
|
||||||
|
for i in range(width/squares):
|
||||||
|
pygame.draw.line(display, darkBlue, (i*squares, 0), (i*squares, height))
|
||||||
|
pygame.draw.line(display, darkBlue, (0, i*squares), (width, i*squares))
|
||||||
|
|
||||||
|
def close():
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
|
||||||
|
def tron():
|
||||||
|
loop = True
|
||||||
|
|
||||||
|
bike1 = tronBike(1, red, darkRed, 0)
|
||||||
|
bike2 = tronBike(2, yellow, darkYellow, width)
|
||||||
|
|
||||||
|
x1 = 1
|
||||||
|
y1 = 0
|
||||||
|
x2 = -1
|
||||||
|
y2 = 0
|
||||||
|
|
||||||
|
while loop:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
close()
|
||||||
|
if event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_q:
|
||||||
|
close()
|
||||||
|
if event.key == pygame.K_UP:
|
||||||
|
if not (x2 == 0 and y2 == 1):
|
||||||
|
x2 = 0
|
||||||
|
y2 = -1
|
||||||
|
if event.key == pygame.K_DOWN:
|
||||||
|
if not (x2 == 0 and y2 == -1):
|
||||||
|
x2 = 0
|
||||||
|
y2 = 1
|
||||||
|
if event.key == pygame.K_LEFT:
|
||||||
|
if not (x2 == 1 and y2 == 0):
|
||||||
|
x2 = -1
|
||||||
|
y2 = 0
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
if not (x2 == -1 and y2 == 0):
|
||||||
|
x2 = 1
|
||||||
|
y2 = 0
|
||||||
|
if event.key == pygame.K_w:
|
||||||
|
if not (x1 == 0 and y1 == 1):
|
||||||
|
x1 = 0
|
||||||
|
y1 = -1
|
||||||
|
if event.key == pygame.K_s:
|
||||||
|
if not (x1 == 0 and y1 == -1):
|
||||||
|
x1 = 0
|
||||||
|
y1 = 1
|
||||||
|
if event.key == pygame.K_a:
|
||||||
|
if not (x1 == 1 and y1 == 0):
|
||||||
|
x1 = -1
|
||||||
|
y1 = 0
|
||||||
|
if event.key == pygame.K_d:
|
||||||
|
if not (x1 == -1 and y1 == 0):
|
||||||
|
x1 = 1
|
||||||
|
y1 = 0
|
||||||
|
|
||||||
|
|
||||||
|
display.fill(background)
|
||||||
|
drawGrid()
|
||||||
|
bike1.draw()
|
||||||
|
bike2.draw()
|
||||||
|
|
||||||
|
bike1.move(x1, y1)
|
||||||
|
bike2.move(x2, y2)
|
||||||
|
|
||||||
|
bike1.checkIfHit(bike2)
|
||||||
|
bike2.checkIfHit(bike1)
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(10)
|
||||||
|
|
||||||
|
tron()
|
BIN
space_invaders_2/explosion.wav
Normal file
BIN
space_invaders_2/explosion/1.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
space_invaders_2/explosion/10.png
Normal file
After Width: | Height: | Size: 3.9 KiB |
BIN
space_invaders_2/explosion/11.png
Normal file
After Width: | Height: | Size: 3.0 KiB |
BIN
space_invaders_2/explosion/12.png
Normal file
After Width: | Height: | Size: 1.6 KiB |
BIN
space_invaders_2/explosion/2.png
Normal file
After Width: | Height: | Size: 5.0 KiB |
BIN
space_invaders_2/explosion/3.png
Normal file
After Width: | Height: | Size: 5.8 KiB |
BIN
space_invaders_2/explosion/4.png
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
space_invaders_2/explosion/5.png
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
space_invaders_2/explosion/6.png
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
space_invaders_2/explosion/7.png
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
space_invaders_2/explosion/8.png
Normal file
After Width: | Height: | Size: 4.6 KiB |
BIN
space_invaders_2/explosion/9.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
space_invaders_2/golpe.wav
Normal file
BIN
space_invaders_2/imagenes/A1.png
Normal file
After Width: | Height: | Size: 5.2 KiB |
BIN
space_invaders_2/imagenes/B1.png
Normal file
After Width: | Height: | Size: 721 B |
BIN
space_invaders_2/imagenes/B2.png
Normal file
After Width: | Height: | Size: 841 B |
BIN
space_invaders_2/imagenes/E1.png
Normal file
After Width: | Height: | Size: 2.9 KiB |
BIN
space_invaders_2/imagenes/fondo.png
Normal file
After Width: | Height: | Size: 626 KiB |
BIN
space_invaders_2/laser.wav
Normal file
224
space_invaders_2/main.py
Normal file
@ -0,0 +1,224 @@
|
|||||||
|
# Juego en Python con pygame
|
||||||
|
# autor: Magno Efren - 2022
|
||||||
|
# https://www.youtube.com/c/MagnoEfren/videos
|
||||||
|
import pygame
|
||||||
|
import random
|
||||||
|
|
||||||
|
pygame.init()
|
||||||
|
pygame.mixer.init()
|
||||||
|
|
||||||
|
fondo = pygame.image.load('imagenes/fondo.png')
|
||||||
|
laser_sonido = pygame.mixer.Sound('laser.wav')
|
||||||
|
explosion_sonido = pygame.mixer.Sound('explosion.wav')
|
||||||
|
golpe_sonido = pygame.mixer.Sound('golpe.wav')
|
||||||
|
|
||||||
|
explosion_list = []
|
||||||
|
for i in range(1,13):
|
||||||
|
explosion = pygame.image.load(f'explosion/{i}.png')
|
||||||
|
explosion_list.append(explosion)
|
||||||
|
|
||||||
|
width = fondo.get_width()
|
||||||
|
height = fondo.get_height()
|
||||||
|
window = pygame.display.set_mode((width, height))
|
||||||
|
pygame.display.set_caption('Juego Space Invaders')
|
||||||
|
run = True
|
||||||
|
fps = 60
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
score = 0
|
||||||
|
vida = 100
|
||||||
|
blanco = (255,255,255)
|
||||||
|
negro = (0,0,0)
|
||||||
|
|
||||||
|
def texto_puntuacion(frame, text, size, x,y):
|
||||||
|
font = pygame.font.SysFont('Small Fonts', size, bold=True)
|
||||||
|
text_frame = font.render(text, True, blanco,negro)
|
||||||
|
text_rect = text_frame.get_rect()
|
||||||
|
text_rect.midtop = (x,y)
|
||||||
|
frame.blit(text_frame, text_rect)
|
||||||
|
|
||||||
|
def barra_vida(frame, x,y, nivel):
|
||||||
|
longitud = 100
|
||||||
|
alto = 20
|
||||||
|
fill = int((nivel/100)*longitud)
|
||||||
|
border = pygame.Rect(x,y, longitud, alto)
|
||||||
|
fill = pygame.Rect(x,y,fill, alto)
|
||||||
|
pygame.draw.rect(frame, (255,0,55),fill)
|
||||||
|
pygame.draw.rect(frame, negro, border,4)
|
||||||
|
|
||||||
|
class Jugador(pygame.sprite.Sprite):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.image = pygame.image.load('imagenes/A1.png').convert_alpha()
|
||||||
|
pygame.display.set_icon(self.image)
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
self.rect.centerx = width//2
|
||||||
|
self.rect.centery = height-50
|
||||||
|
self.velocidad_x = 0
|
||||||
|
self.vida = 100
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.velocidad_x = 0
|
||||||
|
keystate = pygame.key.get_pressed()
|
||||||
|
if keystate[pygame.K_LEFT]:
|
||||||
|
self.velocidad_x = -5
|
||||||
|
elif keystate[pygame.K_RIGHT]:
|
||||||
|
self.velocidad_x = 5
|
||||||
|
|
||||||
|
self.rect.x += self.velocidad_x
|
||||||
|
if self.rect.right > width:
|
||||||
|
self.rect.right = width
|
||||||
|
elif self.rect.left < 0:
|
||||||
|
self.rect.left = 0
|
||||||
|
|
||||||
|
def disparar(self):
|
||||||
|
bala = Balas(self.rect.centerx, self.rect.top)
|
||||||
|
grupo_jugador.add(bala)
|
||||||
|
grupo_balas_jugador.add(bala)
|
||||||
|
laser_sonido.play()
|
||||||
|
|
||||||
|
class Enemigos(pygame.sprite.Sprite):
|
||||||
|
def __init__(self, x, y):
|
||||||
|
super().__init__()
|
||||||
|
self.image = pygame.image.load('imagenes/E1.png').convert_alpha()
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
self.rect.x = random.randrange(1, width-50)
|
||||||
|
self.rect.y = 10
|
||||||
|
self.velocidad_y = random.randrange(-5,20)
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.time = random.randrange(-1, pygame.time.get_ticks()//5000)
|
||||||
|
self.rect.x += self.time
|
||||||
|
if self.rect.x >= width:
|
||||||
|
self.rect.x = 0
|
||||||
|
self.rect.y += 50
|
||||||
|
|
||||||
|
def disparar_enemigos(self):
|
||||||
|
bala = Balas_enemigos(self.rect.centerx, self.rect.bottom)
|
||||||
|
grupo_jugador.add(bala)
|
||||||
|
grupo_balas_enemigos.add(bala)
|
||||||
|
laser_sonido.play()
|
||||||
|
|
||||||
|
class Balas(pygame.sprite.Sprite):
|
||||||
|
def __init__(self, x, y):
|
||||||
|
super().__init__()
|
||||||
|
self.image = pygame.image.load('imagenes/B2.png').convert_alpha()
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
self.rect.centerx = x
|
||||||
|
self.rect.y = y
|
||||||
|
self.velocidad = -18
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.rect.y += self.velocidad
|
||||||
|
if self.rect.bottom <0:
|
||||||
|
self.kill()
|
||||||
|
|
||||||
|
class Balas_enemigos(pygame.sprite.Sprite):
|
||||||
|
def __init__(self, x, y):
|
||||||
|
super().__init__()
|
||||||
|
self.image = pygame.image.load('imagenes/B1.png').convert_alpha()
|
||||||
|
self.image = pygame.transform.rotate(self.image, 180)
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
self.rect.centerx = x
|
||||||
|
self.rect.y = random.randrange(10, width)
|
||||||
|
self.velocidad_y = 4
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
self.rect.y += self.velocidad_y
|
||||||
|
if self.rect.bottom > height:
|
||||||
|
self.kill()
|
||||||
|
|
||||||
|
class Explosion(pygame.sprite.Sprite):
|
||||||
|
def __init__(self, position):
|
||||||
|
super().__init__()
|
||||||
|
self.image = explosion_list[0]
|
||||||
|
img_scala = pygame.transform.scale(self.image, (20,20))
|
||||||
|
self.rect = img_scala.get_rect()
|
||||||
|
self.rect.center = position
|
||||||
|
self.time = pygame.time.get_ticks()
|
||||||
|
self.velocidad_explo = 30
|
||||||
|
self.frames = 0
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
tiempo = pygame.time.get_ticks()
|
||||||
|
if tiempo - self.time > self.velocidad_explo:
|
||||||
|
self.time = tiempo
|
||||||
|
self.frames+=1
|
||||||
|
if self.frames == len(explosion_list):
|
||||||
|
self.kill()
|
||||||
|
else:
|
||||||
|
position = self.rect.center
|
||||||
|
self.image = explosion_list[self.frames]
|
||||||
|
self.rect = self.image.get_rect()
|
||||||
|
self.rect.center = position
|
||||||
|
|
||||||
|
grupo_jugador = pygame.sprite.Group()
|
||||||
|
grupo_enemigos = pygame.sprite.Group()
|
||||||
|
grupo_balas_jugador = pygame.sprite.Group()
|
||||||
|
grupo_balas_enemigos = pygame.sprite.Group()
|
||||||
|
|
||||||
|
player = Jugador()
|
||||||
|
grupo_jugador.add(player)
|
||||||
|
grupo_balas_jugador.add(player)
|
||||||
|
|
||||||
|
for x in range(10):
|
||||||
|
enemigo = Enemigos(10,10)
|
||||||
|
grupo_enemigos.add(enemigo)
|
||||||
|
grupo_jugador.add(enemigo)
|
||||||
|
|
||||||
|
while run:
|
||||||
|
clock.tick(fps)
|
||||||
|
window.blit(fondo, (0,0))
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
run = False
|
||||||
|
elif event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_SPACE:
|
||||||
|
player.disparar()
|
||||||
|
|
||||||
|
grupo_jugador.update()
|
||||||
|
grupo_enemigos.update()
|
||||||
|
grupo_balas_jugador.update()
|
||||||
|
grupo_balas_enemigos.update()
|
||||||
|
|
||||||
|
grupo_jugador.draw(window)
|
||||||
|
|
||||||
|
# Coliciones balas_jugador - enemigo
|
||||||
|
colicion1 = pygame.sprite.groupcollide(grupo_enemigos, grupo_balas_jugador,True,True)
|
||||||
|
for i in colicion1:
|
||||||
|
score+=10
|
||||||
|
enemigo.disparar_enemigos()
|
||||||
|
enemigo = Enemigos(300,10)
|
||||||
|
grupo_enemigos.add(enemigo)
|
||||||
|
grupo_jugador.add(enemigo)
|
||||||
|
|
||||||
|
explo = Explosion(i.rect.center)
|
||||||
|
grupo_jugador.add(explo)
|
||||||
|
explosion_sonido.set_volume(0.3)
|
||||||
|
explosion_sonido.play()
|
||||||
|
|
||||||
|
# Coliciones jugador - balas_enemigo
|
||||||
|
colicion2 = pygame.sprite.spritecollide(player, grupo_balas_enemigos, True)
|
||||||
|
for j in colicion2:
|
||||||
|
player.vida -= 10
|
||||||
|
if player.vida <=0:
|
||||||
|
run = False
|
||||||
|
explo1 = Explosion(j.rect.center)
|
||||||
|
grupo_jugador.add(explo1)
|
||||||
|
golpe_sonido.play()
|
||||||
|
|
||||||
|
# Coliciones jugador - enemigo
|
||||||
|
hits =pygame.sprite.spritecollide(player, grupo_enemigos , False)
|
||||||
|
for hit in hits:
|
||||||
|
player.vida -= 100
|
||||||
|
enemigos = Enemigos(10,10)
|
||||||
|
grupo_jugador.add(enemigos)
|
||||||
|
grupo_enemigos.add(enemigos)
|
||||||
|
if player.vida <=0:
|
||||||
|
run = False
|
||||||
|
# Indicador y Score
|
||||||
|
texto_puntuacion(window, (' SCORE: '+ str(score)+' '), 30, width-85, 2)
|
||||||
|
barra_vida(window, width-285, 0, player.vida)
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
pygame.quit()
|