Python Beginners- Week 5: Adventure Game and Comparing Python Functions to Scratch Blocks

Today we finished our adventure game. Here is the source for it.

</pre>
#This is my adventure game

import time

def scene1():
 print('You are standing on a trail in a forrest')
 print('Before you the trail splits in two')
 print('Which way will you go right or left?''\n')
def makeChoice():
 choice = ''
 while choice != '1' and choice != '2':
 print('Press 1 followed by enter to choose the first option')
 print('Press 2 followed by enter to choose the second option')
 choice = input()
 return choice

def scene2A():
 print('\n''You come to a stream with bridge')
 print('You can cross and follow the trail that leads from the bridge')
 print('or follow the trail that leads along the nearside of the river''\n')

def scene2B():
 print('\n''you come to a steep hill')
 print('you can climb over the hill')
 print('or you can follow the trail which leads around it','\n')

def goodEnd():
 print('You travel along until you hear people''\n')
 time.sleep(1)
 print('They sound like they are having fun''\n')
 time.sleep(1)
 print('You suddenly see them and realise it\'s your friends having a picnic')
 print ('they share their food and drinks with you and you have a great time')

def badEnd():
 print('You travel along''\n')
 time.sleep(1)
 print ('on and on''\n')
 time.sleep(1)
 print('you have no idea where you are''\n')
 time.sleep(1)
 print('suddenly you realise you are back where you started')
#main program starts here
playAgain = 'yes'
while playAgain == 'yes' or playAgain == 'y':

scene1()

#use the makeChoice function to get
 #the player to decide which way to go
 firstChoice = makeChoice()

#this if else statement will show the next
 #scene based on the players choice
 if firstChoice == '1':
 scene2A()
 else:
 scene2B()

secondChoice = makeChoice()

#this if else statement will show the next
 #scene based on the players choice
 if secondChoice == '1':
 goodEnd()
 else:
 badEnd()

print('Do you want to play again? (yes or no)')
 playAgain = input()

We also took a look at Snap which is an extended re-implementation of Scratch that allows us to build our own blocks. Snap is available at http://snap.berkeley.edu/. We compared our Python code to a similar Snap script.

Capture4

We compared Python functions to Scratch blocks and made a few blocks of our own using Snap. Here are my slides from today. python session5

Beginners Scratch – Challenge 5 – A Better GhostBuster Game!

This week, our lead mentor was Declan Fox, who did a great job.

The challenge was to enhance our GhostBuster game from out last challenge.  The game involves PacMan character, controlled by the mouse, who has to catch ghosts that move at random.

This week, we added levels that make it go faster and change the colour of the background, and we added animations (PacMan chomping, ghosts blinking & sticking out their tongues), and background music.

Coding concepts that we discussed included:

  1. Loops
  2. Animation
  3. Broadcasting to communicate between sprites

Here are the notes from the day (PDF): CDA-S2-Challenge05-BetterGhostCatcher.pdf

This is my version of the updated game: http://scratch.mit.edu/projects/cdathenry/2485407

If you would like us to send you the PowerPoint slides of my notes, get in touch via twitter or by leaving a comment.

Advanced Scratch – Week 5 – Code Recycling and parallel processing

What are we going to learn this week.

  • Buzzer Game – Using old code and a little new code we can build a game.
  • Code recycling – How to re-use code that you have already written.
  • Parrallel vs Sequential Processing – sounds worse than it is 🙂

Buzzer Game

Navigate through the Maze to earn More Money, the more you get the Harder the game becomes.

Most of the code for this game you have already written, cool… But there is some  new code that you will need.

So, lets do the New code first…

We need a coin, or something that you can move with the mouse and that will BUZZ and Shake when it touches a particular colour.

This is my Coin here…

I have one costume for normal and one costume fo buzzing!

We now need to set up some code so that you can move it with the mouse, this is similar code to the button code we did, and I found a better way of doing it!

What you want to happen is if the mouse is on the Coin you can Click to grab it and it will move wherever the mouse moves until you let go of the mouse.

We also want it to Buzz! when it touches a particluar colour, to test this I just drew some lines on the Stage and used the Dropper tool to select the right colour.

SEQUENTIAL PROCESSING – This is my Buzz code, I just move it around a little and make a noise. Notice on the Broadcast that it is a Broadcast and Wait, this allows the Buzz code to work correctly.

 

And the code that allows you to click and drag the coin around the Screen goes something like this…

Advanced Tip 1. To test this piece of code, you will need to run your program in full screen mode, otherwise Scratch is in “design” mode and moves the Sprite anyway.

The Maze.

CODE RECYCLING PART 1. Remember last week whne you did the Truchet Patterns, they were pretty maze like, weren’t they. Well all we need to do is import the Truchet Sprite, maybe play around with it a little bit and you will have your maze.

What I changed in my Truchet Pattern, was the size of the initial square, I made it bigger, and also I didn’t stamp along the top line or the bottom line so the coin has somewhere to aim for.

The other thing that you might have to change is if you have used curved lines, you might want to change them for straight lines, otherwise the gaps between them can be very close. Alternatively you can make your coin very small.

So once I had adjusted my pattern, I ended up getting something like this.

CODE RECYCLING PART 2. Remember the Button we did, well let’s bring that into the Game as well so we can use it to Start everything off

Now you need to connect everything together. This is where the fun Starts…. 🙂