import pygame import sys import random from pygame.locals import * from settings import * WIDTH = 600 HEIGHT = 600 FPS = 120 EDITFPS = 10 WHITE = (255,255,255) BLACK = (0,0,0) RED = (255,0,0) GREEN = (0,255,0) BLUE = (0,0,255) YELLOW = (255,255,0) pygame.init() CLOCK = pygame.time.Clock() gameScreen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("CubeAdventures1") SSplayer = pygame.image.load('player.png') SSgrass = pygame.image.load('grass.png') SSwall = pygame.image.load('wall.png') SSredblock = pygame.image.load('redblock.png') SSstar = pygame.image.load('star.png') SSfinish = pygame.image.load('finish.png') blocklist = [] scrollingOn = False ScrollingVerticalOn = False class Game(): def __int__(self): self.running = True def endgame(self): for event in pygame.event.get(): if event.type == QUIT: pygame.QUIT() def doall(self): self.endgame() class Edit(): def __init__(self, x, y, width, height, blockid): self.x = x self.y = y self.width = width self.height = height self.blockid = blockid self.pixels = 10 self.idedit = 0 self.timer = 0 self.vel = 10 self.left = False self.right = False def movement(self): self.timer += 1 print(self.timer) if self.timer >= 3: keys = pygame.key.get_pressed() if keys[K_LEFT]: self.x -= self.pixels self.timer = 0 if keys[K_RIGHT]: self.x += self.pixels self.timer = 0 if keys[K_UP]: self.y -= self.pixels self.timer = 0 if keys[K_DOWN]: self.y += self.pixels self.timer = 0 if keys[K_SPACE]: self.timer = 0 if self.idedit == 0: self.width = 20 self.height = 20 self.blockid = self.idedit blocklist.append(Edit(self.x, self.y, self.width, self.height, self.blockid)) if self.idedit == 1: self.width = 20 self.height = 1 self.blockid = self.idedit blocklist.append(Edit(self.x, self.y, self.width, self.height, self.blockid)) if self.idedit == 2 or 3 or 4 or 5: self.width = 20 self.height = 20 self.blockid = self.idedit blocklist.append(Edit(self.x, self.y, self.width, self.height, self.blockid)) if keys[K_w]: self.idedit += 1 self.timer = 0 if keys[K_s]: self.idedit -= 1 self.timer = 0 if keys[K_q]: with open('map.txt', 'w') as f: for lista in blocklist: f.write(str(lista.x) + " " + str(lista.y) + " " + str(lista.width) + " " + str( lista.height) + " " + str(lista.blockid) + "\n") self.timer = 0 def editordraw(self): if self.idedit == 0: gameScreen.blit(SSplayer, [self.x, self.y]) if self.idedit == 1: gameScreen.blit(SSgrass, [self.x, self.y]) if self.idedit == 2: gameScreen.blit(SSwall, [self.x, self.y]) if self.idedit == 3: gameScreen.blit(SSredblock, [self.x, self.y]) if self.idedit == 4: gameScreen.blit(SSstar, [self.x, self.y]) if self.idedit == 5: gameScreen.blit(SSfinish, [self.x, self.y]) for block in blocklist: if block.blockid == 0: gameScreen.blit(SSplayer, [block.x, block.y]) if block.blockid == 1: gameScreen.blit(SSgrass, [block.x, block.y]) if block.blockid == 2: gameScreen.blit(SSwall, [block.x, block.y]) if block.blockid == 3: gameScreen.blit(SSredblock, [block.x, block.y]) if block.blockid == 4: gameScreen.blit(SSstar, [block.x, block.y]) if block.blockid == 5: gameScreen.blit(SSfinish, [block.x, block.y]) def doall(self): self.movement() self.editordraw() # =============================================== # =============================================== # ============== CLASSES ======================== # =============================================== # ========= WALLS/DOORS/FLOORS ================== class block(object): def __init__(self, x, y, width, height): self.x = x self.y = y self.width = width self.height = height self.hitbox = pygame.Rect(int(self.x), int(self.y), int(self.width), int(self.height)) self.visible = True def draw(self, win): if self.visible: self.hitbox = pygame.Rect(int(self.x), int(self.y), int(self.width), int(self.height)) pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2) # =============================================== # =============================================== game = Game() editor = Edit(300, 300, 0, 0, 0) bg1ground = block(-670, 648, 6510, 1) grounds = [bg1ground] BACKGROUND = (22, 34, 120) while True: edit = True while edit: for event in pygame.event.get(): if event.type == QUIT: pygame.QUIT() keys = pygame.key.get_pressed() if keys[K_f]: with open('map.txt', 'r') as f: for lista in f: text = lista bufor = text.split(" ") blocklist.append( Edit(float(bufor[0]), float(bufor[1]), float(bufor[2]), float(bufor[3]), float(bufor[4]))) rungame = True edit = False # =================================================================================================================================================================== # =================================================================================================================================================================== # =============================================================== MOTION AREA BELLOW ======================================================================== # =================================================================================================================================================================== # =================================================================================================================================================================== # =============================================== # =============================================== # =============== KEYS - LEFT =================== # =============================================== # =============================================== if keys[pygame.K_LEFT] and man.x > man.vel: man.x -= 10 for a in grounds: if a.visible == True: a.x += editor.vel if scrollingOn == False: for e in blocklist: e.x = e.x editor.left = True editor.right = False # =============================================== # =============================================== # =============== KEYS - RIGHT ================== # =============================================== # =============================================== elif keys[pygame.K_RIGHT] and man.x < 1340 - man.width - man.vel: man.x += 10 for a in grounds: if a.visible == True: a.x -= editor.vel if scrollingOn == False: for e in blocklist: e.x = e.x else: editor.right = True editor.left = False # =================================================================================================================================================================== # =================================================================================================================================================================== # =============================================================== ENEMIES MOTION AREA ABOVE ========================================================================= # =================================================================================================================================================================== # =================================================================================================================================================================== # -------------------------------------------------------------- # ----------------- CAMERA ---------------------------------- # -------------------------------------------------------------- if bg1ground.x >= -4499 and bg1ground.x <= -1339: scrollingOn = True if scrollingOn == True: editor.x = 670 if keys[pygame.K_LEFT] and editor.x > editor.vel: for a in platformsA: a.x += editor.vel for e in enemies: e.x += editor.vel elif keys[pygame.K_RIGHT] and editor.x < 1340 - editor.width - editor.vel: for a in platformsA: a.x -= editor.vel for e in enemies: e.x -= editor.vel if bg1ground.x <= -4499 or bg1ground.x >= -1339: scrollingOn = False if editor.x <= 5 and editor.left == True: man.vel = 0 elif editor.x >= 1310 and editor.right == True: editor.vel = 0 else: editor.vel = 10 # -------------------------------------------------------------- # ----------------- CAMERA ---------------------------------- # -------------------------------------------------------------- game.doall() editor.doall() CLOCK.tick(EDITFPS) pygame.display.update() gameScreen.fill(BACKGROUND) while rungame: for event in pygame.event.get(): if event.type == QUIT: pygame.QUIT() game.doall() for lista in blocklist: if lista.blockid == 0: gameScreen.blit(SSplayer, [lista.x, lista.y]) if lista.blockid == 1: gameScreen.blit(SSgrass, [lista.x, lista.y]) if lista.blockid == 2: gameScreen.blit(SSwall, [lista.x, lista.y]) if lista.blockid == 3: gameScreen.blit(SSredblock, [lista.x, lista.y]) if lista.blockid == 4: gameScreen.blit(SSstar, [lista.x, lista.y]) if lista.blockid == 5: gameScreen.blit(SSfinish, [lista.x, lista.y]) CLOCK.tick(FPS) pygame.display.update() gameScreen.fill(BACKGROUND)