Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- class Vec2:
- def __init__(self, x = 0, y = 0):
- self.x, self.y = x, y
- def __add__(self, v):
- return Vec2(self.x + v.x, self.y + v.y)
- def __sub__(self, v):
- return Vec2(self.x - v.x, self.y - v.y)
- def __mul__(self, alpha):
- return Vec2(self.x * alpha, self.y * alpha)
- def __rmul__(self, alpha):
- return Vec2(self.x * alpha, self.y * alpha)
- def intpair(self):
- return (int(self.x), int(self.y))
- def len(self):
- return math.sqrt(self.x * self.x + self.y * self.y)
- class Rect:
- def __init__(self, left, top, right, bottom):
- self.left, self.top, self.right, self.bottom = left, top, right, bottom
- class Frame:
- def __init__(self, pressed, dt):
- self.pressed = pressed
- self.dt = dt
- class Pole:
- def __init__(self, screen):
- self.screen = screen
- def clear(self):
- self.screen.fill((50, 120, 90))
- def circle(self, color, pos, radius):
- pygame.draw.circle(self.screen, color, pos, radius)
- class Player:
- def __init__(self, pos, rect, a = 500, radius = 20, local = False):
- """Constructor of Player class
- self.a - acceleration coef.
- self.r - radius
- """
- self.pos, self.a, self.r, self.local = Vec2(*pos), a, radius, local
- self.pressed = {pygame.K_d: 0, pygame.K_a: 0, pygame.K_s: 0, pygame.K_w: 0}
- self.rect = rect
- self.v = Vec2()
- self.refresh_color()
- def refresh_color(self):
- """Set color (it depens on the module of Player speed)"""
- self.color1 = min(150, int(self.v.len()) + 50)
- self.color2 = min(255, int(self.v.len()) + 70)
- self.color3 = min(255, int(self.v.len()) + 20)
- def handle_border(self):
- if self.pos.x - self.r < self.rect.left:
- if self.v.x < 0:
- self.v.x = -self.v.x
- self.pos.x = self.rect.left + self.r
- if self.pos.y - self.r < self.rect.top:
- if self.v.y < 0:
- self.v.y = -self.v.y
- self.pos.y = self.rect.top + self.r
- if self.pos.x + self.r > self.rect.right:
- if self.v.x > 0:
- self.v.x = -self.v.x
- self.pos.x = self.rect.right - self.r
- if self.pos.y + self.r > self.rect.bottom:
- if self.v.y > 0:
- self.v.y = -self.v.y
- self.pos.y = self.rect.bottom - self.r
- def set_pressed(self, pressed):
- self.pressed[pygame.K_d] = pressed.get(pygame.K_d, 0)
- self.pressed[pygame.K_a] = pressed.get(pygame.K_a, 0)
- self.pressed[pygame.K_s] = pressed.get(pygame.K_s, 0)
- self.pressed[pygame.K_w] = pressed.get(pygame.K_w, 0)
- def set_pos(self, pos, v):
- self.pos = Vec2(*pos)
- self.v = Vec2(*v)
- def update(self, dt):
- """Update Player state"""
- f = Vec2()
- f.x = self.pressed[pygame.K_d] - self.pressed[pygame.K_a]
- f.y = self.pressed[pygame.K_s] - self.pressed[pygame.K_w]
- f *= self.a
- self.v = self.v + dt * (f - self.v)
- self.pos += dt * self.v
- self.handle_border()
- self.refresh_color()
- def render(self, pole):
- """Draw Player on the Game window"""
- pole.circle((self.color1, self.color2, self.color3), self.pos.intpair(), self.r)
- class Bullet(Player):
- def __init__(self, posb, rect, ba=500, bradius=20, local=False):
- self.posb, self.ba, self.br, self.local = Vec2(*posb), ba, bradius, local
- self.bpressed = {pygame.mouse.get_pos(): 0}
- self.rect = rect
- self.bv = Vec2()
- def set_bpressed(self, bpressed):
- self.bpressed[pygame.mouse.get_pos()] = bpressed.get(pygame.mouse.get_pos(), 0)
- def set_bpos(self, posb, bv):
- self.posb = Vec2(*posb)
- self.bv = Vec2(*bv)
- def bupdate(self, dt):
- """Update Bullet state"""
- fb = Vec2()
- #fb.x = self.posb - self.pos
- #fb.y = self.posb - self.pos
- fb.x = self.bpressed[pygame.mouse.get_pos()] - 0
- fb.y = self.bpressed[pygame.mouse.get_pos()] - 0
- fb *= self.ba
- self.bv = self.bv + dt * (fb - self.bv)
- self.posb += dt * self.bv
- def brender(self, pole):
- """Draw Player on the Game window"""
- pole.circle((255, 0, 0), self.posb.intpair(), self.br)
- class World:
- def __init__(self, rect):
- self.rect = rect
- self.unit = []
- self.object = []
- self.frame = Frame(pygame.key.get_pressed(), 0)
- def set_frame(self, frame):
- self.frame = frame
- def update(self):
- for u in self.unit:
- if u.local:
- u.set_pressed(dict(enumerate(self.frame.pressed)))
- u.update(self.frame.dt)
- def bupdate(self):
- for b in self.object:
- if b.local:
- b.set_bpressed(dict(enumerate(self.frame.pressed)))
- b.bupdate(self.frame.dt)
- def render(self, pole):
- pole.clear()
- for u in self.unit:
- u.render(pole)
- def brender(self, pole):
- for b in self.object:
- b.brender(pole)
- def addUnit(self, u):
- self.unit.append(u)
- def addobject(self, b):
- self.object.append(b)
- class Game:
- def __init__(self):
- self._gaming = True
- self.size = self.width, self.height = 640, 400
- self.screen = pygame.display.set_mode(self.size)
- pygame.display.set_caption("Tank's")
- self.clock = pygame.time.Clock()
- self.pole = Pole(self.screen)
- self.world = World(Rect(0, 0, self.width, self.height))
- self.world.addUnit(Player(pos=(300, 200), rect=self.world.rect, local=True))
- self.world.addobject(Bullet(posb=(10, 10), rect=self.world.rect, local=True))
- def exit(self):
- """Exit the game"""
- self._gaming = False
- def handle_event(self, event):
- """Handling one pygame event"""
- if event.type == pygame.QUIT:
- self.exit()
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- self.exit()
- if event.type in (pygame.KEYDOWN, pygame.KEYUP) and event.key in (pygame.K_d, pygame.K_a, pygame.K_s, pygame.K_w):
- """Update pressed for smooth sync"""
- self.world.set_frame(Frame(pygame.key.get_pressed(), self.world.frame.dt))
- if event.type == pygame.mouse.get_pos():
- self.world.set_frame(Frame(pygame.mouse.get_pos(), self.world.frame.dt))
- def execute(self):
- """Execution loop of the game"""
- while self._gaming:
- # get all pygame events from queue
- for event in pygame.event.get():
- self.handle_event(event)
- dt = self.clock.tick(50) / 1000.0
- self.world.set_frame(Frame(pygame.key.get_pressed(), dt))
- self.world.update()
- self.world.bupdate()
- self.world.render(self.pole)
- self.world.brender(self.pole)
- pygame.display.flip()
- game = Game()
- game.execute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement