Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- SCREEN_SIZE = (400, 300)
- CLOCK = 60
- class Directions:
- NORD = UP = 0
- SUD = DOWN = 1
- WEST = LEFT = 2
- EST = RIGHT = 3
- class Entity:
- def __init__(self, x, y, w = 10, h = 10, color=(255,0,0)):
- self.x = x
- self.y = y
- self.w = w
- self.h = h
- self.color = color
- self.speed = 1
- def draw(self, surface):
- pygame.draw.rect(surface, self.color, (self.x, self.y, self.w, self.h))
- def update(self):
- pass
- def destroy(self):
- entities.remove(self)
- def move(self, direction):
- if direction == Directions.UP:
- self.y -= self.speed
- elif direction == Directions.DOWN:
- self.y += self.speed
- elif direction == Directions.LEFT and self.x > 0:
- self.x -= self.speed
- elif direction == Directions.RIGHT and self.x < SCREEN_SIZE[0] - self.w:
- self.x += self.speed
- class Player(Entity):
- def __init__(self):
- w = 10
- h = 10
- x = SCREEN_SIZE[0] // 2 - w // 2
- y = SCREEN_SIZE[1] - h - 10
- Entity.__init__(self, x, y, w, h, (0, 255, 0))
- self.__fire_counter = 0
- def fire(self):
- entities.append(Bullet(self.x, self.y - self.w))
- def update(self):
- if self.__fire_counter == 0:
- self.fire()
- self.__fire_counter += 1
- if self.__fire_counter == CLOCK//2:
- self.__fire_counter = 0
- class Bullet(Entity):
- def __init__(self, x, y):
- Entity.__init__(self, x, y, color = (255, 0, 0))
- self.speed = 1
- def update(self):
- self.move(Directions.UP)
- if self.y < -self.h:
- self.destroy()
- for e in entities:
- if e != self and e != player and type(e) != Bullet:
- if self.collide(e):
- self.destroy()
- e.destroy()
- break
- def collide(self, entity):
- #and (self.y >= entity.y and self.y <= entity.y + entity.h)
- return self.x >= entity.x
- class Enemy(Entity):
- def __init__(self, x, y):
- Entity.__init__(self, x, y, color = (255, 0, 255))
- pygame.init()
- screen = pygame.display.set_mode(SCREEN_SIZE)
- done = False
- clock = pygame.time.Clock()
- player = Player()
- entities = [player, Enemy(0,0)]
- while not done:
- pygame.draw.rect(screen, (0, 0, 0), (0, 0, *SCREEN_SIZE))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT]:
- player.move(Directions.LEFT)
- elif keys[pygame.K_RIGHT]:
- player.move(Directions.RIGHT)
- for e in entities:
- e.draw(screen)
- e.update()
- pygame.display.flip()
- clock.tick(CLOCK)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement