def update(self, round): #updates the movements for both character self.movements(self.characters[0], self.characters[1], 0) self.movements(self.characters[1], self.characters[0], 1) #checks for wall collisions for wall in self.walls: for character in self.characters: #moves characters out of walls if collision occurs if wall.touch(character) == 'right': character.pos = Vector(wall.lEdge - (character.sprite.scaling * character.sprite.spriteDim[0] / 2), character.pos.getY()) elif wall.touch(character) == 'left': character.pos = Vector(wall.rEdge + (character.sprite.scaling * character.sprite.spriteDim[0] / 2), character.pos.getY()) #checks for platform collisions for platform in self.platforms: for character in self.characters: if platform.touch(character): #stops falling on collision with platform character.stop_fall() else: #fall character.fall() #make sure no character goes under platform if (character.pos.getY() + (character.sprite.scaling * character.sprite.spriteDim[1] / 2)) - 4 > platform.edge_top: character.pos = Vector(character.pos.getX(), platform.edge_top - (character.sprite.scaling * character.sprite.spriteDim[1] / 2) - 4) for character in self.characters: if character.dead: round.over(character)
def movements(self,character, other, bool): #movements that are only available when not blocking if not character.dead: if not self.keyboard.down[bool]: character.not_blocking() # x velocity can only be changed when not jumping (no midair direction changes) if self.keyboard.left[bool] and not character.jumping: character.vel = Vector(-1, character.vel.getY()) character.setState("walk") if self.keyboard.right[bool] and not character.jumping: character.vel = Vector(1, character.vel.getY()) character.setState("walk") # no double jumping if self.keyboard.up[bool] and not character.jumping: character.attemptJump() # can attack if jumping (attack becomes jump kick if self.keyboard.attack[bool]: character.punch(other) # fires a fireball if the character is on the ground if self.keyboard.fire[bool] and not character.jumping: character.fire(other) if self.keyboard.idle[bool] and not character.jumping: character.setState("idle") #can block if not jumping if self.keyboard.down[bool] and not character.jumping: character.blocking() character.setState("block") character.move() character.setfacing(other)
def __init__(self, player, direction): self.character = player self.stoppedPos = Vector(-100, -100) #off canvas self.pos = self.stoppedPos self.direction = direction #self.sprite = self.set_sprite() # gets correct sprite self.vel = Vector() self.sprite = self.set_sprite("Red")
def mainMenuButtons(click): radius = 36 red = Vector(246, 74) yel = Vector(246, 157) gre = Vector(246, 240) cenToClick = click.copy().subtract( red.copy()) # dist between centre of ball and click if (cenToClick.length() <= radius): # check click is in ball quit(0) cenToClick = click.copy().subtract( yel.copy()) # dist between centre of ball and click if (cenToClick.length() <= radius): # check click is in ball Master.instructions() cenToClick = click.copy().subtract( gre.copy()) # dist between centre of ball and click if (cenToClick.length() <= radius): # check click is in ball # Master.gameLoop() Master.modeSelect()
def gameMenuButtons(click): #Red, yel and green correspond to the position of the buttons displayed during the game radius = 20 red = Vector(30, 40) yel = Vector(80, 40) gre = Vector(130, 40) cenToClick = click.copy().subtract( red.copy()) # dist between centre of ball and click if (cenToClick.length() <= radius): # check click is in ball quit(0) cenToClick = click.copy().subtract( yel.copy()) # dist between centre of ball and click if (cenToClick.length() <= radius): # check click is in ball Master.instructions() cenToClick = click.copy().subtract( gre.copy()) # dist between centre of ball and click if (cenToClick.length() <= radius): # check click is in ball # Master.gameLoop() Master.menu()
def multiplayer(click): centerY = 235 buttonWidth = 120 multi = Vector(150, centerY) solo = Vector(350, centerY) if ((abs(click.getY() - centerY)) < 85): if abs(click.getX() - (multi.copy().getX())) < buttonWidth / 2: Ai.level = 0 Mouse.multiNoSel = True elif abs(click.getX() - (solo.copy().getX())) < buttonWidth / 2: Ai.level = 1 Mouse.multiNoSel = True
def handler(position): click = Vector(position[0], position[1]) if Mouse.screen == "splash": #todo change to dict Master.menu() elif Mouse.screen == "menu": Mouse.mainMenuButtons(click) elif Mouse.screen == "instructions": Master.menu() elif Mouse.screen == "game": Mouse.gameMenuButtons(click) elif Mouse.screen == "MultOrNo": Mouse.multiplayer(click) elif Mouse.screen == "lvlSelect": Mouse.lvlSelect(click) elif Mouse.screen == "spriteSelect": Mouse.spriSel(click) else: pass
CANVAS_WIDTH = 500 CANVAS_HEIGHT = 500 has_ai = False sprites = { "Red": "https://i.ibb.co/M7Ff2wx/redsheet.png", "Blue": "https://i.ibb.co/2hv9qcq/bluesheet.png", "Green": "https://i.ibb.co/K2616MG/greensheet.png", "Yellow": "https://i.ibb.co/CbMDdVC/yellowsheet.png", "Base": "https://i.ibb.co/6m5sd2h/fullsheet.png" } SPRITE1 = "Red" SPRITE2 = "Blue" player1Sprite = Sprite(sprites[SPRITE1], 2, 14, (100, 300), 4, "idle", "left") player2Sprite = Sprite(sprites[SPRITE2], 2, 14, (400, 300), 4, "idle", "right") ##Get Sprite sheets player1 = Character(player1Sprite, Vector(100, 300), 1, 'right') player2 = Character(player2Sprite, Vector(400, 300), 2, 'left') round = Rounds(player1, player2) #when GameLoop is called by a class, init starts the frame def init(sp1, sp2, ai): global SPRITE1, SPRITE2, has_ai has_ai = ai kbd.ai = has_ai SPRITE1 = sp1 SPRITE2 = sp2 Master.masterframe.setDrawHandler(draw) Mouse.screen = "game" #Master.masterframe.set_canvas_background('rgba(0, 200, 200, 0.3)')
def stop(self): self.pos = self.stoppedPos self.vel = Vector() self.character.fireball_ready = True
def setvel(self, direction, x=0): if direction == 'left': vel = Vector(-x, self.vel.getY()) else: vel = Vector(x, self.vel.getY()) return vel
class Fireball: #initialises fireball with a player and a direction #if fireball is not being fired, it is stored off canvas (-100,-100) def __init__(self, player, direction): self.character = player self.stoppedPos = Vector(-100, -100) #off canvas self.pos = self.stoppedPos self.direction = direction #self.sprite = self.set_sprite() # gets correct sprite self.vel = Vector() self.sprite = self.set_sprite("Red") #sets velocity depending on direction def setvel(self, direction, x=0): if direction == 'left': vel = Vector(-x, self.vel.getY()) else: vel = Vector(x, self.vel.getY()) return vel #moves fireball def move(self): self.pos = self.pos + self.vel #fires a fireball from a position def shoot(self, starting, direction): self.pos = starting self.direction = direction self.vel = self.setvel(self.direction, 10) self.character.fireball_ready = False #stops fireball once a limit is reached def stop(self): self.pos = self.stoppedPos self.vel = Vector() self.character.fireball_ready = True #sets fireball sprite def set_sprite(self, colour): self.sprite = Sprite(sprites[colour], 2, 4, self.pos, 1, "fireball", self.direction) def setFacing(self, facing): self.direction = facing self.sprite.setFacing(self.direction) self.sprite.updateDirection() #draws the sprite on the canvas def draw(self, canvas): self.sprite.update(canvas, self.pos.getP()) #looping method def update(self, canvas, enemy): self.draw(canvas) self.move() #bounds for stopping fireball if enemy.check_hit(self.pos): enemy.hit(15) self.stop() if self.off_screen(): self.stop() #boundary for screen def off_screen(self): return self.pos.x < 0 or self.pos.x > 500
def lvlSelect(click): spriteWidth = 100 spriteHeight = 170 centerY = 235 lvl1 = Vector(1 * spriteWidth, centerY) lvl2 = Vector(2 * spriteWidth, centerY) lvl3 = Vector(3 * spriteWidth, centerY) lvl4 = Vector(4 * spriteWidth, centerY) if ((abs(click.getY() - centerY)) < spriteHeight / 2): if abs(click.getX() - (lvl1.copy().getX())) < spriteWidth / 2: Ai.level = 1 Mouse.levelSel = True elif abs(click.getX() - (lvl2.copy().getX())) < spriteWidth / 2: Ai.level = 2 Mouse.levelSel = True elif abs(click.getX() - (lvl3.copy().getX())) < spriteWidth / 2: Ai.level = 3 Mouse.levelSel = True elif abs(click.getX() - (lvl4.copy().getX())) < spriteWidth / 2: Ai.level = 4 Mouse.levelSel = True
def spriSel(click): rows = (180, 260) columns = (187.5, 312.5) red = Vector(columns[0], rows[0]) green = Vector(columns[1], rows[0]) blue = Vector(columns[0], rows[1]) yell = Vector(columns[1], rows[1]) imgWidth = 125 imgHeight = 160 if (abs(click.getY() - rows[0])) < imgHeight / 2: if abs(click.getX() - (red.copy().getX())) < imgWidth / 2: Mouse.spriteSel2 = Mouse.spriteSel1 Mouse.spriteSel1 = True Mouse.spriteSelection = [0, 0] elif abs(click.getX() - (green.copy().getX())) < imgWidth / 2: Mouse.spriteSel2 = Mouse.spriteSel1 Mouse.spriteSel1 = True Mouse.spriteSelection = [1, 0] elif (abs(click.getY() - rows[1])) < imgHeight / 2: if abs(click.getX() - (blue.copy().getX())) < imgWidth / 2: Mouse.spriteSel2 = Mouse.spriteSel1 Mouse.spriteSel1 = True Mouse.spriteSelection = [0, 1] elif abs(click.getX() - (yell.copy().getX())) < imgWidth / 2: Mouse.spriteSel2 = Mouse.spriteSel1 Mouse.spriteSel1 = True Mouse.spriteSelection = [1, 1]