Python Mic
Python Mic
KOPARGOAN
SANJIVANI K.B.P.POLYTECHNIC.
Submitted By:
Dhandre Chetan(2100340485)
Submitted By:
Dhandre Chetan (2100340485)
SOURCE CODE:
from tkinter import *
import random
import time
class Game:
def __init__(self):
self.tk = Tk()
self.tk.title("Stick Boy")
self.tk.resizable(0, 0)
self.tk.wm_attributes("-topmost", 1)
self.canvas.pack()
self.tk.update()
self.canvas_height = 500
self.canvas_width = 500
self.bg = PhotoImage(file="background.gif")
w = self.bg.width()
h = self.bg.height()
self.sprites = []
self.running = True
def mainloop(self):
while 1:
if self.running:
sprite.move()
else:
time.sleep(1)
self.canvas.itemconfig(self.game_over_text, state='normal')
self.tk.update_idletasks()
self.tk.update()
time.sleep(0.02)
class Coords:
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
if (co1.x1 > co2.x1 and co1.x1 < co2.x2) or (co1.x2 > co2.x1 and co1.x2 < co2.x2) or
(co2.x1 > co1.x1 and co2.x1 < co1.x2) or (co2.x2 > co1.x1 and co2.x2 < co1.x1):
return True
else:
return False
if (co1.y1 > co2.y1 and co1.y1 < co2.y2) or (co1.y2 > co2.y1 and co1.y2 < co2.y2) or
(co2.y1 > co1.y1 and co2.y1 < co1.y2) or (co2.y2 > co1.y1 and co2.y2 < co1.y1):
return True
else:
return False
return True
return False
if within_y(co1, co2):
return True
return False
if within_x(co1, co2):
return True
return False
if within_x(co1, co2):
y_calc = co1.y2 + y
return True
return False
class Sprite:
self.game = game
self.endgame = False
self.coordinates = None
def move(self):
pass
def coords(self):
return self.coordinates
class PlatformSprite(Sprite):
Sprite.__init__(self, game)
self.photo_image = photo_image
class MovingPlatformSprite(PlatformSprite):
self.x = 2
self.counter = 0
self.last_time = time.time()
self.width = width
self.height = height
def coords(self):
xy = self.game.canvas.coords(self.image)
self.coordinates.x1 = xy[0]
self.coordinates.y1 = xy[1]
return self.coordinates
def move(self):
self.last_time = time.time()
self.game.canvas.move(self.image, self.x, 0)
self.counter += 1
self.x *= -1
self.counter = 0
class DoorSprite(Sprite):
self.closed_door = PhotoImage(file="door1.gif")
self.open_door = PhotoImage(file="door2.gif")
self.endgame = True
def opendoor(self):
self.game.canvas.itemconfig(self.image, image=self.open_door)
self.game.tk.update_idletasks()
def closedoor(self):
self.game.canvas.itemconfig(self.image, image=self.closed_door)
self.game.tk.update_idletasks()
class StickFigureSprite(Sprite):
Sprite.__init__(self, game)
self.images_left = [
PhotoImage(file="figure-L1.gif"),
PhotoImage(file="figure-L2.gif"),
PhotoImage(file="figure-L3.gif")
self.images_right = [
PhotoImage(file="figure-R1.gif"),
PhotoImage(file="figure-R2.gif"),
PhotoImage(file="figure-R3.gif")
image=self.images_left[0], anchor='nw')
self.x = -2
self.y = 0
self.current_image = 0
self.current_image_add = 1
self.jump_count = 0
self.last_time = time.time()
self.coordinates = Coords()
game.canvas.bind_all('<KeyPress-Left>', self.turn_left)
game.canvas.bind_all('<KeyPress-Right>', self.turn_right)
game.canvas.bind_all('<space>', self.jump)
if self.y == 0:
self.x = -2
if self.y == 0:
self.x = 2
if self.y == 0:
self.y = -4
self.jump_count = 0
def animate(self):
self.last_time = time.time()
self.current_image += self.current_image_add
if self.current_image >= 2:
self.current_image_add = -1
if self.current_image <= 0:
self.current_image_add = 1
if self.x < 0:
if self.y != 0:
self.game.canvas.itemconfig(self.image,
image=self.images_left[2])
else:
self.game.canvas.itemconfig(self.image,
image=self.images_left[self.current_image])
if self.y != 0:
self.game.canvas.itemconfig(self.image,
image=self.images_right[2])
else:
self.game.canvas.itemconfig(self.image,
image=self.images_right[self.current_image])
def coords(self):
xy = self.game.canvas.coords(self.image)
self.coordinates.x1 = xy[0]
self.coordinates.y1 = xy[1]
self.coordinates.x2 = xy[0] + 27
self.coordinates.y2 = xy[1] + 30
return self.coordinates
def move(self):
self.animate()
if self.y < 0:
self.jump_count += 1
self.y = 4
if self.y > 0:
self.jump_count -= 1
co = self.coords()
left = True
right = True
top = True
bottom = True
falling = True
self.y = 0
bottom = False
self.y = 0
top = False
self.x = 0
right = False
self.x = 0
left = False
if sprite == self:
continue
sprite_co = sprite.coords()
self.y = -self.y
top = False
self.y = 0
bottom = False
top = False
falling = False
self.x = 0
left = False
if sprite.endgame:
self.end(sprite)
self.x = 0
right = False
if sprite.endgame:
self.end(sprite)
self.y = 4
self.game.running = False
sprite.opendoor()
time.sleep(1)
self.game.canvas.itemconfig(self.image, state='hidden')
sprite.closedoor()
g = Game()
g.sprites.append(platform1)
g.sprites.append(platform2)
g.sprites.append(platform3)
g.sprites.append(platform4)
g.sprites.append(platform5)
g.sprites.append(platform6)
g.sprites.append(platform7)
g.sprites.append(platform8)
g.sprites.append(platform8)
g.sprites.append(platform9)
g.sprites.append(platform10)
g.sprites.append(door)
sf = StickFigureSprite(g)
g.sprites.append(sf)
g.mainloop()
OUTPUT:
CONCLUSION
In this article, we have created a simple Stickman game in python with the help of
the Tkinter library. You can try to make the game more interesting and challenging
by adding obstacles on the way or decreasing the health of the Stickman Game in
Python whenever it falls down on the ground. Hope this article will help you.
REFERENCES
https://www.sourcecodester.com/python
https://github.com/MvE09/Mr_Stickman-game-from-Python-for-Kids-
Jason-R.-Briggs1