Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- vector = pygame.math.Vector2
- BackgroundColor = 0
- playercolor = 0
- length = 60
- walls = []
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.i = pygame.image.load("manBlue_gun.png").convert_alpha()
- self.i = pygame.transform.scale(self.i, (length,length))
- self.image = self.i
- self.rect = self.image.get_rect()
- self.rect.topleft = vector(length, length)
- self.rot = 0
- self.position = vector(length, length)
- self.move = (0,0)
- def update(self):
- keys = pygame.key.get_pressed()
- if keys[pygame.K_RIGHT]:
- self.rot = (self.rot + 10) % 360
- print("Rotating right")
- # self.image = pygame.transform.rotate(self.i, self.rot)
- elif keys[pygame.K_LEFT]:
- self.rot = (self.rot - 10) % 360
- print("Rotating left")
- self.image = pygame.transform.rotate(self.i, self.rot)
- pos = self.rect.center
- self.rect = self.image.get_rect()
- self.rect.center = pos
- # self.rect.move_ip(0,0)
- self.move = (0, 0)
- if keys[pygame.K_UP]:
- self.move = vector(17, 0).rotate(-self.rot)
- elif keys[pygame.K_DOWN]:
- self.move = vector(-17, 0).rotate(-self.rot)
- if not check_collision(self.rect, self.move):
- self.rect.move_ip(self.move)
- else:
- self.move = (0,0)
- self.position = vector(self.rect.center)
- class bullets(pygame.sprite.Sprite):
- def __init__(self, angle, pos):
- super().__init__()
- self.image = pygame.Surface((10, 10))
- self.image.fill((0,255,0))
- self.rect = self.image.get_rect()
- self.bulletspeed = 0
- self.vector = vector(20, 0).rotate(-angle)
- self.rect.center = pos
- def update(self):
- self.rect.move_ip(self.vector)
- class wall(pygame.sprite.Sprite):
- def __init__(self, c, r):
- super().__init__()
- # print(c, r)
- self.image = pygame.Surface((length, length))
- self.image.fill((0, 125, 125))
- self.rect = self.image.get_rect()
- self.row = r
- self.column = c
- self.rect.topleft = (self.column*length, self.row*length)
- class enemies(pygame.sprite.Sprite):
- def __init__(self, c, r):
- super().__init__()
- self.i = pygame.image.load("robot1_hold.png").convert_alpha()
- self.i = pygame.transform.scale(self.i, (length, length))
- self.image = self.i
- self.image.fill((0,255,0))
- self.rect = self.image.get_rect()
- self.row = r
- self.column = c
- self.rect.topleft = (self.column*length, self.row*length)
- self.rot = 0
- # def update(self):
- def check_collision(rect, movement):
- new_pos_rect = rect.move(movement)
- for w in walls:
- if w.rect.colliderect(new_pos_rect):
- return True
- return False
- def setupmap():
- global walls
- with open("map.txt", "r") as file:
- for r, row in enumerate(file):
- for c, element in enumerate(row):
- if element == "1":
- print(c, r)
- w = wall(c, r)
- walls.append(w)
- elif element == "M":
- print(c, r)
- e = enemies(c, r)
- enemies_grp.add(e)
- allelements.add(e)
- total_rows = r + 1
- total_columns = c + 1
- return total_rows, total_columns
- def update_camera(camera, player, mapwidth, mapheight):
- camera.x = player.position.x - (WIDTH // 2)
- camera.y = player.position.y - (HEIGHT // 2)
- # if camera.x < 0:
- camera.x = player.move[0]
- # elif camera.x > mapwidth - WIDTH:
- # camera.x = mapwidth - WIDTH
- # if camera.y < 0:
- camera.y = player.move[1]
- # elif camera.y > mapheight - HEIGHT:
- # camera.y = mapheight - HEIGHT
- # print("camera", camera.x)
- # print("Player position", player.position.x)
- # for e in elements:
- # e.rect.move_ip(-camera.x, -camera.y)
- # print("Move")
- # def apply_camera(camera, element):
- # rect = element.rect.move(-camera.x, -camera.y)
- # return rect
- WIDTH, HEIGHT = 800, 600
- pygame.init()
- window = pygame.display.set_mode((800, 600))
- camera = pygame.Rect(-400, -300, WIDTH, HEIGHT)
- run = True
- clock = pygame.time.Clock()
- elements = pygame.sprite.Group()
- allelements = pygame.sprite.Group()
- wall_grp = pygame.sprite.Group()
- enemies_grp = pygame.sprite.Group()
- mapsize = setupmap()
- for w in walls:
- wall_grp.add(w)
- allelements.add(w)
- player = Player()
- elements.add(player)
- allelements.add(player)
- while run:
- clock.tick(30)
- window.fill(BackgroundColor)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- b = bullets(player.rot, player.rect.center)
- elements.add(b)
- allelements.add(b)
- update_camera(camera, player, mapsize[1]*length, mapsize[0]*length)
- for e in allelements:
- # print("camera x,y ", camera.x, camera.y)
- e.rect.move_ip(-camera.x, -camera.y)
- # window.blit(e.image, apply_camera(camera, e))
- # for e in enemies_grp:
- # # e.rect.move_ip(-camera.x, -camera.y)
- # window.blit(e.image, apply_camera(camera, e))
- # for e in elements:
- # # e.rect.move_ip(-camera.x, -camera.y)
- # window.blit(e.image, apply_camera(camera, e))
- # enemies_grp.update()
- # enemies_grp.draw(window)
- # wall_grp.update()
- # wall_grp.draw(window)
- allelements.update()
- allelements.draw(window)
- pygame.display.update()
- pygame.quit()
Add Comment
Please, Sign In to add comment