Advertisement
Xit700

Untitled

May 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.13 KB | None | 0 0
  1. import pygame
  2. import math
  3.  
  4. class Vec2:
  5.     def __init__(self, x = 0, y = 0):
  6.         self.x, self.y = x, y
  7.  
  8.     def __add__(self, v):
  9.         return Vec2(self.x + v.x, self.y + v.y)
  10.  
  11.     def __sub__(self, v):
  12.         return Vec2(self.x - v.x, self.y - v.y)
  13.  
  14.     def __mul__(self, alpha):
  15.         return Vec2(self.x * alpha, self.y * alpha)
  16.  
  17.     def __rmul__(self, alpha):
  18.         return Vec2(self.x * alpha, self.y * alpha)
  19.  
  20.     def intpair(self):
  21.         return (int(self.x), int(self.y))
  22.  
  23.     def len(self):
  24.         return math.sqrt(self.x * self.x + self.y * self.y)
  25.  
  26. class Rect:
  27.     def __init__(self, left, top, right, bottom):
  28.         self.left, self.top, self.right, self.bottom = left, top, right, bottom
  29.  
  30. class Frame:
  31.     def __init__(self, pressed, dt):
  32.         self.pressed = pressed
  33.         self.dt = dt
  34.  
  35. class Pole:
  36.     def __init__(self, screen):
  37.         self.screen = screen
  38.  
  39.     def clear(self):
  40.         self.screen.fill((50, 120, 90))
  41.  
  42.     def circle(self, color, pos, radius):
  43.         pygame.draw.circle(self.screen, color, pos, radius)
  44.  
  45. class Player:
  46.     def __init__(self, pos, rect, a = 500, radius = 20, local = False):
  47.         """Constructor of Player class
  48.        self.a - acceleration coef.
  49.        self.r - radius
  50.        """
  51.  
  52.         self.pos, self.a, self.r, self.local = Vec2(*pos), a, radius, local
  53.         self.pressed = {pygame.K_d: 0, pygame.K_a: 0, pygame.K_s: 0, pygame.K_w: 0}
  54.         self.rect = rect
  55.         self.v = Vec2()
  56.         self.refresh_color()
  57.  
  58.     def refresh_color(self):
  59.         """Set color (it depens on the module of Player speed)"""
  60.         self.color1 = min(150, int(self.v.len()) + 50)
  61.         self.color2 = min(255, int(self.v.len()) + 70)
  62.         self.color3 = min(255, int(self.v.len()) + 20)
  63.  
  64.     def handle_border(self):
  65.         if self.pos.x - self.r < self.rect.left:
  66.             if self.v.x < 0:
  67.                 self.v.x = -self.v.x
  68.             self.pos.x = self.rect.left + self.r
  69.         if self.pos.y - self.r < self.rect.top:
  70.             if self.v.y < 0:
  71.                 self.v.y = -self.v.y
  72.             self.pos.y = self.rect.top + self.r
  73.         if self.pos.x + self.r > self.rect.right:
  74.             if self.v.x > 0:
  75.                 self.v.x = -self.v.x
  76.             self.pos.x = self.rect.right - self.r
  77.         if self.pos.y + self.r > self.rect.bottom:
  78.             if self.v.y > 0:
  79.                 self.v.y = -self.v.y
  80.             self.pos.y = self.rect.bottom - self.r
  81.  
  82.     def set_pressed(self, pressed):
  83.         self.pressed[pygame.K_d] = pressed.get(pygame.K_d, 0)
  84.         self.pressed[pygame.K_a] = pressed.get(pygame.K_a, 0)
  85.         self.pressed[pygame.K_s] = pressed.get(pygame.K_s, 0)
  86.         self.pressed[pygame.K_w] = pressed.get(pygame.K_w, 0)
  87.  
  88.     def set_pos(self, pos, v):
  89.         self.pos = Vec2(*pos)
  90.         self.v = Vec2(*v)
  91.  
  92.     def update(self, dt):
  93.         """Update Player state"""
  94.         f = Vec2()
  95.         f.x = self.pressed[pygame.K_d] - self.pressed[pygame.K_a]
  96.         f.y = self.pressed[pygame.K_s] - self.pressed[pygame.K_w]
  97.         f *= self.a
  98.         self.v = self.v + dt * (f - self.v)
  99.         self.pos += dt * self.v
  100.  
  101.         self.handle_border()
  102.  
  103.         self.refresh_color()
  104.  
  105.     def render(self, pole):
  106.         """Draw Player on the Game window"""
  107.         pole.circle((self.color1, self.color2, self.color3), self.pos.intpair(), self.r)
  108.  
  109. class Bullet(Player):
  110.     def __init__(self, posb, rect, ba=500, bradius=20, local=False):
  111.         self.posb, self.ba, self.br, self.local = Vec2(*posb), ba, bradius, local
  112.         self.bpressed = {pygame.mouse.get_pos(): 0}
  113.         self.rect = rect
  114.         self.bv = Vec2()
  115.  
  116.     def set_bpressed(self, bpressed):
  117.         self.bpressed[pygame.mouse.get_pos()] = bpressed.get(pygame.mouse.get_pos(), 0)
  118.  
  119.     def set_bpos(self, posb, bv):
  120.         self.posb = Vec2(*posb)
  121.         self.bv = Vec2(*bv)
  122.  
  123.     def bupdate(self, dt):
  124.         """Update Bullet state"""
  125.         fb = Vec2()
  126.         #fb.x = self.posb - self.pos
  127.         #fb.y = self.posb - self.pos
  128.         fb.x = self.bpressed[pygame.mouse.get_pos()] - 0
  129.         fb.y = self.bpressed[pygame.mouse.get_pos()] - 0
  130.         fb *= self.ba
  131.         self.bv = self.bv + dt * (fb - self.bv)
  132.         self.posb += dt * self.bv
  133.  
  134.     def brender(self, pole):
  135.         """Draw Player on the Game window"""
  136.         pole.circle((255, 0, 0), self.posb.intpair(), self.br)
  137.  
  138. class World:
  139.     def __init__(self, rect):
  140.         self.rect = rect
  141.         self.unit = []
  142.         self.object = []
  143.         self.frame = Frame(pygame.key.get_pressed(), 0)
  144.  
  145.     def set_frame(self, frame):
  146.         self.frame = frame
  147.  
  148.     def update(self):
  149.         for u in self.unit:
  150.             if u.local:
  151.                 u.set_pressed(dict(enumerate(self.frame.pressed)))
  152.             u.update(self.frame.dt)
  153.  
  154.     def bupdate(self):
  155.         for b in self.object:
  156.             if b.local:
  157.                 b.set_bpressed(dict(enumerate(self.frame.pressed)))
  158.             b.bupdate(self.frame.dt)
  159.  
  160.     def render(self, pole):
  161.         pole.clear()
  162.         for u in self.unit:
  163.             u.render(pole)
  164.     def brender(self, pole):
  165.         for b in self.object:
  166.             b.brender(pole)
  167.  
  168.     def addUnit(self, u):
  169.         self.unit.append(u)
  170.  
  171.     def addobject(self, b):
  172.         self.object.append(b)
  173.  
  174. class Game:
  175.     def __init__(self):
  176.         self._gaming = True
  177.         self.size = self.width, self.height = 640, 400
  178.         self.screen = pygame.display.set_mode(self.size)
  179.         pygame.display.set_caption("Tank's")
  180.         self.clock = pygame.time.Clock()
  181.  
  182.         self.pole = Pole(self.screen)
  183.  
  184.         self.world = World(Rect(0, 0, self.width, self.height))
  185.         self.world.addUnit(Player(pos=(300, 200), rect=self.world.rect, local=True))
  186.         self.world.addobject(Bullet(posb=(10, 10), rect=self.world.rect, local=True))
  187.  
  188.     def exit(self):
  189.         """Exit the game"""
  190.         self._gaming = False
  191.  
  192.     def handle_event(self, event):
  193.         """Handling one pygame event"""
  194.         if event.type == pygame.QUIT:
  195.             self.exit()
  196.         elif event.type == pygame.KEYDOWN:
  197.             if event.key == pygame.K_ESCAPE:
  198.                 self.exit()
  199.         if event.type in (pygame.KEYDOWN, pygame.KEYUP) and event.key in (pygame.K_d, pygame.K_a, pygame.K_s, pygame.K_w):
  200.             """Update pressed for smooth sync"""
  201.             self.world.set_frame(Frame(pygame.key.get_pressed(), self.world.frame.dt))
  202.         if event.type == pygame.mouse.get_pos():
  203.             self.world.set_frame(Frame(pygame.mouse.get_pos(), self.world.frame.dt))
  204.  
  205.     def execute(self):
  206.         """Execution loop of the game"""
  207.         while self._gaming:
  208.             # get all pygame events from queue
  209.             for event in pygame.event.get():
  210.                 self.handle_event(event)
  211.  
  212.             dt = self.clock.tick(50) / 1000.0
  213.             self.world.set_frame(Frame(pygame.key.get_pressed(), dt))
  214.  
  215.             self.world.update()
  216.             self.world.bupdate()
  217.             self.world.render(self.pole)
  218.             self.world.brender(self.pole)
  219.             pygame.display.flip()
  220.  
  221.  
  222. game = Game()
  223. game.execute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement