Advertisement
here2share

# Tk_jump_maze_game.py

Jul 5th, 2022
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.59 KB | None | 0 0
  1. # Tk_jump_maze_game.py
  2.  
  3. from tkinter import *
  4. from PIL import Image, ImageTk
  5. import time
  6. import random
  7. import math
  8. import os
  9.  
  10. #CONFIGURATION
  11. WIDTH = 800
  12. HEIGHT = 600
  13. FPS = 30
  14. WHITE = (255, 255, 255)
  15. BLACK = (0, 0, 0)
  16. RED = (255, 0, 0)
  17. GREEN = (0, 255, 0)
  18. BLUE = (0, 0, 255)
  19. YELLOW = (255, 255, 0)
  20.  
  21. #PLAYER SETTINGS
  22. PLAYER_GRAV = 0.8
  23. PLAYER_FRICTION = -0.12
  24. PLAYER_JUMP = 15
  25.  
  26. #GAME SETTINGS
  27. BGCOLOR = BLACK
  28. GAMEOVER = False
  29.  
  30. #INITIALIZE pygame and create window
  31. root = Tk()
  32. root.title("MY GAME")
  33. canvas = Canvas(root, width=WIDTH, height=HEIGHT)
  34. root.resizable(0,0)
  35. canvas.pack()
  36.  
  37. #LOAD IMAGES
  38. game_dir = os.path.dirname(__file__)
  39. img_dir = os.path.join(game_dir, "img")
  40. PLAYER_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "boy.png")))
  41. PLATFORM_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "platform.png")))
  42. BG_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "maze1.png")))
  43. ENEMY_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "enemy1.png")))
  44. ENEMY2_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "enemy2.png")))
  45. ENEMY3_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "enemy3.png")))
  46. POWER_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "power.png")))
  47. HEART_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "heart.png")))
  48. CLOCK_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "clock.png")))
  49.  
  50. #PLAYER CLASS
  51. class Player(object):
  52.     def __init__(self, x, y, width, height):
  53.         self.x = x
  54.         self.y = y
  55.         self.width = width
  56.         self.height = height
  57.         self.speedx = 0
  58.         self.speedy = 0
  59.         self.jump = False
  60.         self.jumpCount = 10
  61.         self.falling = True
  62.         self.sliding = False
  63.         self.hitbox = (self.x + 10, self.y, 28, 50)
  64.         self.health = 10
  65.         self.visible = True
  66.         self.immune = False
  67.         self.immuneCount = 0
  68.  
  69.  
  70.     def draw(self):
  71.         if self.visible == True:
  72.             canvas.create_image(self.x, self.y, image=PLAYER_IMG)
  73.         self.hitbox = (self.x + 10, self.y, 28, 50)
  74.         #canvas.create_rectangle(self.hitbox[0], self.hitbox[1] - 20, self.hitbox[0] + 50, self.hitbox[1] + 10, outline='red')
  75.         if self.immune == True:
  76.             canvas.create_rectangle(self.hitbox[0] - 1, self.hitbox[1] - 5, self.hitbox[0] + 29, self.hitbox[1] + 65, outline='white')
  77.  
  78. #PLATFORM CLASS
  79. class Platform(object):
  80.     def __init__(self,x,y,width,height):
  81.         self.x = x
  82.         self.y = y
  83.         self.width = width
  84.         self.height = height
  85.         self.hitbox = (self.x, self.y, self.width, self.height)
  86.         self.visible = True
  87.  
  88.     def draw(self):
  89.         if self.visible == True:
  90.             canvas.create_image(self.x, self.y, image=PLATFORM_IMG)
  91.         #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
  92.  
  93. #ENEMY CLASS
  94. class Enemy(object):
  95.     def __init__(self, x, y, width, height):
  96.         self.x = x
  97.         self.y = y
  98.         self.width = width
  99.         self.height = height
  100.         self.speedx = 0
  101.         self.speedy = 0
  102.         self.hitbox = (self.x, self.y, 28, 50)
  103.         self.health = 10
  104.         self.visible = True
  105.  
  106.     def draw(self):
  107.         if self.visible == True:
  108.             canvas.create_image(self.x, self.y, image=ENEMY_IMG)
  109.         #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + 28, self.hitbox[1] + 50, outline='red')
  110.  
  111. #POWERUP CLASS
  112. class Powerup(object):
  113.     def __init__(self):
  114.         self.x = random.randint(100, WIDTH)
  115.         self.y = -100
  116.         self.speedx = 0
  117.         self.speedy = 0
  118.         self.width = 50
  119.         self.height = 50
  120.         self.hitbox = (self.x, self.y, self.width, self.height)
  121.         self.visible = True
  122.  
  123.     def draw(self):
  124.         if self.visible == True:
  125.             canvas.create_image(self.x, self.y, image=POWER_IMG)
  126.         #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
  127.  
  128. #HEARTUP CLASS
  129. class Heartup(object):
  130.     def __init__(self):
  131.         self.x = random.randint(100, WIDTH)
  132.         self.y = -100
  133.         self.speedx = 0
  134.         self.speedy = 0
  135.         self.width = 50
  136.         self.height = 50
  137.         self.hitbox = (self.x, self.y, self.width, self.height)
  138.         self.visible = True
  139.  
  140.     def draw(self):
  141.         if self.visible == True:
  142.             canvas.create_image(self.x, self.y, image=HEART_IMG)
  143.         #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
  144.  
  145. #CLOCKUP CLASS
  146. class Clockup(object):
  147.     def __init__(self):
  148.         self.x = random.randint(100, WIDTH)
  149.         self.y = -100
  150.         self.speedx = 0
  151.         self.speedy = 0
  152.         self.width = 50
  153.         self.height = 50
  154.         self.hitbox = (self.x, self.y, self.width, self.height)
  155.         self.visible = True
  156.  
  157.     def draw(self):
  158.         if self.visible == True:
  159.             canvas.create_image(self.x, self.y, image=CLOCK_IMG)
  160.         #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
  161.  
  162. #CREATING SPRITES
  163. player = Player(300, HEIGHT-50, 64, 64)
  164. platformCount = random.randint(2, 6)
  165. platforms = []
  166. enemies = []
  167. powerups = []
  168. heartups = []
  169. clockups = []
  170.  
  171. for i in range(platformCount):
  172.     platforms.append(Platform(random.randint(0, WIDTH), random.randint(-70, -30), 100, 10)TH),
  173.  
  174. for i in range(2):
  175.     enemies.append(Enemy(random.randint(0, WIDTH), random.randint(-200, -50), 64, 64))
  176.  
  177. powerup = Powerup()
  178. heartup = Heartup()
  179. clockup = Clockup()
  180.  
  181. #FUNCTION TO REDRAW SPRITES ON SCREEN
  182. def redrawGameWindow():
  183.     global GAMEOVER
  184.     canvas.create_image(0,0,image=BG_IMG)
  185.     canvas.create_text(WIDTH/2, HEIGHT/2, text="You have completed Level 1", font="Arial 30", fill="white")
  186.     canvas.create_text(WIDTH/2, HEIGHT/2 + 40, text="Press SPACE to continue to Level 2", font="Arial 20", fill="white")
  187.     canvas.create_text(WIDTH/2, HEIGHT/2 + 80, text="Press ESCAPE to exit", font="Arial 20", fill="white")
  188.     for platform in platforms:
  189.         platform.draw()
  190.     for enemy in enemies:
  191.         enemy.draw()
  192.     powerup.draw()
  193.     heartup.draw()
  194.     clockup.draw()
  195.     player.draw()
  196.     canvas.create_text(WIDTH/2, 50, text="LEVEL: 2", font="Arial 20", fill="white")
  197.     canvas.create_text(30, 30, text="HEALTH: " + str(player.health), font="Arial 20", fill="white")
  198.     canvas.create_text(WIDTH - 30, 30, text="POWER: " + str(powerup.visible), font="Arial 20", fill="white")
  199.     canvas.create_text(WIDTH/2, HEIGHT - 30, text="SCORE: " + str(score), font="Arial 20", fill="white")
  200.     root.update_idletasks()
  201.     root.update()
  202.     time.sleep(0.01)
  203.  
  204. #GAME LOOP
  205. while not GAMEOVER:
  206.     for event in root.tkinter_bind():
  207.         if event.type == "2":
  208.             if event.key == "escape":
  209.                 GAMEOVER = True
  210.             if event.key == "space":
  211.                 GAMEOVER = True
  212.  
  213.     if player.visible == True:
  214.         if player.hitbox[1] < HEIGHT + 10:
  215.             player.falling = True
  216.         else:
  217.             player.falling = False
  218.             player.jumpCount = 10
  219.  
  220.         if player.jumpCount >= 0:
  221.             neg = 1
  222.             if player.jumpCount < 0:
  223.                 neg = -1
  224.             player.speedy -= (player.jumpCount ** 2) * 0.3 * neg
  225.             player.jumpCount -= 1
  226.  
  227.         else:
  228.             player.jump = False
  229.  
  230.     player.speedx = 0
  231.  
  232.     #KEYBOARD BINDING
  233.     root.tkinter_bind()
  234.     redrawGameWindow()
  235.  
  236. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement