Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_jump_maze_game.py
- from tkinter import *
- from PIL import Image, ImageTk
- import time
- import random
- import math
- import os
- #CONFIGURATION
- WIDTH = 800
- HEIGHT = 600
- FPS = 30
- WHITE = (255, 255, 255)
- BLACK = (0, 0, 0)
- RED = (255, 0, 0)
- GREEN = (0, 255, 0)
- BLUE = (0, 0, 255)
- YELLOW = (255, 255, 0)
- #PLAYER SETTINGS
- PLAYER_GRAV = 0.8
- PLAYER_FRICTION = -0.12
- PLAYER_JUMP = 15
- #GAME SETTINGS
- BGCOLOR = BLACK
- GAMEOVER = False
- #INITIALIZE pygame and create window
- root = Tk()
- root.title("MY GAME")
- canvas = Canvas(root, width=WIDTH, height=HEIGHT)
- root.resizable(0,0)
- canvas.pack()
- #LOAD IMAGES
- game_dir = os.path.dirname(__file__)
- img_dir = os.path.join(game_dir, "img")
- PLAYER_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "boy.png")))
- PLATFORM_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "platform.png")))
- BG_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "maze1.png")))
- ENEMY_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "enemy1.png")))
- ENEMY2_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "enemy2.png")))
- ENEMY3_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "enemy3.png")))
- POWER_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "power.png")))
- HEART_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "heart.png")))
- CLOCK_IMG = ImageTk.PhotoImage(Image.open(os.path.join(img_dir, "clock.png")))
- #PLAYER CLASS
- class Player(object):
- def __init__(self, x, y, width, height):
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.speedx = 0
- self.speedy = 0
- self.jump = False
- self.jumpCount = 10
- self.falling = True
- self.sliding = False
- self.hitbox = (self.x + 10, self.y, 28, 50)
- self.health = 10
- self.visible = True
- self.immune = False
- self.immuneCount = 0
- def draw(self):
- if self.visible == True:
- canvas.create_image(self.x, self.y, image=PLAYER_IMG)
- self.hitbox = (self.x + 10, self.y, 28, 50)
- #canvas.create_rectangle(self.hitbox[0], self.hitbox[1] - 20, self.hitbox[0] + 50, self.hitbox[1] + 10, outline='red')
- if self.immune == True:
- canvas.create_rectangle(self.hitbox[0] - 1, self.hitbox[1] - 5, self.hitbox[0] + 29, self.hitbox[1] + 65, outline='white')
- #PLATFORM CLASS
- class Platform(object):
- def __init__(self,x,y,width,height):
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.hitbox = (self.x, self.y, self.width, self.height)
- self.visible = True
- def draw(self):
- if self.visible == True:
- canvas.create_image(self.x, self.y, image=PLATFORM_IMG)
- #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
- #ENEMY CLASS
- class Enemy(object):
- def __init__(self, x, y, width, height):
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.speedx = 0
- self.speedy = 0
- self.hitbox = (self.x, self.y, 28, 50)
- self.health = 10
- self.visible = True
- def draw(self):
- if self.visible == True:
- canvas.create_image(self.x, self.y, image=ENEMY_IMG)
- #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + 28, self.hitbox[1] + 50, outline='red')
- #POWERUP CLASS
- class Powerup(object):
- def __init__(self):
- self.x = random.randint(100, WIDTH)
- self.y = -100
- self.speedx = 0
- self.speedy = 0
- self.width = 50
- self.height = 50
- self.hitbox = (self.x, self.y, self.width, self.height)
- self.visible = True
- def draw(self):
- if self.visible == True:
- canvas.create_image(self.x, self.y, image=POWER_IMG)
- #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
- #HEARTUP CLASS
- class Heartup(object):
- def __init__(self):
- self.x = random.randint(100, WIDTH)
- self.y = -100
- self.speedx = 0
- self.speedy = 0
- self.width = 50
- self.height = 50
- self.hitbox = (self.x, self.y, self.width, self.height)
- self.visible = True
- def draw(self):
- if self.visible == True:
- canvas.create_image(self.x, self.y, image=HEART_IMG)
- #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
- #CLOCKUP CLASS
- class Clockup(object):
- def __init__(self):
- self.x = random.randint(100, WIDTH)
- self.y = -100
- self.speedx = 0
- self.speedy = 0
- self.width = 50
- self.height = 50
- self.hitbox = (self.x, self.y, self.width, self.height)
- self.visible = True
- def draw(self):
- if self.visible == True:
- canvas.create_image(self.x, self.y, image=CLOCK_IMG)
- #canvas.create_rectangle(self.hitbox[0], self.hitbox[1], self.hitbox[0] + self.width, self.hitbox[1] + self.height, outline='red')
- #CREATING SPRITES
- player = Player(300, HEIGHT-50, 64, 64)
- platformCount = random.randint(2, 6)
- platforms = []
- enemies = []
- powerups = []
- heartups = []
- clockups = []
- for i in range(platformCount):
- platforms.append(Platform(random.randint(0, WIDTH), random.randint(-70, -30), 100, 10)TH),
- for i in range(2):
- enemies.append(Enemy(random.randint(0, WIDTH), random.randint(-200, -50), 64, 64))
- powerup = Powerup()
- heartup = Heartup()
- clockup = Clockup()
- #FUNCTION TO REDRAW SPRITES ON SCREEN
- def redrawGameWindow():
- global GAMEOVER
- canvas.create_image(0,0,image=BG_IMG)
- canvas.create_text(WIDTH/2, HEIGHT/2, text="You have completed Level 1", font="Arial 30", fill="white")
- canvas.create_text(WIDTH/2, HEIGHT/2 + 40, text="Press SPACE to continue to Level 2", font="Arial 20", fill="white")
- canvas.create_text(WIDTH/2, HEIGHT/2 + 80, text="Press ESCAPE to exit", font="Arial 20", fill="white")
- for platform in platforms:
- platform.draw()
- for enemy in enemies:
- enemy.draw()
- powerup.draw()
- heartup.draw()
- clockup.draw()
- player.draw()
- canvas.create_text(WIDTH/2, 50, text="LEVEL: 2", font="Arial 20", fill="white")
- canvas.create_text(30, 30, text="HEALTH: " + str(player.health), font="Arial 20", fill="white")
- canvas.create_text(WIDTH - 30, 30, text="POWER: " + str(powerup.visible), font="Arial 20", fill="white")
- canvas.create_text(WIDTH/2, HEIGHT - 30, text="SCORE: " + str(score), font="Arial 20", fill="white")
- root.update_idletasks()
- root.update()
- time.sleep(0.01)
- #GAME LOOP
- while not GAMEOVER:
- for event in root.tkinter_bind():
- if event.type == "2":
- if event.key == "escape":
- GAMEOVER = True
- if event.key == "space":
- GAMEOVER = True
- if player.visible == True:
- if player.hitbox[1] < HEIGHT + 10:
- player.falling = True
- else:
- player.falling = False
- player.jumpCount = 10
- if player.jumpCount >= 0:
- neg = 1
- if player.jumpCount < 0:
- neg = -1
- player.speedy -= (player.jumpCount ** 2) * 0.3 * neg
- player.jumpCount -= 1
- else:
- player.jump = False
- player.speedx = 0
- #KEYBOARD BINDING
- root.tkinter_bind()
- redrawGameWindow()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement