The Tank Game Python is a fully functional desktop application project that covers all of the elements that IT students and computer-related courses will require for their college projects or for leisure time purposes.
What is Tank Game Python?
The Tank Game In Python is an armored battle game vehicle designed for leisure purposes.
The game is so simple to play; you just need to aim at the exact location of the opponents’ tanks to hit them exactly.
The player needs to have good calculations and aim to destroy the opponent’s tank.
This Tank Python is useful for learning new skills and practicing Python game development. This project is quite useful, and the concept and logic of the project are simple to grasp. The source code is open source and free to use. Simply scroll down and click the download option.
Tank Game Python: Project Output
How to play Tank Game Python?
This Tank Game Python Code is simple to use; simply run the Python code, and a game menu will appear, with options to play, control, and exit the game.
To begin the game, click the start button, which will take you to the game’s main features, where you can play and play against an opponent tank.
To win, you must first destroy the opponent’s tank.
About the Project: Tank Game Python With Source Code
The Tank Game On Python game is graphical user interface (GUI) based, with basic controls for the users. When it comes to gameplay, it’s one of the most addicting games out there.
All of the game’s mechanics are straightforward.
The basic goal of this game is to shoot down and destroy your opponent’s tank before you are destroyed. Enemies become more difficult as the player shoots more.
The player must use the left and right arrow keys to move the tank and the Space Bar to destroy the enemy tanks.
Remember that the player must maintain a power level to attack, and there is a power bar at the top.
The Tank Python Game is a simple desktop application made using the Python programming language. We may also create highly fascinating games with the Python programming language. The Tank game is one of them.
The project system file comprises resource files as well as a Python script. The graphics of the game are smooth, and the controls are simple.
This is How To Make A Tank Game In Python it includes a tutorial and a code development guide. This is a simple and basic-level little project for learning purposes.
Tank game is open source, so you can download the zip file and change it to fit your needs.
You can also customize this project to meet your needs and create a fantastic advanced-level project.
Project Details and Technology: Tank Game Python
Project Name: | Tank Game Python |
Abstract: | This Tank Game Python is a simple battle tank game. It’s for people who desire a gaming platform like this for their free time, as well as students who need a project like this. |
Language/s Used: | Python (GUI Based) |
Python version (Recommended): | 3.8 or 3.9 |
Type: | Desktop Application |
Developer: | Glenn Magada Azuelo |
Updates: | 0 |
The code given below is the full source code on how to make the game from Python
The given code below is a Python file for TankGame.py
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) 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) clock = pygame.time.Clock() tnk_width = 40 tnk_height = 20 tur_width = 5 whl_width = 5 grnd_height = 35 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) def Score(Score): txt = s_font.render("Score: " + str(Score), True, white) game_layout_display.blit(txt, [0, 0]) 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() 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) 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] 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] 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) 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) 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: # game_layout_display.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) 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]) 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 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(startShell[0],startShell[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 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(game_layout_display, red, (startShell[0],startShell[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!") pow_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 def power(level): text = s_font.render("Power: " + str(level) + "%", True, wheat) game_layout_display.blit(text, [display_width / 2, 0]) 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="large") 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) # msg_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) 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) 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) 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)) 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()
This Tank Game Python With Source Code also includes a downloadable Project With Source Code for free, just find the downloadable source code below and click to start downloading.
By the way, if you are new to Python programming and don’t have any idea what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS for you.
I also have here How to Download and Install the Latest Version of Python on Windows.
To start executing a Tank Game Python, make sure that you have installed Python on your computer.
Tank Game Python: Steps on how to run the project
Time needed: 5 minutes
These are the steps on how to run Tank Game Python
- Step 1: Download Source Code
First, find the downloadable source code below and click to start downloading the source code file.
- Step 2: Extract File
Next, after finished to download the file, go to the file location and right-click the file and click extract.
- Step 3 : Open PyCharm
Next, open pycharm IDE and open the project you’ve downloaded.
- Step 4: Run Project
Next, go to the Pycharm and click the run button to start executing the project.
Download the Source Code below!
Summary
This article is a way to enhance and develop our skills and logic ideas which is important in practicing Python programming language which is the most well-known and most usable programming language in many companies.
Related Articles
- Mario Game In Python With Source Code
- Traffic Racer Game In Python With Source Code
- Pacman In Python Code
- Sudoku Game Python Code
- Color Game Using Tkinter In Python With Source Code
- Hangman Game In Python With Source Code
- Rock Paper Scissors Game In Python With Source Code
- Bouncing Ball Game In Python With Source Code
- Snake Game In Python Code
- Typing Speed Test In Python With Source Code
- Flappy Bird Game In Python With Source Code
- Tic Tac Toe In Python With Source Code
- Quiz Application In Python With Source Code
- Tetris Game In Python With Source Code
Inquiries
If you have any questions or suggestions about Tank Game Python With Source Code, please feel free to leave a comment below.