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, 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
- for i in range(0,self.W*self.H):
- if self.maze[ bx + (by*self.W) ] == 1:
- screen.blit(block_surf,( bx * 40 , by * 40))
- bx = bx + 1
- if bx > self.W-1:
- bx = 0
- by = by + 1
- def collisions(self):
- bx = 0
- by = 0
- for i in range(0, self.W * self.H):
- if self.maze[ bx + (by * self.W) ] == 1:
- rect = pygame.Rect(bx * 40, by * 40, 40, 40)
- if rect.colliderect(Player.get_rect):
- pass # Collision!
- return False
- bx = bx + 1
- if bx > self.W-1:
- bx = 0
- by = by + 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()
- collisions = maze.collisions
- self.speedx = 0
- self.speedy = 0
- keystate = pygame.key.get_pressed()
- if keystate[pygame.K_LEFT]:
- if collisions == False:
- self.speedx = 0
- else:
- self.speedx = -1
- elif keystate[pygame.K_RIGHT]:
- if self.rect.x > 920:
- self.speedx = 0
- # elif self.rect.y <= 636 and self.rect.y > 580:
- # self.speedy = 0
- elif self.rect.x == 40 and self.rect.y < 482 and self.rect.y > 401:
- self.speedy = 0
- elif self.rect.x == 40 and self.rect.y < 399 and self.rect.y > 282:
- self.speedy = 0
- elif self.rect.x == 40 and self.rect.y < 279 and self.rect.y > 160:
- self.speedy = 0
- elif self.rect.x == 40 and self.rect.y < 157 and self.rect.y > 41:
- self.speedy = 0
- else:
- self.speedx = 1
- elif keystate[pygame.K_UP]:
- if self.rect.y < 41:
- self.speedy = 0
- elif self.rect.y == 159 and self.rect.x > 46 and self.rect.x < 160:
- self.speedy = 0
- elif self.rect.y == 281 and self.rect.x > 45 and self.rect.x < 154:
- self.speedy = 0
- else:
- self.speedy = -1
- elif keystate[pygame.K_DOWN]:
- if self.rect.y > 480:
- self.speedy = 0
- elif self.rect.x > 405 and self.rect.x < 634 and self.rect.y > 440:
- self.speedy = 0
- elif self.rect.y == 280 and self.rect.x > 46 and self.rect.x < 155:
- self.speedy = 0
- else:
- self.speedy = 1
- self.rect.x += self.speedx
- self.rect.y += self.speedy
- def get_rect(self):
- return pygame.Rect(self.rect.x, self.rect.y, 40, 40)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement