def __init__(self, kbd): # instaciating the plaers and ball, as well as keyboard self.kbd = kbd self.chicken = Player(1, Vector(), Vector(WIDTH - 50, HEIGHT / 2), 20) self.chicken2 = Player(2, Vector(), Vector(0 + 50, HEIGHT / 2), 20) self.ball = Ball(Vector(WIDTH / 2, HEIGHT / 2), 5) self.objects = [self.chicken, self.chicken2, self.ball] # put the players and ball in an array self.inCollision = set() # a set to handle anything in collision
class Ball: def __init__(self, pos, name, radius=25): # making the football self.pos = pos self.radius = radius self.vel = Vector() self.border = 2 self.name = name self.bouncing = False self.image = ball self.imageWidth = self.image.get_width() self.imageHeight = self.image.get_height() self.dimX = 60 # dimentions of image on canvas self.dimY = 60 def outX(self): # checking to see if ball is out of bounds x axis return (self.pos.x - self.radius < 0 or self.pos.x + self.radius > WIDTH ) # checks if position of ball is outside canvas def outY(self): # checking to see if ball is out of bounds 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, 'Blue', 'Blue') # draws blue ball which handles colisions canvas.draw_image( self.image, (self.imageWidth / 2, self.imageHeight / 2), # draw football 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 bounce(self, normal): self.vel.reflect(normal) def update(self): self.pos.add(self.vel) # updates velocity of ball if self.outY( ) and not self.bouncing: # bouncing from walls (only on top and bottom) self.bouncing = True # boolean to make sure it doesn't get stuck bouncing with the wall self.vel.y *= -0.85 # so that it does not keep gaining speed when bouncing else: self.bouncing = False def collides(self, other): # checking it is coliding with anything else return ((self.pos - other.pos).length() <= (self.radius + self.border) + (other.radius + other.border) ) # border being the border of circles def contains(self, other): # to avoid sticky balls/ merging with players return ((self.pos - other.pos).length() < (self.border + self.radius + other.radius + other.border))
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 __init__(self, pos, name, radius=25): # making the football self.pos = pos self.radius = radius self.vel = Vector() self.border = 2 self.name = name self.bouncing = False self.image = ball self.imageWidth = self.image.get_width() self.imageHeight = self.image.get_height() self.dimX = 60 # dimentions of image on canvas self.dimY = 60
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)
class Character: def __init__(self, pos): self.pos = pos self.vel = Vector() def attack(self): attack_index = [counter % ATTACK_DIM[0], counter // ATTACK_DIM[0]] canvas.draw_image(attack_image, [ ATTACK_CENTER[0] + attack_index[0] * ATTACK_SIZE[0], ATTACK_CENTER[1] + attack_index[1] * ATTACK_SIZE[1] ], ATTACK_SIZE, self.pos.getP(), ATTACK_SIZE) def draw(self, canvas): global counter global back global left global down global space down = False if ((down == True)): front_index = [counter % FRONT_DIM[0], counter // FRONT_DIM[0]] canvas.draw_image(front_image, [ FRONT_CENTER[0] + front_index[0] * FRONT_SIZE[0], FRONT_CENTER[1] + front_index[1] * FRONT_SIZE[1] ], FRONT_SIZE, self.pos.getP(), FRONT_SIZE) if (left == False and back == False and down == False): right_index = [counter % RIGHT_DIM[0], counter // RIGHT_DIM[0]] canvas.draw_image(right_image, [ RIGHT_CENTER[0] + right_index[0] * RIGHT_SIZE[0], RIGHT_CENTER[1] + right_index[1] * RIGHT_SIZE[1] ], RIGHT_SIZE, self.pos.getP(), RIGHT_SIZE) if (back == True): back_index = [counter % BACK_DIM[0], counter // BACK_DIM[0]] canvas.draw_image(back_image, [ BACK_CENTER[0] + back_index[0] * BACK_SIZE[0], BACK_CENTER[1] + back_index[1] * BACK_SIZE[1] ], BACK_SIZE, self.pos.getP(), BACK_SIZE) if (left == True and back == False and down == False): left_index = [counter % LEFT_DIM[0], counter // LEFT_DIM[0]] canvas.draw_image(left_image, [ LEFT_CENTER[0] + left_index[0] * LEFT_SIZE[0], LEFT_CENTER[1] + left_index[1] * LEFT_SIZE[1] ], LEFT_SIZE, self.pos.getP(), LEFT_SIZE) def update(self): self.pos.add(self.vel) self.vel.multiply(0.15)
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)
def update(self): global back global left if self.keyboard.right: self.character.vel.add(Vector(1, 0)) right = True downF = False timerR.start() else: timerR.stop() right = False if self.keyboard.left: self.character.vel.add(Vector(-1, 0)) timerL.start() downF = False left = True else: timerL.stop() left = False if self.keyboard.up: self.character.vel.add(Vector(0, -1)) timerU.start() downF = False back = True else: timerU.stop() back = False if self.keyboard.down: self.character.vel.add(Vector(0, 1)) down = True timerD.start() else: down = False timerD.stop() if self.keyboard.space: space = True else: space = False
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 draw(canvas): global counter1 global rotate global lives ### Drawing background and character### if (c.pos == Vector(130, 50)): canvas.draw_image(imagef, image_centerf, image_sizef, posf, image_sizef) c.pos = (130, 688) else: canvas.draw_image(image, image_center, image_size, pos, image_size) inter.update() c.update() c.draw(canvas) ###Wisps draw and shoot### global counter1 global rotate global rotate2 if (rotate < 1): gun.generator.rotate(270) rotate = 1 p = Particle(gun.pos + gun.generator, gun.generator.getNormalized()) if (counter1 % 50 == 0): particles.append(p) for p in particles: p.draw(canvas) p.update() if (p.pos == c.pos): lives = lives - 1 gun.draw(canvas) gun.drawGenerator(canvas) counter1 = counter1 + 1 ##Wisp2## if (rotate2 < 1): gun1.generator.rotate(90) rotate2 = 1 p = Particle(gun1.pos + gun1.generator, gun1.generator.getNormalized()) if (counter1 % 75 == 0): particles.append(p) for p in particles: p.draw(canvas) p.update() gun1.draw(canvas) gun1.drawGenerator(canvas) w1.draw(canvas) ###Scores### canvas.draw_text(str(int(scores)), [40, 50], 10, "White") canvas.draw_text("Score: ", [10, 50], 10, "White") canvas.draw_text(str(int(lives)), [40, 70], 10, "White") canvas.draw_text("Lives: ", [10, 70], 10, "White")
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 update(self): for object in self.objects: # goes through the array of players and ball updating if object.name == 5: # checks if it is the ball and updates it object.update() else: object.update( self.kbd ) # update players with keyboard input being recieved for o1 in self.objects: # goes through array to check collisions for o2 in self.objects: if o1.collides( o2) and o1 != o2: # for different players hitting ball if (o1.name + o2.name) not in self.inCollision: self.inCollision.add( o1.name + o2.name ) # when coliding add to set inCollision so only handled once when interacting n = (o1.pos - o2.pos).normalize( ) # normalise the vector position between ball and player delta = n * (o1.vel - o2.vel).dot( n) # get dot product of normal and velocity o1.vel.subtract( delta ) # make them opose, and going in different directions o2.vel.add(delta) else: self.inCollision.discard(o1.name + o2.name) # if not coliding if self.ball.pos.x < 0: # checking ball position if it has touched/ gone past goals, and resets position and velcoity score.hit_score2() # adds score to player 2 self.ball.vel = Vector() self.ball.pos = Vector(WIDTH / 2, HEIGHT / 2) self.chicken.pos = Vector(WIDTH - 50, HEIGHT / 2) self.chicken2.pos = Vector(0 + 50, HEIGHT / 2) elif self.ball.pos.x > WIDTH: score.hit_score() # adds score to player 1 self.ball.vel = Vector() self.ball.pos = Vector(WIDTH / 2, HEIGHT / 2) self.chicken.pos = Vector(WIDTH - 50, HEIGHT / 2) self.chicken2.pos = Vector(0 + 50, HEIGHT / 2)
def topVertVec(self): return Vector((self.topVert().x), (self.topVert().y))
def __init__(self, pos): self.pos = pos self.vel = Vector()
def _init_(self): self.pos = Vector(350, 100)
WISP_WIDTH = 1000 / 4 WISP_HEIGHT = 500 / 2 WISP_CENTER = [WISP_WIDTH / 2, WISP_HEIGHT / 2] WISP_SIZE = [WISP_WIDTH, WISP_HEIGHT] WISP_DIM = [4, 2] wisp_image = simplegui.load_image("https://i.imgur.com/tz5HfML.png") #Wisp 2 WISP2_WIDTH = 400 / 4 WISP2_HEIGHT = 200 / 2 WISP2_CENTER = [WISP2_WIDTH / 2, WISP2_HEIGHT / 2] WISP2_SIZE = [WISP2_WIDTH, WISP2_HEIGHT] WISP2_DIM = [4, 2] wisp2_image = simplegui.load_image("https://i.imgur.com/Hv0QHvY.png") #wisp shoot gun = RegularPolygon(2, Vector(490, 600), 10) gun1 = RegularPolygon(2, Vector(90, 563), 10) # Background image (Shilling ground floor) # WIDTH = 768 HEIGHT = 768 image_center = (384, 384) image_size = (750, 750) pos = [384, 384] image = simplegui.load_image("https://i.imgur.com/bFyXh2P.png") #Shilling first floor# WIDTHf = 768 HEIGHTf = 768 image_centerf = (384, 384) image_sizef = (750, 750)
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
def print2(self): return Vector((self.vertices[(self.sides + 1) % len(self.vertices)]) - (self.generator + self.pos))
def restart(self): self.vel = Vector() # restart position for players, when scored self.acc = Vector() # similar for acceleration
from user301_dZJL5znAdk_0 import Vector from user301_uxLQLcWnDf_99 import RegularPolygon from user301_FM91pKbpyD_3 import Keyboard from user301_n8rcr25g0U_0 import Particle import simplegui CANVAS_WIDTH = 2000 CANVAS_HEIGHT = 2000 kbd = Keyboard() pentagon = RegularPolygon(2, Vector(250, 250), 100, kbd, CANVAS_WIDTH, CANVAS_HEIGHT) particles = [] def draw(canvas): if kbd.space: p = Particle(pentagon.pos + pentagon.generator, pentagon.generator.getNormalized()) particles.append(p) for p in particles: p.draw(canvas) p.update() pentagon.draw(canvas) pentagon.drawGenerator(canvas) pentagon.update()
def forwardVel(self): return Vector( self.topVert - self.bottomVert ) #self.topVert.x #-self.bottomVert.x #)/10),((self.topVert.y-self.bottomVert.y)/10))
def forVec(self): return Vector((self.topVertVec().x - self.bottomVertVec().x), (self.topVertVec().y - self.bottomVertVec().y))
def bottomVertVec(self): return Vector((self.bottomVert().x), (self.bottomVert().y))
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)