import pygame import math import random import time import sys pygame.init() SCREEN_WIDTH = 800 SCREEN_HEIGHT = int(SCREEN_WIDTH * 0.8) screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('giggity goo') RED = (255, 0, 0) GREEN = (144, 201, 120) IDK = (176, 198, 207) clock = pygame.time.Clock() FPS = 60 PPOS = 0 PD = 0 BPOS = 0 MPOS = 0 PLAYERALIVE = True DISTANCEFR = 0 #all of the variables font = pygame.font.SysFont("arialblack", 40) TEXT_COL = (255, 255, 255) Phealth = 125 moving_left = False moving_right = False moving_up = False moving_down = False dash = False rect_width = Phealth rect_height = 60 rect_x = 550 rect_y = 75 damage = False banditsight = False start = False lvl1 = False lvl2 = False lvl3 = False invent = False b = (1, 1) scroll_left = False scroll_left = False scroll_left = False scroll_left = False BLACK = (0, 0, 0) #menus def draw_text(text, font, text_col, x, y): img = font.render(text, True, text_col) screen.fill(IDK) screen.blit(img,(x, y)) def inven(): img = pygame.image.load('ver 1/imgs/inventory/stuff/0.png') img = pygame.transform.scale(img,(800, 640)) if invent == True: screen.blit(img, b) def boundery(): pygame.draw.line(screen, RED, (300, 200), (500, 200)) pygame.draw.line(screen, RED, (300, 400), (500, 400)) pygame.draw.line(screen, RED, (300, 400), (300, 200)) pygame.draw.line(screen, RED, (500, 200), (500, 400)) #fill the screen def draw_bg(): screen.fill(GREEN) def hbar(): rect = pygame.draw.rect(screen, RED,(rect_y, rect_x, rect_width, rect_height)) #the code for the player(the size, the image, moving, displaying and all that) class Player(pygame.sprite.Sprite): def __init__(self,char_type, x, y): pygame.sprite.Sprite.__init__(self) self.char_type = char_type self.alive = True self.look = 0 self.direction = 0 self.animation_list = [] self.frame_index = 0 self.action = 0 self.update_time = pygame.time.get_ticks() temp_list = [] for i in range(3): img = pygame.image.load(f'ver 1/imgs/player/idle/{i}.png') img = pygame.transform.scale(img,(70, 120)) temp_list.append(img) self.animation_list.append(temp_list) temp_list = [] for i in range(3): img = pygame.image.load(f'ver 1/imgs/player/PMR/{i}.png') img = pygame.transform.scale(img,(70, 120)) temp_list.append(img) self.animation_list.append(temp_list) temp_list = [] for i in range(3): img = pygame.image.load(f'ver 1/imgs/player/PML/{i}.png') img = pygame.transform.scale(img,(70, 120)) temp_list.append(img) self.animation_list.append(temp_list) self.image = self.animation_list[self.action][self.frame_index] self.rect = self.image.get_rect() self.rect.center = (x, y) global PPOS PPOS = self.rect.center global PPOSX PPOSX = self.rect.x global PPOSY PPOSY = self.rect.y global ML global MR global MU global MD ML = False MR = False MU = False MD = False def move(self, moving_left, moving_right, moving_up, moving_down): dx = 0 dy = 0 if moving_left: dx = -4 ML = True if moving_right: dx = 4 MR = True if moving_up: dy = -4 MU = True if moving_down: dy = 4 MD = True if damage: Phealth =- 7 self.rect.x += dx self.rect.y += dy def update_animation(self): #update animation ANIMATION_COOLDOWN = 100 #update image depending on current frame self.image = self.animation_list[self.action][self.frame_index] #check if enough time has passed since the last update if pygame.time.get_ticks() - self.update_time > ANIMATION_COOLDOWN: self.update_time = pygame.time.get_ticks() self.frame_index += 1 #if the animation has run out then reset to the start if self.frame_index >= len(self.animation_list[self.action]): self.frame_index = 0 def update_action(self, new_action): #check if new action is different to previous one if new_action != self.action: self.action= new_action #update the animation settings self.frame_index = 0 self.update_time = pygame.time.get_ticks() def draw(self): screen.blit(self.image, self.rect) class Merc(pygame.sprite.Sprite): def __init__(self,char_type, x, y): pygame.sprite.Sprite.__init__(self) self.char_type = char_type self.alive = True self.look = 0 self.direction = 0 img = pygame.image.load(f'ver 1/imgs/enemies/Merc/musket/idle/0.png') self.image = pygame.transform.scale(img,(70, 120)) self.rect = img.get_rect() self.rect.center = (x, y) def draw(self): screen.blit(self.image, self.rect) class Bandit(pygame.sprite.Sprite): def __init__(self,char_type, x, y): pygame.sprite.Sprite.__init__(self) self.char_type = char_type self.alive = True self.look = 0 self.direction = 0 img = pygame.image.load(f'ver 1/imgs/enemies/bandit/s&s/idle/0.png') self.image = pygame.transform.scale(img,(70, 120)) self.rect = img.get_rect() self.rect.center = (x, y) def move(self, moving_left, moving_right, moving_up, moving_down): dx = 0 dy = 0 if moving_left: dx = -0 ML = True if moving_right: dx = 0 MR = True if moving_up: dy = -0 MU = True if moving_down: dy = 0 MD = True if damage: Phealth =- 7 self.rect.x += dx self.rect.y += dy def draw(self): screen.blit(self.image, self.rect) class L1(): def __init__(self, x, y): self.image = pygame.image.load('ver 1/imgs/lvls/lvl1/0.png') self.image = pygame.transform.scale(self.image,(1400, 1240)) self.rect = self.image.get_rect() self.rect.center = (x, y) def move(self): dx = 0 dy = 0 if ML: dx = 4 if MR: dx = -4 if MU: dy = 4 if MD: dy = -4 if damage: Phealth =- 7 self.rect.x += dx self.rect.y += dy def draw(self): screen.blit(self.image, self.rect) player = Player(400, 400, 310) merc = Merc(500, 500, 130) bandit = Bandit(300, 300, 275) l1 = L1(700, 600) #main loop run = True while run: #FPS clock.tick(FPS) #the background draw_text("press space to play", font, TEXT_COL, 160, 250) if start == True: draw_bg() player.update_animation() player.draw() player.move(moving_left, moving_right, moving_up, moving_down) inven() boundery() merc.draw() bandit.draw() hbar() if lvl1 == True: l1.draw() l1.move() player.draw() player.move(moving_left, moving_right, moving_up, moving_down) player.update_animation() inven() bandit.draw() start = False if moving_left: player.update_action(2)#run left else: player.update_action(1)#stop and stare if moving_right: player.update_action(1)# run right else: player.update_action(2)#stop and stare player.move(moving_left, moving_right, moving_up, moving_down) #if off turn off for event in pygame.event.get(): #quit game if event.type == pygame.QUIT: run = False sys.exit() #the keys pressed if event.type == pygame.KEYDOWN: if event.key == pygame.K_a: moving_left = True ML = True if event.key == pygame.K_d: moving_right = True MR = True if event.key == pygame.K_w: moving_up = True MU = True if event.key == pygame.K_s: moving_down = True MD = True if event.key == pygame.K_f: dash = True if event.key == pygame.K_SPACE: start = True if event.key == pygame.K_ESCAPE: run = False if event.key == pygame.K_i: invent = True if event.key == pygame.K_o: lvl1 = True if event.key == pygame.K_h: damage = True #the keys released if event.type == pygame.KEYUP: if event.key == pygame.K_a: moving_left = False ML = False if event.key == pygame.K_d: moving_right = False MR = False if event.key == pygame.K_w: moving_up = False MU = False if event.key == pygame.K_s: moving_down = False MD = False if event.key == pygame.K_f: dash = False if event.key == pygame.K_SPACE: damage = False if event.key == pygame.K_i: invent = False pygame.display.flip() pygame.display.update() pygame.quit()