Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame import Vector2, image
- from pygame.display import update
- from pygame.event import post
- from pygame.transform import rotate
- class player(pygame.sprite.Sprite):
- def __init__(self) -> None:
- super().__init__()
- self.originalimage = pygame.image.load("manBlue_gun.png").convert_alpha()
- self.originalimage = pygame.transform.scale(self.originalimage,(length,length))
- self.image = self.originalimage
- self.rect = self.image.get_rect()
- self.rect.center = 200,200
- self.rotation = 0
- self.move = 0,0
- def update(self):
- keys = pygame.key.get_pressed()
- self.rotationspeed = 0
- if keys[pygame.K_d]:
- self.rotationspeed = -3
- if keys[pygame.K_a]:
- self.rotationspeed = 3
- self.rotation = (self.rotation + self.rotationspeed)%360
- self.image = pygame.transform.rotate(self.originalimage,self.rotation)
- position = self.rect.center
- self.rect = self.image.get_rect()
- self.rect.center = position
- dx,dy = 0,0
- if keys[pygame.K_w]:
- dx,dy = pygame.Vector2(5,0).rotate(-self.rotation)
- if keys[pygame.K_s]:
- dx,dy = pygame.Vector2(-5,0).rotate(-self.rotation)
- if not check_collision(self.rect, (dx,dy)):
- pass
- elif not check_collision(self.rect, (dx, 0)):
- dy = 0
- elif not check_collision(self.rect, (0, dy)):
- dx = 0
- else:
- dx,dy = 0,0
- self.move = dx, dy
- self.rect.move_ip(dx,dy)
- class enemies(pygame.sprite.Sprite):
- def __init__(self, c, r):
- 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.row = r
- self.column = c
- self.rect.topleft = (self.column*length, self.row*length)
- self.rot = 0
- self.position = pygame.Vector2(self.rect.center)
- def update(self):
- self.rot = (p1.rect.center - self.position).angle_to(pygame.Vector2(1,0))
- self.image = pygame.transform.rotate(self.i, self.rot)
- pos = self.rect.center
- self.rect = self.image.get_rect()
- self.rect.center = pos
- self.move = Vector2(5, 0).rotate(-self.rot)
- dx, dy = self.move
- if not check_collision(self.rect, (dx,dy)):
- pass
- elif not check_collision(self.rect, (dx, 0)):
- dy = 0
- elif not check_collision(self.rect, (0, dy)):
- dx = 0
- else:
- dx,dy = 0,0
- self.rect.move_ip(dx,dy)
- self.position = pygame.Vector2(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.move = pygame.Vector2(20, 0).rotate(-angle)
- self.rect.center = pos
- def update(self):
- self.rect.move_ip(self.move)
- class wall(pygame.sprite.Sprite):
- def __init__(self, c, r) -> None:
- super().__init__()
- 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)
- 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
- r, c = 0,0
- with open ("map.txt", "r") as file:
- for r, row in enumerate(file):
- for c, element in enumerate(row):
- if element == "1":
- w = wall(c, r)
- walls.append(w)
- elif element == "M":
- 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 updatecamera(move, player):
- for e in move:
- e.rect.move_ip(-player.move[0], -player.move[1])
- pygame.init()
- width = 800
- height = 600
- length = 50
- window = pygame.display.set_mode((width,height))
- clock = pygame.time.Clock()
- element = pygame.sprite.Group()
- wall_grp = pygame.sprite.Group()
- bullet_grp = pygame.sprite.Group()
- enemies_grp = pygame.sprite.Group()
- allelements = pygame.sprite.Group()
- p1 = player()
- element.add(p1)
- allelements.add(p1)
- walls = []
- run = True
- mapsize = setupmap()
- for w in walls:
- wall_grp.add(w)
- allelements.add(w)
- while p1.rect.centerx < width//2:
- for e in allelements:
- e.rect.move_ip(1, 0)
- while run == True:
- clock.tick(60)
- window.fill((130,140,190))
- 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(p1.rotation, p1.rect.center)
- bullet_grp.add(b)
- allelements.add(b)
- pygame.sprite.groupcollide(bullet_grp, enemies_grp, True, True)
- pygame.sprite.groupcollide(bullet_grp, wall_grp, True, False)
- updatecamera(allelements, p1)
- # element.update()
- # element.draw(window)
- allelements.update()
- allelements.draw(window)
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement