Advertisement
V3N0M_Z

Untitled

Jun 7th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.29 KB | None | 0 0
  1. import pygame as py
  2. from pygame import gfxdraw as draw
  3. from sys import exit as ex
  4. from math import *
  5. from settings import *
  6. import os
  7. def check_fullscreen(z, b, size, self):
  8.     if z and not b:
  9.         return py.display.set_mode(
  10.         (0, 0), py.FULLSCREEN)
  11.     elif not b:
  12.         x, y = self.X, self.Y
  13.         return py.display.set_mode(
  14.         (x, y))
  15.     elif b:
  16.         return py.display.set_mode(
  17.         (0, 0), py.FULLSCREEN)
  18.     else:
  19.         return py.display.set_mode(
  20.         (size))
  21. class Main:  
  22.     def __init__(self):
  23.         py.init()
  24.         self.clock = py.time.Clock()
  25.         self.screen = check_fullscreen(
  26.         inital_fullscreen, False, False, self)
  27.         self.fullscreen = inital_fullscreen
  28.         self.X, self.Y = self.screen.get_size()
  29.         self.playerPos_init()
  30.         self.running = True
  31.        
  32.     def loop(self):
  33.         self.screen.fill((backGround_Color))
  34.         self.font = py.font.Font('font.ttf', fontSize)
  35.         self.font2 = py.font.Font('font.ttf', 50)
  36.         if self.p1Shooting:
  37.             x, y = 0, 0
  38.             for z in self.Bullets1:
  39.                 x, y = z
  40.                 draw.filled_circle(
  41.                 self.screen, int(x), int(floor(
  42.                 y)), bulletRadius, self.bulletColor1)
  43.             if y+bulletSpeed < (self.Y-b_Border)-(bulletRadius/2):
  44.                 self.Bullets1.insert(0, (x, y+bulletSpeed))
  45.                 self.Bullets1.remove((x, y))
  46.             else:
  47.                 self.Bullets1 = []
  48.                 self.p1Shooting = False
  49.         if self.p2Shooting:
  50.             x, y = 0, 0
  51.             for z in self.Bullets2:
  52.                 x, y = z
  53.                 draw.filled_circle(
  54.                 self.screen, int(x), int(floor(
  55.                 y)), bulletRadius, self.bulletColor2)
  56.             if y-bulletSpeed > t_Border+bulletRadius:
  57.                 self.Bullets2.insert(0, (x, y-bulletSpeed))
  58.                 self.Bullets2.remove((x, y))
  59.             else:
  60.                 self.Bullets2 = []
  61.                 self.p2Shooting = False
  62.         self.chk_plrCollusion()
  63.         self.Borders()
  64.         self.refPlayers()
  65.         if self.running:
  66.             self.events = py.event.get()
  67.         else:
  68.             self.text = self.font.render(self.won, True, (fontColor))
  69.             self.resetText = self.font2.render('press backspace to restart', True, (fontColor))
  70.             x, y = self.font.size(self.won)
  71.             x1, y1 = self.font2.size('press backspace to restart')
  72.             rad = playerRadius
  73.             if self.won == 'player 1 wins!':
  74.                 self.screen.blit(self.text, ((self.X/2)-(x/2), (
  75.                 (((self.Y/2)-(m_Border/2))-t_Border)/2)-(y/2)))
  76.             else:
  77.                 self.screen.blit(self.text, ((self.X/2)-(x/2), (
  78.                 ((self.Y-b_Border)-(m_Border/2))/1.25)-(y/2)))
  79.             self.screen.blit(self.resetText, ((self.X/2)-(x1/2), (self.Y/2)-(y1/2)))
  80.         for x in self.events:
  81.             if x.type == py.QUIT:
  82.                 py.quit()
  83.                 ex()
  84.         py.display.update()
  85.         self.clock.tick(framesPerSecond)
  86.     def Borders(self):
  87.         X, Y = self.X, self.Y
  88.         draw.box(self.screen
  89.         , py.Rect(0, 0, X, t_Border), border_Color)
  90.         draw.box(self.screen
  91.         , py.Rect(0, Y-b_Border, X, Y), border_Color)
  92.         draw.box(self.screen
  93.         , py.Rect(0, 0, l_Border, Y), border_Color)
  94.         draw.box(self.screen
  95.         , py.Rect(X-r_Border, 0, X, Y), border_Color)
  96.         draw.box(self.screen
  97.         , py.Rect(0, (Y/2)-(m_Border/2), X
  98.         , m_Border), border_Color)
  99.     def toggleFullscreen(self):
  100.         py.display.quit()
  101.         if self.fullscreen:
  102.             self.fullscreen = False
  103.             self.screen = check_fullscreen(
  104.             self.fullscreen, False, False, self)
  105.         else:
  106.             self.fullscreen = True
  107.             self.screen = check_fullscreen(
  108.             self.fullscreen, True, (self.X, self.Y), self)
  109.     def playerPos_init(self):
  110.         rad = playerRadius
  111.         self.Bullets1, self.Bullets2 = [], []
  112.         self.bulletColor1, self.bulletColor2 = (bulletColor), (bulletColor)
  113.         self.p1 = (ceil(self.X/2)
  114.         , ceil(((self.Y/4)-(m_Border/2))-rad))
  115.         self.p2 = (ceil(self.X/2)
  116.         , ceil(((self.Y/4)+(m_Border/2))+rad)+ceil(self.Y/2))
  117.         self.p1Color, self.p2Color = (initial_playerColor1), (initial_playerColor2)
  118.         (self.p1RunningUp, self.p1RunningDown
  119.          , self.p1RunningRight, self.p1RunningLeft) = False, False, False, False
  120.         (self.p2RunningUp, self.p2RunningDown
  121.          , self.p2RunningRight, self.p2RunningLeft) = False, False, False, False
  122.         self.p1Shooting, self.p2Shooting = False, False
  123.     def End(self):
  124.         while not self.running:
  125.             self.loop()
  126.             for x in py.event.get():
  127.                 if x.type == py.QUIT:
  128.                     py.quit()
  129.                     ex()
  130.                 if x.type == py.KEYDOWN:
  131.                     if x.key == py.K_ESCAPE:
  132.                         py.display.quit()
  133.                         self.toggleFullscreen()
  134.                     elif x.key == py.K_BACKSPACE:
  135.                         py.display.quit()
  136.                         py.quit();os.system('main.py');ex()                        
  137.             py.display.update()
  138.             self.clock.tick(framesPerSecond)
  139.     def refPlayers(self):
  140.         if self.p1RunningUp:
  141.             y = self.p1[1]
  142.             if y-playerStep > playerRadius+t_Border:
  143.                 self.p1 = (self.p1[0], y-playerStep)
  144.             else:
  145.                 self.p1 = (self.p1[0], playerRadius+t_Border)
  146.         if self.p1RunningDown:
  147.             y = self.p1[1]
  148.             if y+playerStep < ((self.Y/2)-(m_Border/2))-playerRadius:
  149.                 self.p1 = (self.p1[0], y+playerStep)
  150.             else:
  151.                 self.p1 = (self.p1[0], ceil(
  152.                 ((self.Y/2)-(m_Border/2))-playerRadius))
  153.         if self.p1RunningRight:
  154.             x = self.p1[0]
  155.             if x+playerStep < (self.X-playerRadius)-r_Border:
  156.                 self.p1 = (x+playerStep, self.p1[1])
  157.             else:
  158.                 self.p1 = ((self.X-playerRadius)-r_Border, self.p1[1])
  159.         if self.p1RunningLeft:
  160.             x = self.p1[0]
  161.             if x-playerStep > playerRadius+l_Border:
  162.                 self.p1 = (x-playerStep, self.p1[1])
  163.             else:
  164.                 self.p1 = (playerRadius+l_Border, self.p1[1])
  165.         if self.p2RunningUp:
  166.             y = self.p2[1]
  167.             if y-playerStep > ((self.Y/2)+(m_Border/2))+playerRadius:
  168.                 self.p2 = (self.p2[0], y-playerStep)
  169.             else:
  170.                 self.p2 = (self.p2[0], ceil(
  171.                 ((self.Y/2)+(m_Border/2))+playerRadius))
  172.         if self.p2RunningDown:
  173.             y = self.p2[1]
  174.             if y+playerStep < (self.Y-playerRadius)-b_Border:
  175.                 self.p2 = (self.p2[0], y+playerStep)
  176.             else:
  177.                 self.p2 = (self.p2[0], ceil(
  178.                 (self.Y-playerRadius)-b_Border))
  179.         if self.p2RunningRight:
  180.             x = self.p2[0]
  181.             if x+playerStep < (self.X-playerRadius)-r_Border:
  182.                 self.p2 = (x+playerStep, self.p2[1])
  183.             else:
  184.                 self.p2 = (ceil((self.X-playerRadius)-r_Border)
  185.                 , self.p2[1])
  186.         if self.p2RunningLeft:
  187.             x = self.p2[0]
  188.             if x-playerStep > playerRadius+l_Border:
  189.                 self.p2 = (x-playerStep, self.p2[1])
  190.             else:
  191.                 self.p2 = (playerRadius+l_Border, self.p2[1])
  192.         draw.filled_circle(self.screen
  193.         , int(self.p1[0]), int(self.p1[1]), playerRadius, (self.p1Color))
  194.         draw.filled_circle(self.screen
  195.         , int(self.p2[0]), int(self.p2[1]), playerRadius, (self.p2Color))
  196.     def Bullet(self, plr):
  197.         if plr == self.p1 and not self.p1Shooting:
  198.             self.p1Shooting = True
  199.             b = self.Bullets1
  200.             b.insert(len(b), (self.p1[0], self.p1[1]+(playerRadius/2)))
  201.         if plr == self.p2 and not self.p2Shooting:
  202.             self.p2Shooting = True
  203.             b = self.Bullets2
  204.             b.insert(len(b), (self.p2[0], self.p2[1]-(playerRadius/2)))
  205.     def chk_plrCollusion(self):
  206.         global bulletSpeed
  207.         for z in self.Bullets1:
  208.             bX, bY = z
  209.             X, Y = self.p2
  210.             if hypot(X-bX, Y-bY) <= playerRadius+bulletRadius:
  211.                 self.running = False
  212.                 bulletSpeed = 0
  213.                 self.won = 'player 1 wins!'
  214.                 (self.p1RunningUp, self.p1RunningDown
  215.                  , self.p1RunningRight, self.p1RunningLeft) = False, False, False, False
  216.                 (self.p2RunningUp, self.p2RunningDown
  217.                  , self.p2RunningRight, self.p2RunningLeft) = False, False, False, False
  218.         for z in self.Bullets2:
  219.             bX, bY = z
  220.             X, Y = self.p1
  221.             if hypot(X-bX, Y-bY) <= playerRadius+bulletRadius:
  222.                 self.running = False
  223.                 bulletSpeed = 0
  224.                 self.won = 'player 2 wins!'
  225.                 (self.p1RunningUp, self.p1RunningDown
  226.                  , self.p1RunningRight, self.p1RunningLeft) = False, False, False, False
  227.                 (self.p2RunningUp, self.p2RunningDown
  228.                  , self.p2RunningRight, self.p2RunningLeft) = False, False, False, False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement