SHARE
TWEET

Untitled

a guest Feb 27th, 2020 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. from abc import ABC, abstractmethod
  3. from math import cos, sin, pi
  4.  
  5. import pygame
  6.  
  7. aqua      = (  0, 255, 255)   # морская волна
  8. black     = (  0,   0,   0)   # черный
  9. blue      = (  0,   0, 255)   # синий
  10. fuchsia   = (255,   0, 255)   # фуксия
  11. gray      = (128, 128, 128)   # серый
  12. green     = (  0, 128,   0)   # зеленый
  13. lime      = (  0, 255,   0)   # цвет лайма
  14. maroon    = (128,   0,   0)   # темно-бордовый
  15. navy_blue = (  0,   0, 128)   # темно-синий
  16. olive     = (128, 128,   0)   # оливковый
  17. purple    = (128,   0, 128)   # фиолетовый
  18. red       = (255,   0,   0)   # красный
  19. silver    = (192, 192, 192)   # серебряный
  20. teal      = (  0, 128, 128)   # зелено-голубой
  21. white     = (255, 255, 255)   # белый
  22. yellow    = (255, 255,   0)   # желтый
  23.  
  24. class Color:
  25.     RED = (255, 0, 0)
  26.     GREEN = (0, 255, 0)
  27.  
  28.  
  29. class PG:
  30.     _instance = None
  31.  
  32.     def __init__(self, width=500, height=400):
  33.         if PG._instance is not None:  # singleton implementation
  34.             raise ReferenceError('Tying to create singleton object twice')
  35.         else:
  36.             PG._instance = self
  37.         pygame.init()
  38.         self.screen = pygame.display.set_mode((width, height))
  39.         self.width = width
  40.         self.height = height
  41.  
  42.     @staticmethod
  43.     def get():
  44.         if not PG._instance:
  45.             PG()
  46.         return PG._instance
  47.  
  48.  
  49. class PGScene(ABC):
  50.     bgcolor = (0, 0, 0)
  51.  
  52.     def __init__(self):
  53.         self.screen = PG.get().screen
  54.         self.sceneover = False
  55.         self.objects = []
  56.  
  57.     def draw(self):
  58.         self.screen.fill(self.bgcolor)
  59.         for item in self.objects:
  60.             item.draw()
  61.  
  62.     def process_events(self):
  63.         for event in pygame.event.get():
  64.             if event.type == pygame.QUIT:
  65.                 self.sceneover = True
  66.             self.process_event(event)
  67.  
  68.     def process_event(self, event):
  69.         pass
  70.  
  71.  
  72.     @abstractmethod
  73.     def process_logic(self):
  74.         for item in self.objects:
  75.             item.logic()
  76.  
  77.     def main_loop(self):
  78.         while not self.sceneover:
  79.             self.process_events()
  80.             self.process_logic()
  81.             self.draw()
  82.             pygame.display.flip()
  83.             pygame.time.wait(10)
  84.  
  85.  
  86. class Command(ABC):
  87.     def __init__(self, scene):
  88.         self.scene = scene
  89.  
  90.     @abstractmethod
  91.     def execute(self, scene):
  92.         pass
  93.  
  94.  
  95. class UpCommand(Command):
  96.     def execute(self, scene):
  97.         self.scene.panzer.moveForward()
  98.         self.scene.buttons['up'].set_enabled(True)
  99.         self.scene.buttons['down'].set_enabled(False)
  100.         scene.text.text = "движение вперёд"
  101.  
  102.  
  103.  
  104. class DownCommand(Command):
  105.     def execute(self, scene):
  106.         self.scene.panzer.moveBackwards()
  107.         self.scene.buttons['down'].set_enabled(True)
  108.         self.scene.buttons['up'].set_enabled(False)
  109.         scene.text.text = "движение назад"
  110.  
  111.  
  112. class LeftCommand(Command):
  113.     def execute(self, scene):
  114.         self.scene.panzer.rotate(-0.05)
  115.         self.scene.buttons['left'].set_enabled(True)
  116.         self.scene.buttons['right'].set_enabled(False)
  117.         scene.text.text = "поворот влево"
  118.  
  119.  
  120. class RightCommand(Command):
  121.     def execute(self, scene):
  122.         self.scene.panzer.rotate(0.05)
  123.         self.scene.buttons['right'].set_enabled(True)
  124.         self.scene.buttons['left'].set_enabled(False)
  125.         scene.text.text = "поворот вправо"
  126.  
  127. class UpCommandReleased(Command):
  128.     def execute(self, scene):
  129.         self.scene.panzer.stop()
  130.         self.scene.buttons['up'].set_enabled(False)
  131.         scene.text.text = "нет движения"
  132.  
  133.  
  134. class DownCommandReleased(Command):
  135.     def execute(self, scene):
  136.         self.scene.panzer.stop()
  137.         self.scene.buttons['down'].set_enabled(False)
  138.         scene.text.text = "нет движения"
  139.  
  140.  
  141. class LeftCommandReleased(Command):
  142.     def execute(self, scene):
  143.         self.scene.panzer.stop_rotation()
  144.         self.scene.buttons['left'].set_enabled(False)
  145.         scene.text.text = "нет движения"
  146.  
  147.  
  148. class RightCommandReleased(Command):
  149.     def execute(self, scene):
  150.         self.scene.panzer.stop_rotation()
  151.         self.scene.buttons['right'].set_enabled(False)
  152.         scene.text.text = "нет движения"
  153.  
  154.  
  155. class GameScene(PGScene):
  156.     keydown_events = {
  157.         pygame.K_w: UpCommand,
  158.         pygame.K_s: DownCommand,
  159.         pygame.K_a: LeftCommand,
  160.         pygame.K_d: RightCommand,
  161.     }
  162.  
  163.     keyup_events = {
  164.         pygame.K_w: UpCommandReleased,
  165.         pygame.K_s: DownCommandReleased,
  166.         pygame.K_a: LeftCommandReleased,
  167.         pygame.K_d: RightCommandReleased,
  168.     }
  169.  
  170.     def __init__(self):
  171.         super().__init__()
  172.         self.panzer = Panzer(200, 200)
  173.         self.text = Text('нет движения', 50, 480)
  174.         self.buttons = {
  175.             'up': StateRect(100, 500, 20, 20, Color.GREEN, Color.RED),
  176.             'down': StateRect(100, 530, 20, 20, Color.GREEN, Color.RED),
  177.             'left': StateRect(70, 530, 20, 20, Color.GREEN, Color.RED),
  178.             'right': StateRect(130, 530, 20, 20, Color.GREEN, Color.RED),
  179.         }
  180.         self.objects.append(self.panzer)
  181.         self.objects.append(self.text)
  182.         self.objects += self.buttons.values()
  183.  
  184.     def process_logic(self):
  185.         super().process_logic()
  186.  
  187.     def process_event(self, event):
  188.         if event.type == pygame.KEYDOWN and event.key in GameScene.keydown_events.keys():
  189.             GameScene.keydown_events[event.key](self).execute(self)
  190.         if event.type == pygame.KEYUP and event.key in GameScene.keyup_events.keys():
  191.             GameScene.keyup_events[event.key](self).execute(self)
  192.  
  193.  
  194. class PGObject(ABC):
  195.     def __init__(self):
  196.         self.screen = PG.get().screen
  197.  
  198.     @abstractmethod
  199.     def draw(self):
  200.         pass
  201.  
  202.     def logic(self):
  203.         pass
  204.  
  205.  
  206. class Panzer(PGObject):
  207.     def __init__(self, x, y):
  208.         super().__init__()
  209.         self.rotated_image = self.image = pygame.image.load("panzer.png")
  210.         self.rotated_rect = self.rect = self.image.get_rect()
  211.         self.x = x
  212.         self.y = y
  213.         self.direction = 0
  214.         self.speed = 0
  215.         self.max_speed = 1
  216.         self.rotation_angle = 0
  217.  
  218.     def draw(self):
  219.         self.rect.x = self.x - self.rect.width // 2
  220.         self.rect.y = self.y - self.rect.height // 2
  221.         self.rotated_rect = self.rotated_image.get_rect(center=self.rect.center)
  222.         self.screen.blit(self.rotated_image, self.rotated_rect)
  223.  
  224.     def moveForward(self):
  225.         self.speed = self.max_speed
  226.  
  227.     def moveBackwards(self):
  228.         self.speed = -self.max_speed
  229.  
  230.     def stop(self):
  231.         self.speed = 0
  232.  
  233.     def step(self):
  234.         self.x += cos(self.direction) * self.speed
  235.         self.y += sin(self.direction) * self.speed
  236.         self.direction += self.rotation_angle
  237.         self.rotated_image = pygame.transform.rotate(self.image, -self.direction / pi * 180)
  238.         self.rotated_rect = self.rotated_image.get_rect(center=self.rect.center)
  239.  
  240.     def rotate(self, angle):
  241.         self.rotation_angle = angle
  242.  
  243.     def stop_rotation(self):
  244.         self.rotation_angle = 0
  245.  
  246.     def logic(self):
  247.         self.step()
  248.  
  249.  
  250. class StateRect(PGObject):
  251.     def __init__(self, x, y, width, height, color_enabled, color_disabled):
  252.         super().__init__()
  253.         self.rect = (x, y, width, height)
  254.         self.color_enabled = color_enabled
  255.         self.color_disabled = color_disabled
  256.         self.enabled = False
  257.  
  258.     def draw(self):
  259.         color = self.color_enabled if self.enabled else self.color_disabled,
  260.         pygame.draw.rect(self.screen, color, self.rect, 0)
  261.  
  262.     def set_enabled(self, enabled):
  263.         self.enabled = enabled
  264.  
  265. class Text(PGObject):
  266.     def __init__(self, text, x, y):
  267.         super().__init__()
  268.         self.x = x
  269.         self.y = y
  270.         self.text = text        
  271.  
  272.     def draw(self):
  273.         font = pygame.font.Font(None, 20)
  274.         text = font.render(self.text, True, white)
  275.         self.screen.blit(text, [self.x,self.y])
  276.  
  277.  
  278. if __name__ == "__main__":
  279.     PG(800, 600)
  280.     gs = GameScene()
  281.     gs.main_loop()
  282.     sys.exit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top