Facebook
From Burly Gibbon, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 60
  1. import pygame
  2. import sys
  3. import random
  4. from pygame.locals import *
  5. from settings import *
  6.  
  7. WIDTH = 600
  8. HEIGHT = 600
  9.  
  10. FPS = 120
  11. EDITFPS = 10
  12.  
  13.  
  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. pygame.init()
  22.  
  23. CLOCK = pygame.time.Clock()
  24. gameScreen = pygame.display.set_mode((WIDTH, HEIGHT))
  25. pygame.display.set_caption("CubeAdventures1")
  26.  
  27. SSplayer = pygame.image.load('player.png')
  28. SSgrass = pygame.image.load('grass.png')
  29. SSwall = pygame.image.load('wall.png')
  30. SSredblock = pygame.image.load('redblock.png')
  31. SSstar = pygame.image.load('star.png')
  32. SSfinish = pygame.image.load('finish.png')
  33. blocklist = []
  34.  
  35. scrollingOn = False
  36. ScrollingVerticalOn = False
  37.  
  38.  
  39. class Game():
  40.     def __int__(self):
  41.         self.running = True
  42.  
  43.     def endgame(self):
  44.         for event in pygame.event.get():
  45.             if event.type == QUIT:
  46.                 pygame.QUIT()
  47.  
  48.     def doall(self):
  49.         self.endgame()
  50.  
  51.  
  52. class Edit():
  53.     def __init__(self, x, y, width, height, blockid):
  54.         self.x = x
  55.         self.y = y
  56.         self.width = width
  57.         self.height = height
  58.         self.blockid = blockid
  59.         self.pixels = 10
  60.         self.idedit = 0
  61.         self.timer = 0
  62.         self.vel = 10
  63.         self.left = False
  64.         self.right = False
  65.  
  66.     def movement(self):
  67.         self.timer += 1
  68.         print(self.timer)
  69.         if self.timer >= 3:
  70.             keys = pygame.key.get_pressed()
  71.             if keys[K_LEFT]:
  72.                 self.x -= self.pixels
  73.                 self.timer = 0
  74.             if keys[K_RIGHT]:
  75.                 self.x += self.pixels
  76.                 self.timer = 0
  77.             if keys[K_UP]:
  78.                 self.y -= self.pixels
  79.                 self.timer = 0
  80.             if keys[K_DOWN]:
  81.                 self.y += self.pixels
  82.                 self.timer = 0
  83.             if keys[K_SPACE]:
  84.                 self.timer = 0
  85.                 if self.idedit == 0:
  86.                     self.width = 20
  87.                     self.height = 20
  88.                     self.blockid = self.idedit
  89.                     blocklist.append(Edit(self.x, self.y, self.width, self.height, self.blockid))
  90.                 if self.idedit == 1:
  91.                     self.width = 20
  92.                     self.height = 1
  93.                     self.blockid = self.idedit
  94.                     blocklist.append(Edit(self.x, self.y, self.width, self.height, self.blockid))
  95.                 if self.idedit == 2 or 3 or 4 or 5:
  96.                     self.width = 20
  97.                     self.height = 20
  98.                     self.blockid = self.idedit
  99.                     blocklist.append(Edit(self.x, self.y, self.width, self.height, self.blockid))
  100.             if keys[K_w]:
  101.                 self.idedit += 1
  102.                 self.timer = 0
  103.             if keys[K_s]:
  104.                 self.idedit -= 1
  105.                 self.timer = 0
  106.             if keys[K_q]:
  107.                 with open('map.txt', 'w') as f:
  108.                     for lista in blocklist:
  109.                         f.write(str(lista.x) + " " + str(lista.y) + " " + str(lista.width) + " " + str(
  110.                             lista.height) + " " + str(lista.blockid) + "\n")
  111.                 self.timer = 0
  112.  
  113.     def editordraw(self):
  114.         if self.idedit == 0:
  115.             gameScreen.blit(SSplayer, [self.x, self.y])
  116.         if self.idedit == 1:
  117.             gameScreen.blit(SSgrass, [self.x, self.y])
  118.         if self.idedit == 2:
  119.             gameScreen.blit(SSwall, [self.x, self.y])
  120.         if self.idedit == 3:
  121.             gameScreen.blit(SSredblock, [self.x, self.y])
  122.         if self.idedit == 4:
  123.             gameScreen.blit(SSstar, [self.x, self.y])
  124.         if self.idedit == 5:
  125.             gameScreen.blit(SSfinish, [self.x, self.y])
  126.         for block in blocklist:
  127.             if block.blockid == 0:
  128.                 gameScreen.blit(SSplayer, [block.x, block.y])
  129.             if block.blockid == 1:
  130.                 gameScreen.blit(SSgrass, [block.x, block.y])
  131.             if block.blockid == 2:
  132.                 gameScreen.blit(SSwall, [block.x, block.y])
  133.             if block.blockid == 3:
  134.                 gameScreen.blit(SSredblock, [block.x, block.y])
  135.             if block.blockid == 4:
  136.                 gameScreen.blit(SSstar, [block.x, block.y])
  137.             if block.blockid == 5:
  138.                 gameScreen.blit(SSfinish, [block.x, block.y])
  139.  
  140.     def doall(self):
  141.         self.movement()
  142.         self.editordraw()
  143.  
  144.  
  145. # ===============================================
  146. # ===============================================
  147. # ============== CLASSES ========================
  148. # ===============================================
  149. # ========= WALLS/DOORS/FLOORS ==================
  150. class block(object):
  151.     def __init__(self, x, y, width, height):
  152.         self.x = x
  153.         self.y = y
  154.         self.width = width
  155.         self.height = height
  156.         self.hitbox = pygame.Rect(int(self.x), int(self.y), int(self.width), int(self.height))
  157.         self.visible = True
  158.  
  159.     def draw(self, win):
  160.         if self.visible:
  161.             self.hitbox = pygame.Rect(int(self.x), int(self.y), int(self.width), int(self.height))
  162.             pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
  163.  
  164.  
  165. # ===============================================
  166. # ===============================================
  167. game = Game()
  168. editor = Edit(300, 300, 0, 0, 0)
  169. bg1ground = block(-670, 648, 6510, 1)
  170. grounds = [bg1ground]
  171. BACKGROUND = (22, 34, 120)
  172.  
  173. while True:
  174.     edit = True
  175.     while edit:
  176.         for event in pygame.event.get():
  177.             if event.type == QUIT:
  178.                 pygame.QUIT()
  179.         keys = pygame.key.get_pressed()
  180.         if keys[K_f]:
  181.             with open('map.txt', 'r') as f:
  182.                 for lista in f:
  183.                     text = lista
  184.                     bufor = text.split(" ")
  185.                     blocklist.append(
  186.                         Edit(float(bufor[0]), float(bufor[1]), float(bufor[2]), float(bufor[3]), float(bufor[4])))
  187.             rungame = True
  188.             edit = False
  189.  
  190.     # ===================================================================================================================================================================
  191.     # ===================================================================================================================================================================
  192.     # =============================================================== MOTION AREA BELLOW ========================================================================
  193.     # ===================================================================================================================================================================
  194.     # ===================================================================================================================================================================
  195.     # ===============================================
  196.     # ===============================================
  197.     # =============== KEYS - LEFT ===================
  198.     # ===============================================
  199.     # ===============================================
  200.     if keys[pygame.K_LEFT] and man.x > man.vel:
  201.         man.x -= 10
  202.         for a in grounds:
  203.             if a.visible == True:
  204.                 a.x += editor.vel
  205.         if scrollingOn == False:
  206.             for e in blocklist:
  207.                 e.x = e.x
  208.             editor.left = True
  209.             editor.right = False
  210.  
  211.     # ===============================================
  212.     # ===============================================
  213.     # =============== KEYS - RIGHT ==================
  214.     # ===============================================
  215.     # ===============================================
  216.     elif keys[pygame.K_RIGHT] and man.x < 1340 - man.width - man.vel:
  217.         man.x += 10
  218.         for a in grounds:
  219.             if a.visible == True:
  220.                 a.x -= editor.vel
  221.         if scrollingOn == False:
  222.             for e in blocklist:
  223.                 e.x = e.x
  224.         else:
  225.             editor.right = True
  226.             editor.left = False
  227.  
  228.     # ===================================================================================================================================================================
  229.     # ===================================================================================================================================================================
  230.     # =============================================================== ENEMIES MOTION AREA ABOVE =========================================================================
  231.     # ===================================================================================================================================================================
  232.     # ===================================================================================================================================================================
  233.     # --------------------------------------------------------------
  234.     # ----------------- CAMERA ----------------------------------
  235.     # --------------------------------------------------------------
  236.     if bg1ground.x >= -4499 and bg1ground.x <= -1339:
  237.         scrollingOn = True
  238.  
  239.     if scrollingOn == True:
  240.         editor.x = 670
  241.         if keys[pygame.K_LEFT] and editor.x > editor.vel:
  242.             for a in platformsA:
  243.                 a.x += editor.vel
  244.             for e in enemies:
  245.                 e.x += editor.vel
  246.  
  247.         elif keys[pygame.K_RIGHT] and editor.x < 1340 - editor.width - editor.vel:
  248.             for a in platformsA:
  249.                 a.x -= editor.vel
  250.             for e in enemies:
  251.                 e.x -= editor.vel
  252.  
  253.     if bg1ground.x <= -4499 or bg1ground.x >= -1339:
  254.         scrollingOn = False
  255.  
  256.     if editor.x <= 5 and editor.left == True:
  257.         man.vel = 0
  258.     elif editor.x >= 1310 and editor.right == True:
  259.         editor.vel = 0
  260.     else:
  261.         editor.vel = 10
  262.         # --------------------------------------------------------------
  263.         # ----------------- CAMERA ----------------------------------
  264.         # --------------------------------------------------------------
  265.  
  266.         game.doall()
  267.         editor.doall()
  268.         CLOCK.tick(EDITFPS)
  269.         pygame.display.update()
  270.         gameScreen.fill(BACKGROUND)
  271.  
  272.     while rungame:
  273.         for event in pygame.event.get():
  274.             if event.type == QUIT:
  275.                 pygame.QUIT()
  276.         game.doall()
  277.         for lista in blocklist:
  278.             if lista.blockid == 0:
  279.                 gameScreen.blit(SSplayer, [lista.x, lista.y])
  280.             if lista.blockid == 1:
  281.                 gameScreen.blit(SSgrass, [lista.x, lista.y])
  282.             if lista.blockid == 2:
  283.                 gameScreen.blit(SSwall, [lista.x, lista.y])
  284.             if lista.blockid == 3:
  285.                 gameScreen.blit(SSredblock, [lista.x, lista.y])
  286.             if lista.blockid == 4:
  287.                 gameScreen.blit(SSstar, [lista.x, lista.y])
  288.             if lista.blockid == 5:
  289.                 gameScreen.blit(SSfinish, [lista.x, lista.y])
  290.  
  291.         CLOCK.tick(FPS)
  292.         pygame.display.update()
  293.         gameScreen.fill(BACKGROUND)
  294.