yum yum gems # GAME SET UP # Spawns the objects for the game: the avatar, the gem, and the enemy. avatar = game.spawnObject("turtle") avatar.setControl("arrows") gem = game.spawnObject("gem-green", "random", "random") enemy = game.spawnObject("spider") enemy.setChaseTarget(gem) enemy.setSpeed(0.8) # Shows the directions of the game to the player. game.addTextDirections("Use arrow keys to help the turtle eat the gems before the enemy does!") points = 0 # Defines a function that moves the gem to a random location. def moveGem(): gem.setX("random") gem.setY("random") # GAME LOOP # The while loop repeats rounds of the game until 60 seconds have passed. # Each round updates displays and checks if objects have hit the gem. while game.timePassed() < 60: game.advanceGame() game.setDisplay("TIME", game.timePassed()) game.setDisplay("SCORE", points) if avatar.hit(gem): points += 1 moveGem() if enemy.hit(gem): moveGem() # GAME END # After the loop ends, the conditional checks the point score. # Depending on the number of points, the player sees a win or an end screen. if points >= 10: game.win("You got " + points + " points! You Win!") else: game.end("You got <20 points! Try Again.") rock catches leaf # GAME SET UP # Creates the rock and displays the number of rocks that players get. rock = game.spawnObject("rock-clay", 4.5, 2) # Creates a paddle made of 3 boxes that is moved together with arrow keys. paddle1 = game.spawnObject("box", 4, 0) paddle2 = game.spawnObject("box", 4.5, 0) paddle3 = game.spawnObject("box", 5, 0) paddle1.setControl("arrows") paddle2.setControl("arrows") paddle3.setControl("arrows") # Creates leaves that spell out the word "OK" for the rock to hit. game.spawnObject("leaf-red", 1, 6) game.spawnObject("leaf-red", 1, 5) game.spawnObject("leaf-red", 1, 4) game.spawnObject("leaf-red", 1, 3) game.spawnObject("leaf-red", 2, 6) game.spawnObject("leaf-red", 2, 3) game.spawnObject("leaf-red", 3, 6) game.spawnObject("leaf-red", 3, 5) game.spawnObject("leaf-red", 3, 4) game.spawnObject("leaf-red", 3, 3) game.spawnObject("leaf-red", 5, 6) game.spawnObject("leaf-red", 5, 5) game.spawnObject("leaf-red", 5, 4) game.spawnObject("leaf-red", 5, 3) game.spawnObject("leaf-red", 6, 5) game.spawnObject("leaf-red", 6, 4) game.spawnObject("leaf-red", 7, 6) game.spawnObject("leaf-red", 7, 3) game.spawnObject("leaf-red", 9, 3) # Shows the directions of the game. game.addTextDirections("Catch all the leaves!") game.setBorders("infinite") #Creates global variables that will be used during the game. leavesToCatch = 19 #the number of leaves spawned above numOfRocks = 3 ROCK_SPEED = 0.1 rockVX = -ROCK_SPEED rockVY = ROCK_SPEED # Displays the number of rocks to the player. game.setDisplay("rocks", numOfRocks) # This function resets the rock to its starting location and movement direction. def initRock(): global rockVX, rockVY, ROCK_SPEED rockVX = -ROCK_SPEED rockVY = ROCK_SPEED rock.setXY(4.5, 1.5) # These functions set up how the rock moves when it hits an object. def bounceLeftRight(): global rockVX rockVX = -rockVX def bounceUpDown(): global rockVY rockVY = -rockVY # This function is called if the rock hits a leaf. def removeLeaf(leaf): global leavesToCatch # The rock's direction is changed. if leaf.getY() - rock.getY() > .4: bounceUpDown() elif leaf.getY() - rock.getY() < -.4: bounceUpDown() else: bounceLeftRight() # The leaf that is caught is hidden. leaf.hide() leavesToCatch = leavesToCatch - 1 #If all the leaves are caught, then the player gets the win screen! if leavesToCatch == 0: rock.hide() game.win("You Did It!") # GAME LOOP # The for loop plays 9999 rounds of the game. # Look at comments inside the loop to find out what happens in each round. for i in range(9999): game.advanceGame() # Moves the rock forward with its current direction and speed. rock.setX(rock.getX() + rockVX) rock.setY(rock.getY() + rockVY) paddle1.setY(0) #only move left/right paddle2.setY(0) #only move left/right paddle3.setY(0) #only move left/right # Checks to see if the rock hits the paddle. # If so, it changes the rock's direction, depending on its location. hitPaddle = rock.hit("box") if hitPaddle: rockVY = ROCK_SPEED # Checks to see if the rock hits a side of the game screen. # If so, it changes the rock's direction, depending on its location. if rock.getX() <= -0.2: rockVX = ROCK_SPEED if rock.getX() > 9.2: rockVX = -ROCK_SPEED if rock.getY() > 7.2: rockVY = -ROCK_SPEED # Checks to see if the rock hits any of the leaves. #I f so, it calls the removeLeaf function using the leaf that's hit. rockHitLeaf = rock.hit("leaf-red") if rockHitLeaf: removeLeaf(rock.findNearest("leaf-red")) # Checks to see if the rock has fallen to the bottom of the screen. # If so, then the players loses a rock. if rock.getY() < -0.5: numOfRocks = numOfRocks - 1 game.setDisplay("rocks", numOfRocks) if numOfRocks > 0: initRock() else: rock.hide() game.end("WOMP WOMP") moth battle # GAME SET UP # Creates the objects for the game: a moth, a skull, and enemies. enemy1 = game.spawnObject("storm","center", -1) enemy2 = game.spawnObject("storm", "center", -1) enemy3 = game.spawnObject("storm", "center", -1) skull = game.spawnObject("skull","center", "bottom") skull.setScale(0.5) moth = game.spawnObject("moth","center", "bottom") moth.setControl("wasd") # Shows the directions to the player and sets up the border. game.addTextDirections("Defeat the enemies with skulls of fire! Use WASD keys and don't get hit or touch the walls.") game.setBorders("infinite") # Creates variables that will be used during the game. width = 6 mothSpeed = 0.1 skullSpeed = 0.2 points = 0 lives = 3 # Displays the points and lives for the player game.setDisplay("points", points) game.setDisplay("life", lives) # These functions reset objects to new starting locations. def initAvatar(): moth.setXY("center", "bottom") def initEnemy(enemy): mothSpeed = mothSpeed + 0.005 enemy.setXY(game.random(1, 8), 7) def initSkull(): skull.setXY(moth.getX(), moth.getY()) # This function moves an enemy one step down the screen. def enemyStep(enemy): enemy.setY(enemy.getY() - mothSpeed) # This function checks if an enemy has reached the bottom of the game screen. If it has, then its location resets. def checkEnemy(enemy): if enemy.getY() > 0: enemyStep(enemy) else: initEnemy(enemy) # Builds a wall on the left and right side of the game screen. for i in range(8): game.spawnObject("wall", 0, i) game.spawnObject("wall", 9, i) # GAME LOOP # The for loop plays 9999 rounds of the game. # Look at comments inside the loop to find out what happens in each round. for frame in range(9999): game.advanceGame() game.setDisplay("points", points) # Checks the location of each enemy and moves them one step down the screen. checkEnemy(enemy1) checkEnemy(enemy2) checkEnemy(enemy3) # Moves the skull one step up the game screen. skull.setY(skull.getY() + skullSpeed) # Checks if the skull hits any enemy. # If so, the player gets a point, the enemy resets, and the moth starts moving faster. skullHit = skull.hit("storm") if skullHit: points = points + 1 thisStorm = skull.findNearest("storm") initEnemy(thisStorm) # respawn immediately initSkull() mothSpeed = mothSpeed + 0.01 # every time you defeat an enemy, the moth gets faster # Moves the skull back to the moth once it moves off the screen. if skull.getY() > 7.5: initSkull() # Checks if the moth has hit one of the enemies. # If so, the player loses a life and the moth location is reset. crashEnemy = moth.hit("storm") if crashEnemy: points = points + 1 lives = lives - 1 game.setDisplay("life", lives) initAvatar() # If the player runs out of lives, then the game ends. if lives<1: game.end("GAME OVER") # Checks if the moth has hit one of the walls. # If so, the game ends. crashWall = moth.hit("wall") if crashWall: game.end("GAME OVER") # Keeps the moth from moving off the screen if moth.getY()>7: moth.setY(7) elif moth.getY()<0: moth.setY(0)