Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import pygame
  2.  
  3. SCREEN_SIZE = (400, 300)
  4. CLOCK = 60
  5.  
  6. class Directions:
  7.   NORD = UP = 0
  8.   SUD = DOWN = 1
  9.   WEST = LEFT = 2
  10.   EST = RIGHT = 3
  11.  
  12. class Entity:
  13.   def __init__(self, x, y, w = 10, h = 10, color=(255,0,0)):
  14.     self.x = x
  15.     self.y = y
  16.     self.w = w
  17.     self.h = h
  18.     self.color = color
  19.     self.speed = 1
  20.  
  21.   def draw(self, surface):
  22.     pygame.draw.rect(surface, self.color, (self.x, self.y, self.w, self.h))
  23.  
  24.   def update(self):
  25.     pass
  26.  
  27.   def destroy(self):
  28.     entities.remove(self)
  29.  
  30.   def move(self, direction):
  31.     if direction == Directions.UP:
  32.       self.y -= self.speed
  33.     elif direction == Directions.DOWN:
  34.       self.y += self.speed
  35.     elif direction == Directions.LEFT and self.x > 0:
  36.       self.x -= self.speed
  37.     elif direction == Directions.RIGHT and self.x < SCREEN_SIZE[0] - self.w:
  38.       self.x += self.speed
  39.  
  40.  
  41.  
  42. class Player(Entity):
  43.   def __init__(self):
  44.     w = 10
  45.     h = 10
  46.     x = SCREEN_SIZE[0] // 2 - w // 2
  47.     y = SCREEN_SIZE[1] - h - 10
  48.  
  49.     Entity.__init__(self, x, y, w, h, (0, 255, 0))
  50.  
  51.     self.__fire_counter = 0
  52.  
  53.   def fire(self):
  54.     entities.append(Bullet(self.x, self.y - self.w))
  55.  
  56.   def update(self):
  57.     if self.__fire_counter == 0:
  58.       self.fire()
  59.    
  60.     self.__fire_counter += 1
  61.    
  62.     if self.__fire_counter == CLOCK//2:
  63.       self.__fire_counter = 0
  64.  
  65.  
  66. class Bullet(Entity):
  67.   def __init__(self, x, y):
  68.     Entity.__init__(self, x, y, color = (255, 0, 0))
  69.     self.speed = 1
  70.  
  71.   def update(self):
  72.     self.move(Directions.UP)
  73.     if self.y < -self.h:
  74.       self.destroy()
  75.  
  76.     for e in entities:
  77.       if e != self and e != player and type(e) != Bullet:
  78.         if self.collide(e):
  79.           self.destroy()
  80.           e.destroy()
  81.           break
  82.  
  83.   def collide(self, entity):
  84.      #and (self.y >= entity.y and self.y <= entity.y + entity.h)
  85.     return self.x >= entity.x
  86.  
  87. class Enemy(Entity):
  88.   def __init__(self, x, y):
  89.     Entity.__init__(self, x, y, color = (255, 0, 255))
  90.  
  91.  
  92. pygame.init()
  93. screen = pygame.display.set_mode(SCREEN_SIZE)
  94. done = False
  95.  
  96. clock = pygame.time.Clock()
  97. player = Player()
  98. entities = [player, Enemy(0,0)]
  99.  
  100. while not done:
  101.   pygame.draw.rect(screen, (0, 0, 0), (0, 0, *SCREEN_SIZE))
  102.  
  103.   for event in pygame.event.get():
  104.     if event.type == pygame.QUIT:
  105.       done = True
  106.  
  107.   keys = pygame.key.get_pressed()
  108.   if keys[pygame.K_LEFT]:
  109.     player.move(Directions.LEFT)
  110.   elif keys[pygame.K_RIGHT]:
  111.     player.move(Directions.RIGHT)
  112.  
  113.   for e in entities:
  114.     e.draw(screen)
  115.     e.update()
  116.  
  117.   pygame.display.flip()
  118.  
  119.   clock.tick(CLOCK)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement