Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- import os
- import random
- from random import randint
- # consts for window
- width = 1000
- height = 600
- fps = 60 # frames per second
- # define basic colors
- white = (255, 255, 255)
- black = (0, 0, 0)
- red = (255, 0, 0)
- green = (0, 255, 0)
- blue = (0, 0, 255)
- purple = (230, 76, 153)
- # set up assets folders
- game_folder = os.path.dirname(__file__)
- img_folder = os.path.join(game_folder, "img")
- class Background(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.image.load(os.path.join(img_folder, "background.png"))
- self.rect = self.image.get_rect()
- self.rect.left = 0
- self.rect.top = 0
- class Maze(pygame.sprite.Sprite):
- def __init__(self):
- self.W = 25
- self.H = 14
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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,
- 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, ]
- def draw(self,screen):
- block_surf = pygame.image.load(os.path.join(img_folder, "new.png")).convert()
- bx = 0
- by = 0
- placex = []
- placey = []
- for i in range(0,self.W*self.H):
- if self.maze[ bx + (by*self.W) ] == 1:
- block = screen.blit(block_surf,( bx * 40 , by * 40))
- else:
- placex.append(bx * 10)
- placey.append(by * 10)
- bx = bx + 1
- if bx > self.W-1:
- bx = 0
- by = by + 1
- def collisions(self):
- a = 0
- b = 0
- player = Player()
- for i in range(0, self.W * self.H):
- if self.maze[ a + (b * self.W) ] == 1:
- blocks = pygame.sprite.Group()
- rect = pygame.Rect(a * 40, b * 40, 40, 40)
- blocks.add(rect)
- if spritecollideany(player, blocks, collided = None) == None:
- break # Collision!
- print("Kolizja")
- return False
- a = a + 1
- if a > self.W-1:
- a = 0
- b = b + 1
- class Player(pygame.sprite.Sprite):
- def __init__(self): #sprite for a player
- pygame.sprite.Sprite.__init__(self) #for sprite working
- self.image = pygame.image.load(os.path.join(img_folder, "player.png")).convert() #look of the sprite
- self.image.set_colorkey(white) #to delete black things around rect img
- self.rect = self.image.get_rect() #kind of border around it
- self.rect.centerx = width/2 + 40
- self.rect.bottom = height- 120
- self.speedx = 0
- self.speedy = 0
- def update(self): #moving player
- maze = Maze()
- #placex,placey = maze.draw(screen)
- #collisions = maze.collisions
- self.speedx = 0
- self.speedy = 0
- keystate = pygame.key.get_pressed()
- if keystate[pygame.K_LEFT]:
- collisions = maze.collisions
- if collisions == False:
- self.speedx = 0
- else:
- self.speedx = -1
- elif keystate[pygame.K_RIGHT]:
- self.speedx = 1
- elif keystate[pygame.K_UP]:
- self.speedy = -1
- elif keystate[pygame.K_DOWN]:
- self.speedy = 1
- self.rect.x += self.speedx
- self.rect.y += self.speedy
- #print(self.rect.x, self.rect.y)
- def get_rect(self):
- return pygame.Rect(self.rect.x, self.rect.y, 40, 40)
- class Candy(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self) #for sprite working
- self.liczba = randint(0,100)
- #print(self.liczba)
- if self.liczba % 2 == 0:
- self.image = pygame.image.load(os.path.join(img_folder,"candy1.png")).convert() #look of the sprite
- if self.liczba % 3 == 0:
- self.image = pygame.image.load(os.path.join(img_folder,"candy2.png")).convert()
- else:
- self.image = pygame.image.load(os.path.join(img_folder,"candy3.png")).convert()
- self.image.set_colorkey(black) #to delete black things around rect img
- self.rect = self.image.get_rect() #kind of border around it
- maze = Maze()
- #placex, placey = maze.draw(screen)
- #self.choose = randint(0,len(placex))
- #self.rect.centerx = placex[self.choose]
- #self.rect.bottom = placey[self.choose]
- # initialize pygame and create window
- pygame.init() #start pygame
- pygame.mixer.init() #start music
- # screen = pygame.display.set_mode((1000, 520)
- screen = pygame.display.set_mode((width,height))
- pygame.display.set_caption("Candy Maze") #name of my window
- clock = pygame.time.Clock()
- # display_surf = None
- image_surf = None
- block_surf = pygame.image.load(os.path.join(img_folder, "new.png")).convert()
- BackGround = Background()
- MaZe = Maze()
- all_sprites = pygame.sprite.Group() #group of sprites
- player = Player()
- all_sprites.add(player)
- candy = Candy()
- all_sprites.add(candy)
- #text
- font = pygame.font.Font('candyshop.ttf', 26)
- text = font.render('Level One',True,purple,white)
- textRect = text.get_rect()
- textRect.center = (width // 2, 585)
- # main game loop
- running = True
- while running:
- # keep loop running at the right speed
- clock.tick(fps) #speed of the loop
- # events
- for event in pygame.event.get():
- # check for closing the window
- if event.type == pygame.QUIT:
- running = False
- elif event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- pygame.quit()
- # update
- MaZe.collisions
- all_sprites.update()
- # draw/render
- # screen.fill(white)
- screen.blit(BackGround.image, BackGround.rect) #draw background
- # display_surf.blit(image_surf,(self.player.x,self.player.y))
- MaZe.draw(screen)
- all_sprites.draw(screen)
- screen.blit(text, textRect)
- # after drawing everything flip the display
- pygame.display.flip() #quicker process to draw things
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement