Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.21 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. import os
  4. import random
  5. from random import randint
  6.  
  7. # consts for window
  8. width = 1000
  9. height = 600
  10. fps = 60  # frames per second
  11.  
  12. # define basic colors
  13. white = (255, 255, 255)
  14. black = (0, 0, 0)
  15. red = (255, 0, 0)
  16. green = (0, 255, 0)
  17. blue = (0, 0, 255)
  18. purple = (230, 76, 153)
  19.  
  20. # set up assets folders
  21. game_folder = os.path.dirname(__file__)
  22. img_folder = os.path.join(game_folder, "img")
  23.  
  24.  
  25. class Background(pygame.sprite.Sprite):
  26.     def __init__(self):
  27.         pygame.sprite.Sprite.__init__(self)
  28.         self.image = pygame.image.load(os.path.join(img_folder, "background.png"))
  29.         self.rect = self.image.get_rect()
  30.         self.rect.left = 0
  31.         self.rect.top = 0
  32.  
  33.  
  34. class Maze(pygame.sprite.Sprite):
  35.     def __init__(self):
  36.         self.W = 25
  37.         self.H = 14
  38.         self.maze = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39.                      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  40.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  41.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
  42.                      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  43.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  44.                      1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  45.                      1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  46.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  47.                      1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  48.                      1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
  49.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1,
  50.                      1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
  51.                      1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ]
  52.     def draw(self,screen):
  53.         block_surf = pygame.image.load(os.path.join(img_folder, "new.png")).convert()
  54.         bx = 0
  55.         by = 0
  56.         placex = []
  57.         placey = []
  58.         for i in range(0,self.W*self.H):
  59.             if self.maze[ bx + (by*self.W) ] == 1:
  60.                 block = screen.blit(block_surf,( bx * 40 , by * 40))
  61.             else:
  62.                 placex.append(bx * 10)
  63.                 placey.append(by * 10)
  64.             bx = bx + 1
  65.             if bx > self.W-1:
  66.                 bx = 0
  67.                 by = by + 1
  68.     def collisions(self):
  69.         a = 0
  70.         b = 0
  71.         player = Player()
  72.         for i in range(0, self.W * self.H):
  73.             if self.maze[ a + (b * self.W) ] == 1:
  74.                 blocks = pygame.sprite.Group()
  75.                 rect = pygame.Rect(a * 40, b * 40, 40, 40)
  76.                 blocks.add(rect)
  77.                
  78.  
  79.                 if spritecollideany(player, blocks, collided = None) == None:
  80.                     break  # Collision!
  81.                     print("Kolizja")
  82.                     return False
  83.  
  84.                 a = a + 1
  85.                 if a > self.W-1:
  86.                     a = 0
  87.                     b = b + 1
  88. class Player(pygame.sprite.Sprite):
  89.     def __init__(self): #sprite for a player
  90.         pygame.sprite.Sprite.__init__(self) #for sprite working
  91.         self.image = pygame.image.load(os.path.join(img_folder, "player.png")).convert() #look of the sprite
  92.         self.image.set_colorkey(white) #to delete black things around rect img
  93.         self.rect = self.image.get_rect() #kind of border around it
  94.         self.rect.centerx = width/2 + 40
  95.         self.rect.bottom = height- 120
  96.         self.speedx = 0
  97.         self.speedy = 0
  98.     def update(self): #moving player
  99.         maze = Maze()
  100.         #placex,placey = maze.draw(screen)
  101.         #collisions = maze.collisions
  102.         self.speedx = 0
  103.         self.speedy = 0
  104.         keystate = pygame.key.get_pressed()
  105.         if keystate[pygame.K_LEFT]:
  106.             collisions = maze.collisions
  107.             if collisions == False:
  108.                 self.speedx = 0
  109.             else:
  110.                 self.speedx = -1
  111.         elif keystate[pygame.K_RIGHT]:
  112.             self.speedx = 1
  113.         elif keystate[pygame.K_UP]:
  114.             self.speedy = -1
  115.         elif keystate[pygame.K_DOWN]:
  116.             self.speedy = 1
  117.         self.rect.x += self.speedx
  118.         self.rect.y += self.speedy
  119.         #print(self.rect.x, self.rect.y)
  120.     def get_rect(self):
  121.         return pygame.Rect(self.rect.x, self.rect.y, 40, 40)
  122.  
  123. class Candy(pygame.sprite.Sprite):
  124.     def __init__(self):
  125.         pygame.sprite.Sprite.__init__(self) #for sprite working
  126.         self.liczba = randint(0,100)
  127.         #print(self.liczba)
  128.         if self.liczba % 2 == 0:
  129.             self.image = pygame.image.load(os.path.join(img_folder,"candy1.png")).convert() #look of the sprite
  130.         if self.liczba % 3 == 0:
  131.             self.image = pygame.image.load(os.path.join(img_folder,"candy2.png")).convert()
  132.         else:
  133.             self.image = pygame.image.load(os.path.join(img_folder,"candy3.png")).convert()
  134.         self.image.set_colorkey(black) #to delete black things around rect img
  135.         self.rect = self.image.get_rect() #kind of border around it
  136.         maze = Maze()
  137.         #placex, placey = maze.draw(screen)
  138.         #self.choose = randint(0,len(placex))
  139.         #self.rect.centerx = placex[self.choose]
  140.         #self.rect.bottom = placey[self.choose]
  141.  
  142.  
  143.  
  144. # initialize pygame and create window
  145. pygame.init() #start pygame
  146. pygame.mixer.init() #start music
  147. # screen = pygame.display.set_mode((1000, 520)
  148. screen = pygame.display.set_mode((width,height))
  149. pygame.display.set_caption("Candy Maze") #name of my window
  150. clock = pygame.time.Clock()
  151.  
  152. # display_surf = None
  153. image_surf = None
  154. block_surf = pygame.image.load(os.path.join(img_folder, "new.png")).convert()
  155.  
  156. BackGround = Background()
  157. MaZe = Maze()
  158. all_sprites = pygame.sprite.Group() #group of sprites
  159. player = Player()
  160. all_sprites.add(player)
  161. candy = Candy()
  162. all_sprites.add(candy)
  163.  
  164. #text
  165. font = pygame.font.Font('candyshop.ttf', 26)
  166. text = font.render('Level One',True,purple,white)
  167. textRect = text.get_rect()
  168. textRect.center = (width // 2, 585)
  169.  
  170. # main game loop
  171. running = True
  172. while running:
  173.     # keep loop running at the right speed
  174.     clock.tick(fps) #speed of the loop
  175.     # events
  176.     for event in pygame.event.get():
  177.         # check for closing the window
  178.         if event.type == pygame.QUIT:
  179.             running = False
  180.         elif event.type == KEYDOWN:
  181.             if event.key == K_ESCAPE:
  182.                 pygame.quit()
  183.     # update
  184.     MaZe.collisions
  185.     all_sprites.update()
  186.     # draw/render
  187.     # screen.fill(white)
  188.     screen.blit(BackGround.image, BackGround.rect) #draw background
  189.     # display_surf.blit(image_surf,(self.player.x,self.player.y))
  190.     MaZe.draw(screen)
  191.     all_sprites.draw(screen)
  192.     screen.blit(text, textRect)
  193.     # after drawing everything flip the display
  194.     pygame.display.flip() #quicker process to draw things
  195. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement