class RegularPolygon: def __init__(self, sides, pos, size, Keyboard, CANVAS_HEIGHT, CANVAS_WIDTH): self.CANVAS_HEIGHT = CANVAS_HEIGHT self.CANVAS_WIDTH = CANVAS_WIDTH self.Keyboard = Keyboard self.sides = sides self.pos = pos self.vel = Vector() self.size = size self.generator = Vector(0, -self.size) self.vertices = [] self.botVert = self.generator + self.pos def computeVertices(self): angle = 360 / self.sides gen = self.generator.copy() self.vertices = [] for i in range(self.sides): self.vertices.append(self.pos + gen) gen.rotate(angle) def computeSides(self): self.computeVertices() self.lines = [ Line(self.vertices[i], self.vertices[(i + 1) % len(self.vertices)]) for i in range(len(self.vertices)) ] def draw(self, canvas): self.computeSides() for line in self.lines: line.draw(canvas) def drawGenerator(self, canvas): line = Line(self.pos, self.pos + self.generator) line.draw(canvas) def bottomVert(self): #bottom corner position return self.vertices[(self.sides + 1) % len(self.vertices)] def bottomVertVec(self): return Vector((self.bottomVert().x), (self.bottomVert().y)) def topVert(self): #top corner position return (self.generator + self.pos) def topVertVec(self): return Vector((self.topVert().x), (self.topVert().y)) def forVec(self): return Vector((self.topVertVec().x - self.bottomVertVec().x), (self.topVertVec().y - self.bottomVertVec().y)) def forwardVel(self): return Vector( self.topVert - self.bottomVert ) #self.topVert.x #-self.bottomVert.x #)/10),((self.topVert.y-self.bottomVert.y)/10)) def print2(self): return Vector((self.vertices[(self.sides + 1) % len(self.vertices)]) - (self.generator + self.pos)) def update(self): self.pos.add(self.vel) self.vel.multiply(0.85) if self.pos.y > self.CANVAS_HEIGHT: self.pos.y = 0 if self.pos.y < 0: self.pos.y = self.CANVAS_HEIGHT if self.pos.x > self.CANVAS_WIDTH: self.pos.x = 0 if self.pos.x < 0: self.pos.x = self.CANVAS_WIDTH if self.Keyboard.right and self.Keyboard.left: self.vel.add(Vector(0, 0)) if self.Keyboard.right and self.Keyboard.up: self.vel.add(Vector(1, -1.5)) if self.Keyboard.left and self.Keyboard.up: self.vel.add(Vector(-1, -1.5)) if self.Keyboard.right and self.Keyboard.down: self.vel.add(Vector(1, 1.5)) if self.Keyboard.left and self.Keyboard.down: self.vel.add(Vector(-1, 1.5)) if self.Keyboard.up: self.vel.add(Vector(self.forVec().x / 100, self.forVec().y / 100)) if self.Keyboard.down: self.vel.add( Vector(-self.forVec().x / 100, -(self.forVec().y) / 100)) if self.Keyboard.right: self.generator.rotate(1.5) if self.Keyboard.left: self.generator.rotate(-1.5)
class Player: # to create instances of the player def __init__(self, name, vel, pos, radius=50): self.name = name # name of player self.pos = pos # position of player self.vel = vel # velocity of player self.image = chickenHead self.imageWidth = self.image.get_width() self.imageHeight = self.image.get_height() self.dimX = 50 # dimentions of image on canvas self.dimY = 50 self.speed = 0.45 # multiplier of players speed self.radius = 20 self.border = 2 # border of circle underneath player sprite self.acc = Vector() # acceleration def restart(self): self.vel = Vector() # restart position for players, when scored self.acc = Vector() # similar for acceleration def outX( self ): # checking if the players are wanting to go out of bound in the x axis if self.name == 1: return (self.pos.x < WIDTH / 2 + self.radius or self.pos.x > WIDTH - self.radius ) # checks if position of ball is outside canvas return (self.pos.x < 0 + self.radius or self.pos.x > WIDTH / 2 - self.radius) def outY(self): # checking if players go out of bount y axis return (self.pos.y - self.radius < 0 or self.pos.y + self.radius > HEIGHT) def draw(self, canvas): canvas.draw_circle( self.pos.getP(), self.radius, self.border, 'Red' ) # drawing a circle underneath sprite which actually does the colisions canvas.draw_image( self.image, (self.imageWidth / 2, self.imageHeight / 2), # drawing sprite of players, centre source (self.imageWidth, self.imageHeight), # width hight source (self.pos.x, self.pos.y), # centre destination (self.dimX, self.dimY)) # width- height destination def positionUpdate( self, keyboard): # allowing player moveent using keyboard class if self.name == 1: if keyboard.right and self.pos.getP()[0] < ( WIDTH - self.radius ): # checks to see player isn't out of bounds on far right x axis self.vel.add( Vector(1, 0) * self.speed) # add to velcoity vector to go to the right if keyboard.left and self.pos.getP()[0] > ( WIDTH / 2 + self.radius ): # only does as long as it is not off bound left xaxis self.vel.add(Vector(-1, 0) * self.speed) # goes 1 position to the left if keyboard.up and self.pos.getP()[1] > ( 0 + self.radius): # won't pass vector if player is on edge top self.vel.add(Vector(0, -1) * self.speed) # goes up if keyboard.down and self.pos.getP()[1] < ( HEIGHT - self.radius ): # won't pass vector if going off screen bottom self.vel.add(Vector(0, 1) * self.speed) # goes down else: # for player 2, similar method but for its buttons if keyboard.right2 and self.pos.getP()[0] < (WIDTH / 2 - self.radius): self.vel.add(Vector(1, 0) * self.speed) if keyboard.left2 and self.pos.getP()[0] > (0 + self.radius): self.vel.add(Vector(-1, 0) * self.speed) if keyboard.up2 and self.pos.getP()[1] > (0 + self.radius): self.vel.add(Vector(0, -1) * self.speed) if keyboard.down2 and self.pos.getP()[1] < (HEIGHT - self.radius): self.vel.add(Vector(0, 1) * self.speed) def update(self, keyboard): global WIDTH, HEIGHT self.positionUpdate(keyboard) if self.vel.x > 4: # 4 is the max velocity allowed, so that players do not go too quick self.vel.x = 4 if self.vel.y > 4: self.vel.y = 4 if self.vel.x < -4: # likewise, -4 so its not too slow self.vel.x = -4 if self.vel.y < -4: self.vel.y = -4 if self.outX(): # checking for boundries for x axis if self.name == 1: # confining players to their side of their screen if self.pos.x < WIDTH / 2 + 20 and self.vel.x < 0: # player one has the right half self.vel.x = 0 # sets velocity to 0 so they can't move if self.pos.x > WIDTH - 40 and self.vel.x > 0: # so doesn't go off on right ride screen self.vel.x = 0 else: if self.pos.x < 0 + 40 and self.vel.x < 0: # player 2 has left side self.vel.x = 0 if self.pos.x > WIDTH / 2 - 20 and self.vel.x > 0: # doesn't allow to go off screen left self.vel.x = 0 if self.outY(): # checking likewise for y axis if self.pos.y < 50 and self.vel.y < 0: # 50 (2*radius + extra so doesn't glitch) self.vel.y = 0 if self.pos.y > HEIGHT - 40 and self.vel.y > 0: self.vel.y = 0 if self.name == 1: if keyboard.pressed1 == 0: self.vel = self.vel.subtract(self.vel) else: if keyboard.pressed2 == 0: self.vel = Vector() self.pos.add(self.vel) # adding keyboard velocity to chicken def collides( self, other): # checking if two balls passed together are in colision return ((self.pos - other.pos).length() <= (self.radius + self.border) + (other.radius + other.border) ) # border being the border of circles