Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as py
- from pygame import gfxdraw as draw
- from sys import exit as ex
- from math import *
- from settings import *
- import os
- def check_fullscreen(z, b, size, self):
- if z and not b:
- return py.display.set_mode(
- (0, 0), py.FULLSCREEN)
- elif not b:
- x, y = self.X, self.Y
- return py.display.set_mode(
- (x, y))
- elif b:
- return py.display.set_mode(
- (0, 0), py.FULLSCREEN)
- else:
- return py.display.set_mode(
- (size))
- class Main:
- def __init__(self):
- py.init()
- self.clock = py.time.Clock()
- self.screen = check_fullscreen(
- inital_fullscreen, False, False, self)
- self.fullscreen = inital_fullscreen
- self.X, self.Y = self.screen.get_size()
- self.playerPos_init()
- self.running = True
- def loop(self):
- self.screen.fill((backGround_Color))
- self.font = py.font.Font('font.ttf', fontSize)
- self.font2 = py.font.Font('font.ttf', 50)
- if self.p1Shooting:
- x, y = 0, 0
- for z in self.Bullets1:
- x, y = z
- draw.filled_circle(
- self.screen, int(x), int(floor(
- y)), bulletRadius, self.bulletColor1)
- if y+bulletSpeed < (self.Y-b_Border)-(bulletRadius/2):
- self.Bullets1.insert(0, (x, y+bulletSpeed))
- self.Bullets1.remove((x, y))
- else:
- self.Bullets1 = []
- self.p1Shooting = False
- if self.p2Shooting:
- x, y = 0, 0
- for z in self.Bullets2:
- x, y = z
- draw.filled_circle(
- self.screen, int(x), int(floor(
- y)), bulletRadius, self.bulletColor2)
- if y-bulletSpeed > t_Border+bulletRadius:
- self.Bullets2.insert(0, (x, y-bulletSpeed))
- self.Bullets2.remove((x, y))
- else:
- self.Bullets2 = []
- self.p2Shooting = False
- self.chk_plrCollusion()
- self.Borders()
- self.refPlayers()
- if self.running:
- self.events = py.event.get()
- else:
- self.text = self.font.render(self.won, True, (fontColor))
- self.resetText = self.font2.render('press backspace to restart', True, (fontColor))
- x, y = self.font.size(self.won)
- x1, y1 = self.font2.size('press backspace to restart')
- rad = playerRadius
- if self.won == 'player 1 wins!':
- self.screen.blit(self.text, ((self.X/2)-(x/2), (
- (((self.Y/2)-(m_Border/2))-t_Border)/2)-(y/2)))
- else:
- self.screen.blit(self.text, ((self.X/2)-(x/2), (
- ((self.Y-b_Border)-(m_Border/2))/1.25)-(y/2)))
- self.screen.blit(self.resetText, ((self.X/2)-(x1/2), (self.Y/2)-(y1/2)))
- for x in self.events:
- if x.type == py.QUIT:
- py.quit()
- ex()
- py.display.update()
- self.clock.tick(framesPerSecond)
- def Borders(self):
- X, Y = self.X, self.Y
- draw.box(self.screen
- , py.Rect(0, 0, X, t_Border), border_Color)
- draw.box(self.screen
- , py.Rect(0, Y-b_Border, X, Y), border_Color)
- draw.box(self.screen
- , py.Rect(0, 0, l_Border, Y), border_Color)
- draw.box(self.screen
- , py.Rect(X-r_Border, 0, X, Y), border_Color)
- draw.box(self.screen
- , py.Rect(0, (Y/2)-(m_Border/2), X
- , m_Border), border_Color)
- def toggleFullscreen(self):
- py.display.quit()
- if self.fullscreen:
- self.fullscreen = False
- self.screen = check_fullscreen(
- self.fullscreen, False, False, self)
- else:
- self.fullscreen = True
- self.screen = check_fullscreen(
- self.fullscreen, True, (self.X, self.Y), self)
- def playerPos_init(self):
- rad = playerRadius
- self.Bullets1, self.Bullets2 = [], []
- self.bulletColor1, self.bulletColor2 = (bulletColor), (bulletColor)
- self.p1 = (ceil(self.X/2)
- , ceil(((self.Y/4)-(m_Border/2))-rad))
- self.p2 = (ceil(self.X/2)
- , ceil(((self.Y/4)+(m_Border/2))+rad)+ceil(self.Y/2))
- self.p1Color, self.p2Color = (initial_playerColor1), (initial_playerColor2)
- (self.p1RunningUp, self.p1RunningDown
- , self.p1RunningRight, self.p1RunningLeft) = False, False, False, False
- (self.p2RunningUp, self.p2RunningDown
- , self.p2RunningRight, self.p2RunningLeft) = False, False, False, False
- self.p1Shooting, self.p2Shooting = False, False
- def End(self):
- while not self.running:
- self.loop()
- for x in py.event.get():
- if x.type == py.QUIT:
- py.quit()
- ex()
- if x.type == py.KEYDOWN:
- if x.key == py.K_ESCAPE:
- py.display.quit()
- self.toggleFullscreen()
- elif x.key == py.K_BACKSPACE:
- py.display.quit()
- py.quit();os.system('main.py');ex()
- py.display.update()
- self.clock.tick(framesPerSecond)
- def refPlayers(self):
- if self.p1RunningUp:
- y = self.p1[1]
- if y-playerStep > playerRadius+t_Border:
- self.p1 = (self.p1[0], y-playerStep)
- else:
- self.p1 = (self.p1[0], playerRadius+t_Border)
- if self.p1RunningDown:
- y = self.p1[1]
- if y+playerStep < ((self.Y/2)-(m_Border/2))-playerRadius:
- self.p1 = (self.p1[0], y+playerStep)
- else:
- self.p1 = (self.p1[0], ceil(
- ((self.Y/2)-(m_Border/2))-playerRadius))
- if self.p1RunningRight:
- x = self.p1[0]
- if x+playerStep < (self.X-playerRadius)-r_Border:
- self.p1 = (x+playerStep, self.p1[1])
- else:
- self.p1 = ((self.X-playerRadius)-r_Border, self.p1[1])
- if self.p1RunningLeft:
- x = self.p1[0]
- if x-playerStep > playerRadius+l_Border:
- self.p1 = (x-playerStep, self.p1[1])
- else:
- self.p1 = (playerRadius+l_Border, self.p1[1])
- if self.p2RunningUp:
- y = self.p2[1]
- if y-playerStep > ((self.Y/2)+(m_Border/2))+playerRadius:
- self.p2 = (self.p2[0], y-playerStep)
- else:
- self.p2 = (self.p2[0], ceil(
- ((self.Y/2)+(m_Border/2))+playerRadius))
- if self.p2RunningDown:
- y = self.p2[1]
- if y+playerStep < (self.Y-playerRadius)-b_Border:
- self.p2 = (self.p2[0], y+playerStep)
- else:
- self.p2 = (self.p2[0], ceil(
- (self.Y-playerRadius)-b_Border))
- if self.p2RunningRight:
- x = self.p2[0]
- if x+playerStep < (self.X-playerRadius)-r_Border:
- self.p2 = (x+playerStep, self.p2[1])
- else:
- self.p2 = (ceil((self.X-playerRadius)-r_Border)
- , self.p2[1])
- if self.p2RunningLeft:
- x = self.p2[0]
- if x-playerStep > playerRadius+l_Border:
- self.p2 = (x-playerStep, self.p2[1])
- else:
- self.p2 = (playerRadius+l_Border, self.p2[1])
- draw.filled_circle(self.screen
- , int(self.p1[0]), int(self.p1[1]), playerRadius, (self.p1Color))
- draw.filled_circle(self.screen
- , int(self.p2[0]), int(self.p2[1]), playerRadius, (self.p2Color))
- def Bullet(self, plr):
- if plr == self.p1 and not self.p1Shooting:
- self.p1Shooting = True
- b = self.Bullets1
- b.insert(len(b), (self.p1[0], self.p1[1]+(playerRadius/2)))
- if plr == self.p2 and not self.p2Shooting:
- self.p2Shooting = True
- b = self.Bullets2
- b.insert(len(b), (self.p2[0], self.p2[1]-(playerRadius/2)))
- def chk_plrCollusion(self):
- global bulletSpeed
- for z in self.Bullets1:
- bX, bY = z
- X, Y = self.p2
- if hypot(X-bX, Y-bY) <= playerRadius+bulletRadius:
- self.running = False
- bulletSpeed = 0
- self.won = 'player 1 wins!'
- (self.p1RunningUp, self.p1RunningDown
- , self.p1RunningRight, self.p1RunningLeft) = False, False, False, False
- (self.p2RunningUp, self.p2RunningDown
- , self.p2RunningRight, self.p2RunningLeft) = False, False, False, False
- for z in self.Bullets2:
- bX, bY = z
- X, Y = self.p1
- if hypot(X-bX, Y-bY) <= playerRadius+bulletRadius:
- self.running = False
- bulletSpeed = 0
- self.won = 'player 2 wins!'
- (self.p1RunningUp, self.p1RunningDown
- , self.p1RunningRight, self.p1RunningLeft) = False, False, False, False
- (self.p2RunningUp, self.p2RunningDown
- , self.p2RunningRight, self.p2RunningLeft) = False, False, False, False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement