Tank Game Python with Source Code

Tank Game Python with Source Code

Tank Game project is created using Python Programming Language. The Tank Game Python with Source Code used a Pygame and Random module. Pygame is a cross-platform set of Python modules designed for writing video games. This is the game between computer and user. This simple Tank Game Python is a desktop application.

This Tank Game Python has the usage of the UP and DOWN keys you may manage the direction of your cannon. Press LEFT-CTRL or LEFT-ALT to change the energy stage of the shoot. The SPACE key will release the cannonball.

The LEFT and RIGHT keys assist you to transport your tank to the left and right. The primary goal of this game is to shoot and destroy the enemies tank.

Anyway if you want level up your knowledge in programming especially games in python, try this new article I’ve made for you Code For Game in Python: Python Game Projects With Source Code

Tank Game Python with Source Code 2...
Tank Game Python with Source Code 2020 Free Download

Before you start on how to create Tank Game Python, make sure that you have PyCharm IDE and Pygame installed in your computer.

By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you. I also have here How to Download and Install Latest Version of Python on Windows.

This are the steps on How to Create Tank Game Python.

Tank Game Python with Source Code

  • Step 1: Create a project name.

    First when you finished installed the Pycharm IDE in your computer, open it and then create a “project name” after creating a project name click the “create” button.Creating Project Name in Tank Game in python with Source Code

  • Step 2: Create a python file.

    Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.Creating Python File Name in Tank Game in python with Source Code

  • Step 3: Name your python file.

    Third after creating a python file, Name your python file after that click “enter“.Naming a Python File Name in Tank Game in python with Source Code

  • Step 4: The actual code.

    This is the actual coding on how to create Tank Game Python, and you are free to copy this code and download the full source code given below.

Importing Pygame Module

In the code below, which is pygame library is an open-source module for the Python programming language specifically intended to help you make games and other multimedia applications. Pygame can run across many platforms and operating systems.(Python Source Code for Tank Game)

import pygame

Importing Random Module

In the code below. which is for the random() function, which generates random numbers between 0 and 1.(Python Source Code for Tank Game)

import random

This module is for the design main window

In the code given below, which is for the design of main screen window colors, title of the game and the background image.

display_width = 800
display_height = 600

game_layout_display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Tanks Game - Brought To You By Itsourcecode.com')

Resources = pygame.image.load("resources/game_background.png")
pygame.display.set_icon(Resources)

# colors
wheat = (245, 222, 179)

white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)

red = (200, 0, 0)
light_red = (255, 0, 0)

yellow = (200, 200, 0)
light_yellow = (255, 255, 0)

green = (34, 177, 76)
light_green = (0, 255, 0)

This module is for the geometry of tank

In the code given below, which is for the geometry of tank width and height.

tnk_width = 40
tnk_height = 20

tur_width = 5
whl_width = 5

This module is for the font size

In the code given below, which is for the font size use such as small, medium, large, and very small.

s_font = pygame.font.SysFont("Times New Roman", 25)
m_font = pygame.font.SysFont("Times New Roman", 50)
l_font = pygame.font.SysFont("Times New Roman", 85)
vs_font = pygame.font.SysFont("Times New Roman", 25)

This module is for the score

In the code given below, which is for the defining score function.

def Score(Score):
txt = s_font.render("Score: " + str(Score), True, white)
game_layout_display.blit(txt, [0, 0])

This module is for the player tank

In the code given below, which is for the function of player tank, defining turrets position and wheels dimension.(Python Source Code for Tank Game)

def tank(x, y, turret_position):
x = int(x)
y = int(y)

pos_Turrets = [(x - 27, y - 2),
(x - 26, y - 5),
(x - 25, y - 8),
(x - 23, y - 12),
(x - 20, y - 14),
(x - 18, y - 15),
(x - 15, y - 17),
(x - 13, y - 19),
(x - 11, y - 21)
]

pygame.draw.circle(game_layout_display, blue, (x, y), int(tnk_height / 2))
pygame.draw.rect(game_layout_display, blue, (x - tnk_height, y, tnk_width, tnk_height))

pygame.draw.line(game_layout_display, blue, (x, y), pos_Turrets[turret_position], tur_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 15, y + 20), whl_width)

return pos_Turrets[turret_position]

This module is for the computer tank

In the code given below, which is for the function for computer tank , defining turrets position and wheel dimension.

def computer_tank(x, y, turret_position):
x = int(x)
y = int(y)

pos_Turrets = [(x + 27, y - 2),
(x + 26, y - 5),
(x + 25, y - 8),
(x + 23, y - 12),
(x + 20, y - 14),
(x + 18, y - 15),
(x + 15, y - 17),
(x + 13, y - 19),
(x + 11, y - 21)
]

pygame.draw.circle(game_layout_display, blue, (x, y), int(tnk_height / 2))
pygame.draw.rect(game_layout_display, blue, (x - tnk_height, y, tnk_width, tnk_height))

pygame.draw.line(game_layout_display, blue, (x, y), pos_Turrets[turret_position], tur_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 15, y + 20), whl_width)

return pos_Turrets[turret_position]

This module is for the game control

In the code given below, which is for the function of game control screen.

def game_ctrls():
gameControl = True

while gameControl:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

game_layout_display.fill(black)
msg_screen("Controls", white, -100, size="large")
msg_screen("Fire: Spacebar", wheat, -30)
msg_screen("Move Turret: Up and Down arrows", wheat, 10)
msg_screen("Move Tank: Left and Right arrows", wheat, 50)
msg_screen("Press D to raise Power % AND Press A to lower Power % ", wheat, 140)
msg_screen("Pause: P", wheat, 90)

btn("Play", 150, 500, 100, 50, green, light_green, action="play")
btn("Main", 350, 500, 100, 50, yellow, light_yellow, action="main")
btn("Quit", 550, 500, 100, 50, red, light_red, action="quit")

pygame.display.update()

clock.tick(15)

This module is for the buttons

In the code given below, which is for the function of buttons having action calls and text.

def btn(txt, x, y, width, height, inactive_color, active_color, action=None,size=" "):
cursor = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
# print(click)
if x + width > cursor[0] > x and y + height > cursor[1] > y:
pygame.draw.rect(game_layout_display, active_color, (x, y, width, height))
if click[0] == 1 and action != None:
if action == "quit":
pygame.quit()
quit()

if action == "controls":
game_ctrls()

if action == "play":
gameLoop()

if action == "main":
game_intro()

else:
pygame.draw.rect(game_layout_display, inactive_color, (x, y, width, height))

txt_btn(txt, black, x, y, width, height)

This module is for the explosion of tank

In the code given below, which is for the function for explosion of both tanks.

def explosion(x, y, size=50):

exp = True

while exp:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

startPoint = x, y

choice_colors = [red, light_red, yellow, light_yellow]

mgntde = 1

while mgntde < size:
exploding_bit_x = x + random.randrange(-1 * mgntde, mgntde)
exploding_bit_y = y + random.randrange(-1 * mgntde, mgntde)

pygame.draw.circle(game_layout_display, choice_colors[random.randrange(0, 4)], (exploding_bit_x, exploding_bit_y),
random.randrange(1, 5))
mgntde += 1

pygame.display.update()
clock.tick(100)

exp = False

This module is for the firing for player tank

In the code given below, which is for the function of firing for player tank.

def playerfireShell(xy, tankx, tanky, turPost, gun_power, xloc, bar_width, ranHeight, eTankX, eTankY):

fire = True
damage = 0

startShell = list(xy)

print("FIRE!", xy)

while fire:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

# print(startingShell[0],startingShell[1])
pygame.draw.circle(game_layout_display, red, (startShell[0], startShell[1]), 5)

startShell[0] -= (12 - turPost) * 2

# y = x**2
startShell[1] += int(
(((startShell[0] - xy[0]) * 0.015 / (gun_power / 50)) ** 2) - (turPost + turPost / (12 - turPost)))

if startShell[1] > display_height - grnd_height:
print("Last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0] * display_height - grnd_height) / startShell[1])
hit_y = int(display_height - grnd_height)
print("Impact:", hit_x, hit_y)

if eTankX + 10 > hit_x > eTankX - 10:
print("Critical Hit!")
damage = 25
elif eTankX + 15 > hit_x > eTankX - 15:
print("Hard Hit!")
damage = 18
elif eTankX + 25 > hit_x > eTankX - 25:
print("Medium Hit")
damage = 10
elif eTankX + 35 > hit_x > eTankX - 35:
print("Light Hit")
damage = 5

explosion(hit_x, hit_y)
fire = False

check_x_1 = startShell[0] <= xloc + bar_width
check_x_2 = startShell[0] >= xloc

check_y_1 = startShell[1] <= display_height
check_y_2 = startShell[1] >= display_height - ranHeight

if check_x_1 and check_x_2 and check_y_1 and check_y_2:
print("Last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0]))
hit_y = int(startShell[1])
print("Impact:", hit_x, hit_y)
explosion(hit_x, hit_y)
fire = False

pygame.display.update()
clock.tick(60)
return damage

This module is for the firing for computer tank

In the code given below, which is for the function of firing for computer tank.

def computerfireShell(xy, tankx, tanky, turPost, gun_power, xloc, bar_width, ranHeight, ptankx, ptanky):

damage = 0
cPower = 1
pow_found = False

while not pow_found:
cPower += 1
if cPower > 100:
pow_found = True
# print(currentPower)

fire = True
startShell = list(xy)

while fire:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

# pygame.draw.circle(gameDisplay, red, (startingShell[0],startingShell[1]),5)

startShell[0] += (12 - turPost) * 2
startShell[1] += int(
(((startShell[0] - xy[0]) * 0.015 / (cPower / 50)) ** 2) - (turPost + turPost / (12 - turPost)))

if startShell[1] > display_height - grnd_height:
hit_x = int((startShell[0] * display_height - grnd_height) / startShell[1])
hit_y = int(display_height - grnd_height)
# explosion(hit_x,hit_y)
if ptankx + 15 > hit_x > ptankx - 15:
print("target acquired!")
power_found = True
fire = False

check_x_1 = startShell[0] <= xloc + bar_width
check_x_2 = startShell[0] >= xloc

check_y_1 = startShell[1] <= display_height
check_y_2 = startShell[1] >= display_height - ranHeight

if check_x_1 and check_x_2 and check_y_1 and check_y_2:
hit_x = int((startShell[0]))
hit_y = int(startShell[1])
# explosion(hit_x,hit_y)
fire = False

fire = True
startShell = list(xy)
print("FIRE!", xy)

while fire:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

pygame.draw.circle(game_layout_display, red, (startShell[0], startShell[1]), 5)

startShell[0] += (12 - turPost) * 2



gun_power = random.randrange(int(cPower * 0.90), int(cPower * 1.10))

startShell[1] += int(
(((startShell[0] - xy[0]) * 0.015 / (gun_power / 50)) ** 2) - (turPost + turPost / (12 - turPost)))

if startShell[1] > display_height - grnd_height:
print("last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0] * display_height - grnd_height) / startShell[1])
hit_y = int(display_height - grnd_height)
print("Impact:", hit_x, hit_y)

if ptankx + 10 > hit_x > ptankx - 10:
print("Critical Hit!")
damage = 25
elif ptankx + 15 > hit_x > ptankx - 15:
print("Hard Hit!")
damage = 18
elif ptankx + 25 > hit_x > ptankx - 25:
print("Medium Hit")
damage = 10
elif ptankx + 35 > hit_x > ptankx - 35:
print("Light Hit")
damage = 5

explosion(hit_x, hit_y)
fire = False

check_x_1 = startShell[0] <= xloc+ bar_width
check_x_2 = startShell[0] >= xloc

check_y_1 = startShell[1] <= display_height
check_y_2 = startShell[1] >= display_height - ranHeight

if check_x_1 and check_x_2 and check_y_1 and check_y_2:
print("Last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0]))
hit_y = int(startShell[1])
print("Impact:", hit_x, hit_y)
explosion(hit_x, hit_y)
fire = False

pygame.display.update()
clock.tick(60)
return damage

This module is for the game over screen

In the code given below, which is for the function of game over screen.

def game_over():
game_over = True

while game_over:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

game_layout_display.fill(black)
msg_screen("Game Over", white, -100, size="large")
msg_screen("You died.", wheat, -30)

btn("Play Again", 150, 500, 150, 50, wheat, light_green, action="play")
btn("Controls", 350, 500, 100, 50, wheat, light_yellow, action="controls")
btn("Quit", 550, 500, 100, 50, wheat, light_red, action="quit")

pygame.display.update()

clock.tick(15)

This module is for the player winner screen

In the code given below, which is for the function of player winner screen

def you_win():
win = True

while win:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

game_layout_display.fill(black)
msg_screen("You won!", white, -100, size="large")
msg_screen("Congratulations!", wheat, -30)

btn("play Again", 150, 500, 150, 50, wheat, light_green, action="play")
btn("controls", 350, 500, 100, 50, wheat, light_yellow, action="controls")
btn("quit", 550, 500, 100, 50, wheat, light_red, action="quit")

pygame.display.update()

clock.tick(15)

This module is for the health bars

In the code given below ,which is for the function for health bars of player tank and computer tank.

def health_bars(p_health, e_health):
if p_health > 75:
p_health_color = green
elif p_health > 50:
p_health_color = yellow
else:
p_health_color = red

if e_health > 75:
e_health_color = green
elif e_health > 50:
e_health_color = yellow
else:
e_health_color = red

pygame.draw.rect(game_layout_display, p_health_color, (680, 25, p_health, 25))
pygame.draw.rect(game_layout_display, e_health_color, (20, 25, e_health, 25))

Complete Source Code of Tank Game Python

import pygame
import random

pygame.init()

display_width = 800
display_height = 600

game_layout_display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Tanks Game - Brought To You By Itsourcecode.com')

Resources = pygame.image.load("resources/game_background.png")
pygame.display.set_icon(Resources)

# colors
wheat = (245, 222, 179)

white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)

red = (200, 0, 0)
light_red = (255, 0, 0)

yellow = (200, 200, 0)
light_yellow = (255, 255, 0)

green = (34, 177, 76)
light_green = (0, 255, 0)

# for picking current time for the frames per second
clock = pygame.time.Clock()
# geometry of tank and its turret
tnk_width = 40
tnk_height = 20

tur_width = 5
whl_width = 5

grnd_height = 35
# fonts with size, for text_object function
s_font = pygame.font.SysFont("Times New Roman", 25)
m_font = pygame.font.SysFont("Times New Roman", 50)
l_font = pygame.font.SysFont("Times New Roman", 85)
vs_font = pygame.font.SysFont("Times New Roman", 25)

# defining score function
def Score(Score):
txt = s_font.render("Score: " + str(Score), True, white)
game_layout_display.blit(txt, [0, 0])

# defining function to get the fonts and sizes assigned with them by size names by default size="small"
def txt_object(txt, color, size="small"):
if size == "small":
txtSrfc = s_font.render(txt, True, color)
if size == "medium":
txtSrfc = m_font.render(txt, True, color)
if size == "large":
txtSrfc = l_font.render(txt, True, color)
if size == "vsmall":
txtSrfc = vs_font.render(txt, True, color)

return txtSrfc, txtSrfc.get_rect()

# function for texts that has to appear over button
def txt_btn(message, color, btnx, btny, btnwidth, btnheight, size="vsmall"):
txtSrf, textRect = txt_object(message, color, size)
textRect.center = ((btnx + (btnwidth / 2)), btny + (btnheight / 2))
game_layout_display.blit(txtSrf, textRect)

# function for texts that has to appear over screen
def msg_screen(message, color, y_displace=0, size="small"):
txtSrf, textRect = txt_object(message, color, size)
textRect.center = (int(display_width / 2), int(display_height / 2) + y_displace)
game_layout_display.blit(txtSrf, textRect)

# function for players tank , defining turrets positions and wheels dimensions
def tank(x, y, turret_position):
x = int(x)
y = int(y)

pos_Turrets = [(x - 27, y - 2),
(x - 26, y - 5),
(x - 25, y - 8),
(x - 23, y - 12),
(x - 20, y - 14),
(x - 18, y - 15),
(x - 15, y - 17),
(x - 13, y - 19),
(x - 11, y - 21)
]

pygame.draw.circle(game_layout_display, blue, (x, y), int(tnk_height / 2))
pygame.draw.rect(game_layout_display, blue, (x - tnk_height, y, tnk_width, tnk_height))

pygame.draw.line(game_layout_display, blue, (x, y), pos_Turrets[turret_position], tur_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 15, y + 20), whl_width)

return pos_Turrets[turret_position]

# function for computers tank , defining turrets positions and wheels dimensions
def computer_tank(x, y, turret_position):
x = int(x)
y = int(y)

pos_Turrets = [(x + 27, y - 2),
(x + 26, y - 5),
(x + 25, y - 8),
(x + 23, y - 12),
(x + 20, y - 14),
(x + 18, y - 15),
(x + 15, y - 17),
(x + 13, y - 19),
(x + 11, y - 21)
]

pygame.draw.circle(game_layout_display, blue, (x, y), int(tnk_height / 2))
pygame.draw.rect(game_layout_display, blue, (x - tnk_height, y, tnk_width, tnk_height))

pygame.draw.line(game_layout_display, blue, (x, y), pos_Turrets[turret_position], tur_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)

pygame.draw.circle(game_layout_display, blue, (x - 15, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x - 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 5, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 10, y + 20), whl_width)
pygame.draw.circle(game_layout_display, blue, (x + 15, y + 20), whl_width)

return pos_Turrets[turret_position]

# Game control Screen
def game_ctrls():
gameControl = True

while gameControl:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

game_layout_display.fill(black)
msg_screen("Controls", white, -100, size="large")
msg_screen("Fire: Spacebar", wheat, -30)
msg_screen("Move Turret: Up and Down arrows", wheat, 10)
msg_screen("Move Tank: Left and Right arrows", wheat, 50)
msg_screen("Press D to raise Power % AND Press A to lower Power % ", wheat, 140)
msg_screen("Pause: P", wheat, 90)

btn("Play", 150, 500, 100, 50, green, light_green, action="play")
btn("Main", 350, 500, 100, 50, yellow, light_yellow, action="main")
btn("Quit", 550, 500, 100, 50, red, light_red, action="quit")

pygame.display.update()

clock.tick(15)

# function for buttons having action calls and text on it callings
def btn(txt, x, y, width, height, inactive_color, active_color, action=None,size=" "):
cursor = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
# print(click)
if x + width > cursor[0] > x and y + height > cursor[1] > y:
pygame.draw.rect(game_layout_display, active_color, (x, y, width, height))
if click[0] == 1 and action != None:
if action == "quit":
pygame.quit()
quit()

if action == "controls":
game_ctrls()

if action == "play":
gameLoop()

if action == "main":
game_intro()

else:
pygame.draw.rect(game_layout_display, inactive_color, (x, y, width, height))

txt_btn(txt, black, x, y, width, height)

# function for pause having transparent background, uncommenting fill statement will make it black
def pause():
paused = True
msg_screen("Paused", white, -100, size="large")
msg_screen("Press C to continue playing or Q to quit", wheat, 25)
pygame.display.update()
while paused:
# gameDisplay.fill(black)
for event in pygame.event.get():

if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
paused = False
elif event.key == pygame.K_q:
pygame.quit()
quit()

clock.tick(5)

# function for barrier
def barrier(x_loc, ran_height, bar_width):
pygame.draw.rect(game_layout_display, green, [x_loc, display_height - ran_height, bar_width, ran_height])

# function for explosion for both tanks
def explosion(x, y, size=50):

exp = True

while exp:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

startPoint = x, y

choice_colors = [red, light_red, yellow, light_yellow]

mgntde = 1

while mgntde < size:
exploding_bit_x = x + random.randrange(-1 * mgntde, mgntde)
exploding_bit_y = y + random.randrange(-1 * mgntde, mgntde)

pygame.draw.circle(game_layout_display, choice_colors[random.randrange(0, 4)], (exploding_bit_x, exploding_bit_y),
random.randrange(1, 5))
mgntde += 1

pygame.display.update()
clock.tick(100)

exp = False

# firing function for players tank
def playerfireShell(xy, tankx, tanky, turPost, gun_power, xloc, bar_width, ranHeight, eTankX, eTankY):

fire = True
damage = 0

startShell = list(xy)

print("FIRE!", xy)

while fire:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

# print(startingShell[0],startingShell[1])
pygame.draw.circle(game_layout_display, red, (startShell[0], startShell[1]), 5)

startShell[0] -= (12 - turPost) * 2

# y = x**2
startShell[1] += int(
(((startShell[0] - xy[0]) * 0.015 / (gun_power / 50)) ** 2) - (turPost + turPost / (12 - turPost)))

if startShell[1] > display_height - grnd_height:
print("Last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0] * display_height - grnd_height) / startShell[1])
hit_y = int(display_height - grnd_height)
print("Impact:", hit_x, hit_y)

if eTankX + 10 > hit_x > eTankX - 10:
print("Critical Hit!")
damage = 25
elif eTankX + 15 > hit_x > eTankX - 15:
print("Hard Hit!")
damage = 18
elif eTankX + 25 > hit_x > eTankX - 25:
print("Medium Hit")
damage = 10
elif eTankX + 35 > hit_x > eTankX - 35:
print("Light Hit")
damage = 5

explosion(hit_x, hit_y)
fire = False

check_x_1 = startShell[0] <= xloc + bar_width
check_x_2 = startShell[0] >= xloc

check_y_1 = startShell[1] <= display_height
check_y_2 = startShell[1] >= display_height - ranHeight

if check_x_1 and check_x_2 and check_y_1 and check_y_2:
print("Last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0]))
hit_y = int(startShell[1])
print("Impact:", hit_x, hit_y)
explosion(hit_x, hit_y)
fire = False

pygame.display.update()
clock.tick(60)
return damage

# firing function for computer's tank
def computerfireShell(xy, tankx, tanky, turPost, gun_power, xloc, bar_width, ranHeight, ptankx, ptanky):

damage = 0
cPower = 1
pow_found = False

while not pow_found:
cPower += 1
if cPower > 100:
pow_found = True
# print(currentPower)

fire = True
startShell = list(xy)

while fire:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

# pygame.draw.circle(gameDisplay, red, (startingShell[0],startingShell[1]),5)

startShell[0] += (12 - turPost) * 2
startShell[1] += int(
(((startShell[0] - xy[0]) * 0.015 / (cPower / 50)) ** 2) - (turPost + turPost / (12 - turPost)))

if startShell[1] > display_height - grnd_height:
hit_x = int((startShell[0] * display_height - grnd_height) / startShell[1])
hit_y = int(display_height - grnd_height)
# explosion(hit_x,hit_y)
if ptankx + 15 > hit_x > ptankx - 15:
print("target acquired!")
power_found = True
fire = False

check_x_1 = startShell[0] <= xloc + bar_width
check_x_2 = startShell[0] >= xloc

check_y_1 = startShell[1] <= display_height
check_y_2 = startShell[1] >= display_height - ranHeight

if check_x_1 and check_x_2 and check_y_1 and check_y_2:
hit_x = int((startShell[0]))
hit_y = int(startShell[1])
# explosion(hit_x,hit_y)
fire = False

fire = True
startShell = list(xy)
print("FIRE!", xy)

while fire:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()

pygame.draw.circle(game_layout_display, red, (startShell[0], startShell[1]), 5)

startShell[0] += (12 - turPost) * 2



gun_power = random.randrange(int(cPower * 0.90), int(cPower * 1.10))

startShell[1] += int(
(((startShell[0] - xy[0]) * 0.015 / (gun_power / 50)) ** 2) - (turPost + turPost / (12 - turPost)))

if startShell[1] > display_height - grnd_height:
print("last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0] * display_height - grnd_height) / startShell[1])
hit_y = int(display_height - grnd_height)
print("Impact:", hit_x, hit_y)

if ptankx + 10 > hit_x > ptankx - 10:
print("Critical Hit!")
damage = 25
elif ptankx + 15 > hit_x > ptankx - 15:
print("Hard Hit!")
damage = 18
elif ptankx + 25 > hit_x > ptankx - 25:
print("Medium Hit")
damage = 10
elif ptankx + 35 > hit_x > ptankx - 35:
print("Light Hit")
damage = 5

explosion(hit_x, hit_y)
fire = False

check_x_1 = startShell[0] <= xloc+ bar_width
check_x_2 = startShell[0] >= xloc

check_y_1 = startShell[1] <= display_height
check_y_2 = startShell[1] >= display_height - ranHeight

if check_x_1 and check_x_2 and check_y_1 and check_y_2:
print("Last shell:", startShell[0], startShell[1])
hit_x = int((startShell[0]))
hit_y = int(startShell[1])
print("Impact:", hit_x, hit_y)
explosion(hit_x, hit_y)
fire = False

pygame.display.update()
clock.tick(60)
return damage

# function for power level of players tank
def power(level):
text = s_font.render("Power: " + str(level) + "%", True, wheat)
game_layout_display.blit(text, [display_width / 2, 0])

# function for intro screen
def game_intro():
intro = True

while intro:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
intro = False
elif event.key == pygame.K_q:

pygame.quit()
quit()

game_layout_display.fill(black)
msg_screen("Welcome to Tanks War!", white, -100, size="Medium")
msg_screen("The goal is to shoot and destroy", wheat, 15)
msg_screen("the enemy tank before they destroy you.", wheat, 60)
msg_screen("The more enemies you destroy, the highest score you get.", wheat, 110)
msg_screen("Brought To You by :itsourcecode.com", wheat, 280)
# message_to_screen("Press C to play, P to pause or Q to quit",black,180)


btn("Play", 150, 500, 100, 50, wheat, light_green, action="play",size="vsmall")
btn("Controls", 350, 500, 100, 50, wheat, light_yellow, action="controls",size="vsmall")
btn("Quit", 550, 500, 100, 50, wheat, light_red, action="quit",size="vsmall")

pygame.display.update()

clock.tick(15)

# function for game Over screen
def game_over():
game_over = True

while game_over:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

game_layout_display.fill(black)
msg_screen("Game Over", white, -100, size="large")
msg_screen("You died.", wheat, -30)

btn("Play Again", 150, 500, 150, 50, wheat, light_green, action="play")
btn("Controls", 350, 500, 100, 50, wheat, light_yellow, action="controls")
btn("Quit", 550, 500, 100, 50, wheat, light_red, action="quit")

pygame.display.update()

clock.tick(15)

# function for players win screen
def you_win():
win = True

while win:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()

game_layout_display.fill(black)
msg_screen("You won!", white, -100, size="large")
msg_screen("Congratulations!", wheat, -30)

btn("play Again", 150, 500, 150, 50, wheat, light_green, action="play")
btn("controls", 350, 500, 100, 50, wheat, light_yellow, action="controls")
btn("quit", 550, 500, 100, 50, wheat, light_red, action="quit")

pygame.display.update()

clock.tick(15)

# function for health bars of both tanks
def health_bars(p_health, e_health):
if p_health > 75:
p_health_color = green
elif p_health > 50:
p_health_color = yellow
else:
p_health_color = red

if e_health > 75:
e_health_color = green
elif e_health > 50:
e_health_color = yellow
else:
e_health_color = red

pygame.draw.rect(game_layout_display, p_health_color, (680, 25, p_health, 25))
pygame.draw.rect(game_layout_display, e_health_color, (20, 25, e_health, 25))

# function for main gameloop
def gameLoop():
gExit = False
gOver = False
FPS = 15

p_health = 100
e_health = 100

bar_width = 50

mTankX = display_width * 0.9
mTankY = display_height * 0.9
tnkMove = 0
curTurPost = 0
changeTurs = 0

eTankX = display_width * 0.1
eTankY = display_height * 0.9

f_power = 50
p_change = 0

xloc = (display_width / 2) + random.randint(-0.1 * display_width, 0.1 * display_width)
ranHeight = random.randrange(display_height * 0.1, display_height * 0.6)

while not gExit:

if gOver == True:
# gameDisplay.fill(white)
msg_screen("Game Over", red, -50, size="large")
msg_screen("Press C to play again or Q to exit", black, 50)
pygame.display.update()
while gOver == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gExit = True
gOver = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
gameLoop()
elif event.key == pygame.K_q:

gExit = True
gOver = False

for event in pygame.event.get():

if event.type == pygame.QUIT:
gExit = True

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
tnkMove = -5

elif event.key == pygame.K_RIGHT:
tnkMove = 5

elif event.key == pygame.K_UP:
changeTurs = 1

elif event.key == pygame.K_DOWN:
changeTurs= -1

elif event.key == pygame.K_p:
pause()

elif event.key == pygame.K_SPACE:

damage = playerfireShell(gun, mTankX, mTankY, curTurPost, f_power, xloc, bar_width,
ranHeight, eTankX, eTankY)
e_health -= damage

posMovement = ['f', 'r']
moveInd = random.randrange(0, 2)

for x in range(random.randrange(0, 10)):

if display_width * 0.3 > eTankX > display_width * 0.03:
if posMovement[moveInd] == "f":
eTankX += 5
elif posMovement[moveInd] == "r":
eTankX -= 5

game_layout_display.fill(black)
health_bars(p_health, e_health)
gun = tank(mTankX, mTankY, curTurPost)
e_gun = computer_tank(eTankX, eTankY, 8)
f_power += p_change

power(f_power)

barrier(xloc, ranHeight, bar_width)
game_layout_display.fill(green,
rect=[0, display_height - grnd_height, display_width, grnd_height])
pygame.display.update()

clock.tick(FPS)

damage = computerfireShell(e_gun, eTankX, eTankY, 8, 50, xloc, bar_width,
ranHeight, mTankX, mTankY)
p_health -= damage

elif event.key == pygame.K_a:
p_change = -1
elif event.key == pygame.K_d:
p_change = 1

elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
tnkMove = 0

if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
changeTurs = 0

if event.key == pygame.K_a or event.key == pygame.K_d:
p_change = 0

mTankX += tnkMove

curTurPost += changeTurs

if curTurPost > 8:
curTurPost = 8
elif curTurPost < 0:
curTurPost = 0

if mTankX - (tnk_width / 2) < xloc + bar_width:
mTankX += 5

game_layout_display.fill(black)
health_bars(p_health, e_health)
gun = tank(mTankX, mTankY, curTurPost)
e_gun = computer_tank(eTankX, eTankY, 8)

f_power += p_change

if f_power > 100:
f_power = 100
elif f_power < 1:
f_power = 1

power(f_power)

barrier(xloc, ranHeight, bar_width)
game_layout_display.fill(green, rect=[0, display_height - grnd_height, display_width, grnd_height])
pygame.display.update()

if p_health < 1:
game_over()
elif e_health < 1:
you_win()
clock.tick(FPS)

pygame.quit()
quit()

game_intro()
gameLoop()

How To Run the Tank Game Python with Source Code?

To run this project, you must have installed a Pycharm on your PC (for Windows). Tank Game Python with Source Code is  for educational purposes only!

After downloading the project you must follow the steps below:

Step 1: Unzip the file or Extract the file

Step 2: Double click the TankGame

Step 3: Project is ready to run

Downloadable Source Code 

I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.

Summary

That’s how you create Tank Game Python in your projects. You can always expand and try different ways in implementing the Tank Game Python in your Python projects. Tank Game Python is free to download the open source code and it is use for educational purposes only.

Check out the different Python – related projects below:

Inquiries

If you have any questions or suggestions about Tank Game Python with Source Code, please feel free to leave a comment below.

1 thought on “Tank Game Python with Source Code”

Leave a Comment