PIXEL FIGHT
Well everyone has dreamt of making a game once in his/her lifetime!
Isn't it?
And if you are a student of class 12th then you might have gotten a project work to submit by this December!
And it say's the code should at least be of some 500 lines! 500 LINES MAN REALLY!!😒
But anyway!
Here i am to help all my fellow mates!
So, i have created a GUI based Space-ship Fight game using tkinter library in Python!
I will be posting the code in the below section and trust me i myself am a 12th std. student so the code won't be of god-level. So it won't get rejected by your viva teacher and also is easy to understand. In addition to that i have also mentioned some of the hints to easily understand what each function does and its working!
But in case if you get any trouble understanding the code then here's My Insta Id , where you can put any question whenever you want! would always love to help you!
Just copy,paste and run the code in python idle!
HAVE FUN!😄
from tkinter import *
from math import sqrt
from random import shuffle
from time import sleep, time
import tkinter.messagebox as t
HEIGHT = 768
WIDTH = 1366
window = Tk()
#for creating start button (but after this motion of the ship doesn't work)
'''t.askokcancel("Pixel Fight","hmm someone wants to play Pixel Fight! press ok to play")
if d=="no":
window.destroy()
else:
True'''
#Colours to pick for the enemy ships
colors = ["darkred", "green", "blue", "purple", "pink","red", "lime","black","violet","orange","yellow"]
#Name of the game
window.title("pixel fight")
#Creating interface
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()
#Creating our fighter ship
ship_id = c.create_polygon(20,10,35,25,35,30,25,23.50,25,35,35,45,20,40,5,45,15,35,15,23.50,5,30,5,25,15,15,fill="gold",outline="black")
pos=c.coords(ship_id) #to get actual time co-ordinates of our ship
SHIP_R = 15 #for comparison in Collision_bull
#for giving our ship strating position
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(ship_id, MID_X, MID_Y+200)
#definig functions for the motion of our ship
ship_spd = 20
score = 0
def left(event):
c.move(ship_id,-ship_spd,0)
def right(event):
c.move(1,ship_spd,0)
def up(event):
c.move(1,0,-ship_spd)
def down(event):
c.move(1,0,ship_spd)
#binding all the motion functions of our ship
window.bind("<Left>",left)
window.bind("<Right>",right)
window.bind("<Up>",up)
window.bind("<Down>",down)
#defining bullets for fire
bullet_id=[]
min_bull_r = 10
max_bull_r = 30
bull_r=[]
bull_speed=[]
bull_R = 25
def shoot_ship(event):
r = randint(min_bull_r, max_bull_r)
fire=c.create_rectangle(10,5,15,10,fill="pink")
bull_pos=c.coords(fire)
bullet_id.append(fire)
bull_r.append(r)
def get_coords():
pos = c.coords(ship_id)
x = (pos[0] + pos[2]) / 2
y = (pos[1] + pos[3]) / 2
return x,y
x,y=get_coords()
c.move(fire, x-21,y-17)
bull_speed.append(int(-30))
window.bind("<space>",shoot_ship)
#defining movement of bullet
def move_bullet():
for i in range(len(bullet_id)):
c.move(bullet_id[i], 0 , -30)
#Creating enemy ships (here enemy==bub)
from random import randint
bub_id = list()
bub_r = list()
bub_speed = list()
bub_id_e = list()
bub_r_e = list()
bub_speed_e = list()
min_bub_r = 10
max_bub_r = 30
max_bub_spd = 10
gap = 1500
#Creating Normal Enemy Ships -----------(Normal means these enemy ships can be destroyed!)
def create_bubble():
y = 0
x = randint(0, WIDTH)
r = randint(min_bub_r, max_bub_r)
id1 = c.create_polygon(20+x,40+y,35+x,25+y,35+x,20+y,25+x,27.50+y,25+x,15+y,35+x,5+y,20+x,10+y,5+x,5+y,15+x,15+y,15+x,27.50+y,5+x,20+y,5+x,25+y,outline="white", fill=colors[3])
pos=c.coords(id1)
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(5, max_bub_spd))
#Creating Evil Enemy Ships------------(Evil means these enemy ships can't be destroyed)
def create_bubble_e():
y = 0
x = randint(0, WIDTH)
r = randint(min_bub_r, max_bub_r)
id1 = c.create_polygon(20+x,40+y,35+x,25+y,35+x,20+y,25+x,27.50+y,25+x,15+y,35+x,5+y,20+x,10+y,5+x,5+y,15+x,15+y,15+x,27.50+y,5+x,20+y,5+x,25+y, outline="black", fill='red')
pos=c.coords(id1)
bub_id_e.append(id1)
bub_r_e.append(r)
bub_speed_e.append(randint(6, max_bub_spd))
#Creating Normal Enemy Ships -----------(Normal means these enemy ships can be destroyed!)
def create_bubble_r():
y = 0
x = randint(0, WIDTH)
r = randint(min_bub_r, max_bub_r) #creating random no. which going to be uesd in further comparison
id1 = c.create_polygon(20+x,40+y,35+x,25+y,35+x,20+y,25+x,27.50+y,25+x,15+y,35+x,5+y,20+x,10+y,5+x,5+y,15+x,15+y,15+x,27.50+y,5+x,20+y,5+x,25+y,outline="white", fill=colors[0])
pos=c.coords(id1)
bub_id.append(id1)
bub_r.append(r)
bub_speed.append(randint(6, max_bub_spd))
#Making Both Normal And Evil Enemy Ships move
def move_bubbles():
for i in range(len(bub_id)):
c.move(bub_id[i], 0 , bub_speed[i])
for i in range(len(bub_id_e)):
c.move(bub_id_e[i], 0, bub_speed_e[i])
#Variables for further comparisons in the MAIN LOOP CONDITIONS(for the creation of these obejects)
bub_chance = 30
bull_chance=30
#To get centre co-ordinates of any object where pos[0] and pos[2] gives extreme left and right co-ords of the obeject and same for y co-ords
def get_coords(id_num):
pos = c.coords(id_num)
x = (pos[0] + pos[2]) / 2
y = (pos[1] + pos[3]) / 2
return x, y
#To delete the Normal Enemy Ship
def del_bubble(i):
del bub_r[i]
del bub_speed[i]
c.delete(bub_id[i])
del bub_id[i]
#To delete the Evil Enemy Ship
def del_bubble_r(i):
del bub_r_e[i]
del bub_speed_e[i]
c.delete(bub_id_e[i])
del bub_id_e[i]
#To delete all the Normal Enemy Ships if they get out of vision!
def clean():
for i in range(len(bub_id) -1, -1, -1):
x, y = get_coords(bub_id[i])
if y >1366:
del_bubble(i)
#To delete all the Evil Enemy Ships if they get out of vision!
def cleanRed():
for i in range(len(bub_id_e)-1,-1,-1):
x,y=get_coords(bub_id_e[i])
if y>1366:
del_bubble_r(i)
#To delete Bullets
def del_bull(i):
del bull_r[i]
del bull_speed[i]
c.delete(bullet_id[i])
del bullet_id[i]
#To delete all the Bullets which were shot before and now out of sight!
def cleanbull():
for i in range(len(bullet_id) -1, -1, -1):
x, y = get_coords(bullet_id[i])
if y < 0:
del_bull(i)
#To get distance between any two obejcts
def distance(id1, id2):
x1, y1 = get_coords(id1)
x2, y2 = get_coords(id2)
return sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
#Creating variable for saving points the player scored
score=int(0)
#Defining to delete the Enemy ship and bullet which did collide and add a point of 30 to the score
def collision_bull():
for bub in range(len(bub_id) -1, -1, -1):
for i in range(len(bullet_id) -1, -1, -1):
if distance(bullet_id[i], bub_id[bub]) < (bull_R + bub_r[bub]): #here if the distance between the shot bullet and the enemy ship is less than the sum of
del_bubble(bub) # the constant bull_R and the random no. then the code will understand that the bullet has
del_bull(i) #hit the target and as these comparison is for pixels so it doesnt matter how big the sum
global score #of the random no. be!
score=score +int(30)
cleanbull()
return score
#Defining the collision of our ship and the Normal Enemy ship and Evil Enemy ship
def collision_e():
for bub in range(len(bub_id_e) -1, -1, -1):
if distance(ship_id, bub_id_e[bub]) < (SHIP_R + bub_r_e[bub]):
t.showinfo("Game Ended","You were killed by an enemy ship") #here tkinter.messagebox has been called!(if you die of the collision from Evil Enemy Ship
window.destroy() #the canvas is being destroyed
print("You got ", score, " score!")
sleep(100)
for bub in range(len(bub_id) -1, -1, -1):
if distance(ship_id, bub_id[bub]) < (SHIP_R + bub_r[bub]):
t.showinfo("Game Ended","lol such a loser! duh you could have shot the enemy ship!")
#here tkinter.messagebox has been called!(if you die of the collision from Evil Enemy Ship
window.destroy() ##the canvas is being destroyed
print("You got ", score, " score!")
sleep(100)
#Creating the Score Menu
c.create_text(683, 30, text="SCORE", fill="white")
st = c.create_text(683, 50, fill="gold")
#Creating the level Menu
level=int(0)
c.create_text(50, 30, text="LEVEL", fill="white")
tt = c.create_text(50, 50, fill="gold")
#Creating the Creators Menu
c.create_text(1280, 30, text="Creators", fill="white")
c.create_text(1280, 50,text="Harshit" ,fill="gold")
#Defining the Conditions of Level Ups
def show_l(level):
if score <=400:
c.itemconfig(tt, text=str(1))
elif score <=1500:
c.itemconfig(tt, text=str(2))
else:
c.itemconfig(tt, text=str(3))
#Defining the condintion for adding text in the Score text (defining another term and not using in there itself to update it everytime a bullet hits the target
def show(score):
c.itemconfig(st, text=str(score))
#creating variable for making a condition of creation of Enemy ship
evil_bub = 50
#MAIN GAME LOOP WHICH DETERMINES THE CALLING OF EACH FUNCTION
while True:
if randint(1, bub_chance) == 1: #IT CHECKS RANDOMLY BETWEEN 1-30 AND WHEN IT GETS 1 THERE IS CREATION OF ONE NORMAL ENEMY SHIP
create_bubble()
if randint(1, evil_bub) == 1: #IT CHECKS RANDOMLY BETWEEN 1-30 AND WHEN IT GETS 1 THERE IS CREATION OF ONE EVIL ENEMY SHIP
create_bubble_e()
if randint(1, 50) == 1: #IT CHECKS RANDOMLY BETWEEN 1-30 AND WHEN IT GETS 1 THERE IS CREATION OF ONE NORAML ENEMY SHIP
create_bubble_r()
move_bubbles() #----------------->CALLING MOVE FUNCTION TO MOVE THE NORMAL ENEMY SHIPS
move_bullet() #------------------>CALLING MOVE FUNCTION TO MOVE THE NORMAL ENEMY SHIPS
collision_bull() #--------------->CALLING THE COLLISION OF BULLET FUNCTION
collision_e() #------------------>CALLING THE COLLISION OF SHIPS
clean() #--------------------->CALLING THE DELETE FUNCTION TO STOP OVER FLOW OF STACK BY DELETING THE NORMAL ENEMY SHIPS
cleanRed() #--------------------->CALLING THE DELETE FUNCTION TO STOP OVER FLOW OF STACK BY DELETING THE EVIL ENEMY SHIPS
cleanbull() #-------------------->CALLING THE CLEAN BULLETS FUNCTION
#CONDITION FOR LEVEL UPS
if score >= 400:
evil_bub = 40
bub_chance = 25
if score >= 1000:
evil_bub = 30
bub_chance = 20
if score >= 1500:
evil_bub = 30
bub_chance = 20
show_l(level) #------------------>CALLING THE LEVEL FUNCTION
show(score) #------------------>CALLING THE SCORE FUNCTION
window.update() #------------------>UPDATING THE INTERFACE WITH ALL THE NEW DATA
shuffle(colors) #------------------>CALLING THE FUNCTION TO CHANGE THE COLOR OF THE ENEMY SHIP
sleep(0.0001) #------------------>SLEEP TIME WHEN THE WHOLE INTERFACE FREEZE (0.01 SECOND AS LESSER THE TIME THE WINDOW IS UPDATED THE SMOOTHER THE GAME PLAY)
window.mainloop()
Gud job dude... ��
ReplyDeleteGreat!! I would recommend people to use your service. Thanksfor the good job you did to me.
ReplyDeleteThis was really helpful and I recommend it to more people!!
ReplyDelete