def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # pygamehelper.addSprite(Drawer())
def eachFrame(self): if random.randint(1, 1000) > 995: # spawn new large asteroid newSpeed = 1 newVelocity = randomDirectionAsVector(newSpeed) pygamehelper.addSprite( Asteroid(randomX(), randomY(), newVelocity, 0, 1, 3))
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # pygamehelper.addSprite(LineDrawer(functionFromXCoordToYCoord))
def move(self): self.moveBy(1, 0) self.health = self.health - 1 if self.health == 0: self.dead = True # When an Invader dies, it drops a Bomb pygamehelper.addSprite(Bomb(self.x + self.width / 2, self.y, 100))
def addPlayerCardHolderSprites(self): addSprite( pygamehelper.SpriteWithText(20, 50, 40, 30, "Player", pygamehelper.mediumLargeFont, pygamehelper.white)) addSprite( pygamehelper.SpriteWithText(20, 200, 40, 30, "Computer", pygamehelper.mediumLargeFont, pygamehelper.white)) self.playerCardholders.append(Slot(100, 50)) self.playerCardholders.append(Slot(100, 200)) for s in self.playerCardholders: pygamehelper.addSprite(s) scoreYOffset = 30 self.playerScores.append( pygamehelper.SpriteWithText(20, 50 + scoreYOffset, 40, 30, "0", pygamehelper.largeFont, pygamehelper.white)) self.playerScores.append( pygamehelper.SpriteWithText(20, 200 + scoreYOffset, 40, 30, "0", pygamehelper.largeFont, pygamehelper.white)) for s in self.playerScores: pygamehelper.addSprite(s)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # self.tank = Tank(50, 300, (1, 0)) pygamehelper.addSprite(self.tank)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # # Create a couple of boxes which will bounce around pygamehelper.addSprite(CircleDrawer())
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # # Create a couple of boxes which will bounce around pygamehelper.addSprite(Box(20, 20, 110, 70, 125)) pygamehelper.addSprite(Box(20, 300, 45, 90, 90))
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # pygamehelper.addSprite( Invader(350, 250, (1, 1), 0, 1, "KillSpriteOnEdgeOfScreen")) self.rocket = Rocket(150, 150) pygamehelper.addSprite(self.rocket)
def eachFrame(self): # Intro message - only shows at the start of the game if pygamehelper.gameTick < 1000: showIntroBanner() # Randomly create some more invaders, at random locations on rows on the left of the screen if random.randint(1, 1000) < 20: newInvaderX = 20 newInvaderY = 20 * random.randint(1, 20) newInvaderHealth = 50 + random.randint(1, 200) pygamehelper.addSprite( Invader(newInvaderX, newInvaderY, newInvaderHealth))
def spawnSmallerAsteroidsOnDeath(self): # new asteroids will be a size smaller newSize = self.size - 1 # new speed: increases as size decreases newSpeed = max(3 - newSize, 1) # new angle change speed: increases as size decreases newAngleChangeSpeed = max((3 - newSize) * 2, 1) for i in range(0, 2): newVelocity = randomDirectionAsVector(newSpeed) pygamehelper.addSprite( Asteroid(self.x, self.y, newVelocity, 0, newAngleChangeSpeed, newSize))
def startUIForEvent(self, processingEvent): super().startUIForEvent(processingEvent) print( f"GameResultHandler startUIForEvent, processingEvent: {processingEvent}" ) result = processingEvent[1] self.message = SpriteWithText(30, 350, 300, 30, f"Game Result: {result}", pygamehelper.largeFont, pygamehelper.white) pygamehelper.addSprite(self.message)
def startUIForEvent(self, processingEvent): super().startUIForEvent(processingEvent) print( f"InputRequiredHandler startUIForEvent, processingEvent: {processingEvent}" ) self.stick = SpriteWithText(30, 350, 200, 30, "Stick", pygamehelper.largeFont, pygamehelper.white) pygamehelper.addSprite(self.stick) self.stick.onClick = self.onClickStick self.twist = SpriteWithText(250, 350, 200, 30, "Twist", pygamehelper.largeFont, pygamehelper.white) pygamehelper.addSprite(self.twist) self.twist.onClick = self.onClickTwist
def startUIForEvent(self, processingEvent): super().startUIForEvent(processingEvent) print( f"GameStartHandler startUIForEvent, processingEvent: {processingEvent}" ) # Show initial message for a few ticks self.sprite = pygamehelper.SpriteWithText( 30, 30, 200, 30, "Game Starting...", pygamehelper.largeFont, pygamehelper.white).withTimeout(50) pygamehelper.addSprite(self.sprite) # Add initial deck and player sprites self.gameLogicToUIAdaptor.addDeckSprites() self.gameLogicToUIAdaptor.addPlayerCardHolderSprites()
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # # Create a couple of holes pygamehelper.addSprite(Hole(50, 50)) pygamehelper.addSprite(Hole(150, 150)) pygamehelper.addSprite(Hole(250, 250)) self.hammer = Hammer(400, 400) pygamehelper.addSprite(self.hammer) pygamehelper.addSprite(Explosion(500,500))
def spawnNewInvader(self): newInvaderX = 350 newInvaderY = 250 newInvaderXVel = (random.randint(1, 30) - 15) / 10 newInvaderYVel = (random.randint(1, 30) - 15) / 10 newInvaderAngle = random.randint(1, 360) newInvaderAngleChangeSpeed = (random.randint(1, 100) - 50) / 10 # Some new invaders will Kill/Die on edge of screen, some will Bounce bounceMode = "KillSpriteOnEdgeOfScreen" if random.randint( 1, 10) <= 5 else "BounceSpriteOnEdgeOfScreen" newInvader = Invader(newInvaderX, newInvaderY, (newInvaderXVel, newInvaderYVel), newInvaderAngle, newInvaderAngleChangeSpeed, bounceMode) pygamehelper.addSprite(newInvader)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # pygamehelper.addSprite(Asteroid(150, 150, (1, -1), 0, 1, 3)) pygamehelper.addSprite(Asteroid(250, 450, (1, 1), 0, 1, 3)) pygamehelper.addSprite(Asteroid(350, 450, (1, 1), 0, 1, 3)) self.playerShip = PlayerShip(centreOfScreenVector.x, centreOfScreenVector.y) pygamehelper.addSprite(self.playerShip)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # # Setup a path to draw and follow path = Path() path.addWaypoint(0, 0) path.addWaypoint(50, 50) path.addWaypoint(300, 50) path.addWaypoint(400, 175) path.addWaypoint(400, 300) path.addWaypoint(50, 300) path.addWaypoint(50, 50) # PathDrawer will draw the path as lines pygamehelper.addSprite(PathDrawer(path)) # PathFollowSprite will move along the path pygamehelper.addSprite(PathFollowSprite(path)) # Tag an existing Invader sprite with a moveHandler invader = Invader(20, 20, (1, 1), 0, 1) # We use speed 3 for the invader and this now all works PathFollowMoveHandler.installForSprite(invader, path).setSpeed(3) # TODO: We could tailor the pathFollowMoveHandler here? e.g. with a fluent ".withStopAtEndOfPath" method? # PathFollowMoveHandler.installForSprite(invader, path).withStopAtEndOfPath() # TODO: Add a basic test for this also pygamehelper.addSprite(invader)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # pygamehelper.addSprite(Invader(20, 20, 100)) pygamehelper.addSprite(Invader(20, 50, 100)) pygamehelper.addSprite(Invader(20, 80, 100))
def startUIForEvent(self, processingEvent): super().startUIForEvent(processingEvent) print( f"PlayerCardHandler startUIForEvent, processingEvent: {processingEvent}" ) # cardDealt is of the form: ['Clubs', 4] cardDealt = processingEvent[1] suitOffset = cardSuitToCostumeOffset[cardDealt[0]] cardCostumeIdx = cardNumOrNameToCardCostume[cardDealt[1]] + suitOffset # Create the card sprite and start it moving to the player's holder ("hand") self.card = Card(250, 50, cardCostumeIdx) pygamehelper.addSprite(self.card) destination = self.gameLogicToUIAdaptor.getPlayerCardholder( self.playerNum).getLocation() # adjust destination depending on how many cards the player already has self.gameLogicToUIAdaptor.addPlayerCard(self.playerNum, processingEvent[1]) offset = 3 * len( self.gameLogicToUIAdaptor.getPlayerCards(self.playerNum)) destination = (destination[0] + offset, destination[1] + offset) # move the new card to the destination self.card.setLocationAnimated(destination, 3)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # # Create a couple of boxes which will bounce around pygamehelper.addSprite(Box(20, 20, 110)) pygamehelper.addSprite(Box(20, 300, 45)) # object to keep track of turns self.turnKeeper = Turnkeeper(140, 20) pygamehelper.addSprite(self.turnKeeper)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # # Create sprites self.backgroundImage = BackgroundImage() pygamehelper.addSprite(self.backgroundImage) # Needs to know when zoom level is changed zoomHelper.addZoomChangedListener(self.backgroundImage) self.selectionTool = SelectionTool(50, 50) pygamehelper.addSprite(self.selectionTool) # Intro message # TODO: Allow multi line strings pygamehelper.addSprite( SpriteWithText(30, 30, 200, 30, "Welcome to my editor example", pygamehelper.largeFont, pygamehelper.white).withTimeout(200)) pygamehelper.addSprite( SpriteWithText(30, 60, 200, 30, "- Left click and drag to draw a rectangle", pygamehelper.largeFont, pygamehelper.white).withTimeout(220)) pygamehelper.addSprite( SpriteWithText( 30, 90, 200, 30, "- Left click inside the rectangle and drag it to move it", pygamehelper.largeFont, pygamehelper.white).withTimeout(240)) pygamehelper.addSprite( SpriteWithText(30, 120, 200, 30, "- Right click and drag to pan around", pygamehelper.largeFont, pygamehelper.white).withTimeout(260)) pygamehelper.addSprite( SpriteWithText(30, 150, 200, 30, "- Mouse wheel to zoom in and out", pygamehelper.largeFont, pygamehelper.white).withTimeout(280))
def eachFrame(self): # spawn a new square at random if random.randint(1, 1000) > 975: pygamehelper.addSprite( Box(randomX() - 20, randomY() - 20, randomDirection()))
def move(self): if self.egg == None: if random.randint(1,10000) > 9950: self.egg = Egg(self.x, self.y + 20, self) pygamehelper.addSprite(self.egg)
def __init__(self): super().__init__() # # TODO # Create any initial instances of your sprites here # # Draws the grid self.gridHelper = GridHelper() pygamehelper.addSprite(self.gridHelper) # Object to keep track of turns self.turnKeeper = Turnkeeper(140, 20) pygamehelper.addSprite(self.turnKeeper) # Initial board setup pygamehelper.addSprite( GridPiece(self.gridHelper, 0, 0, self.turnKeeper.getPlayer(1))) pygamehelper.addSprite( GridPiece(self.gridHelper, 1, 1, self.turnKeeper.getPlayer(1))) pygamehelper.addSprite( GridPiece(self.gridHelper, 5, 1, self.turnKeeper.getPlayer(2))) pygamehelper.addSprite( GridPiece(self.gridHelper, 5, 2, self.turnKeeper.getPlayer(2)))
# TODO: This could/should possibly be a sprite? Maybe an exercise for a bright child or adult def showIntroBanner(): messages = ["Hi there", "Welcome to my game", "Hope you'll enjoy it"] currentMessageIndex = int(pygamehelper.gameTick / 100) if currentMessageIndex <= len(messages) - 1: currentMessage = messages[currentMessageIndex] bannerY = 200 + pygamehelper.gameTick % 100 pygamehelper.drawText(currentMessage, 200, bannerY, pygamehelper.hugeFont, pygamehelper.green) # # TODO # Create any initial instances of your sprites here # pygamehelper.addSprite(Invader(20, 20, 100)) pygamehelper.addSprite(Invader(20, 50, 100)) pygamehelper.addSprite(Invader(20, 80, 100)) gameExit = False while not gameExit: # # TODO # Handle any key events in here - maybe we can make this easier? # for event in pygame.event.get(): if event.type == pygamehelper.pygame.QUIT: pygamehelper.pygame.quit() quit() # clear screen area
self.playerScores.append( pygamehelper.SpriteWithText(20, 200 + scoreYOffset, 40, 30, "0", pygamehelper.largeFont, pygamehelper.white)) for s in self.playerScores: pygamehelper.addSprite(s) def getPlayerCardholder(self, playerNum): return self.playerCardholders[playerNum - 1] def updatePlayerScore(self, playerNum, newScore): self.playerScores[playerNum - 1].text = str(newScore) # response is STICK or TWIST def playerHasClicked(self, response): self.blackjackGame.input = 's' if response == "STICK" else 't' def addPlayerCard(self, playerNum, card): self.playerCards[playerNum - 1].append(card) def getPlayerCards(self, playerNum): return self.playerCards[playerNum - 1] pygamehelper.addSprite(GameLogicToUIAdaptor()) # Create game loop and UI controller code g = MyGameLoop() # Run UI game loop at 60 fps g.runGameLoop()
def addDeckSprites(self): for i in range(1, 10): ii = i * 6 s = Card(300 + ii, 50 + ii, i) self.deckSprites.append(s) pygamehelper.addSprite(s)