1 / 20

Python Introduction continued…

Python Introduction continued…. Professor Frank J. Rinaldo Creation Core - office 401. Random Numbers. Random number are used extensive in games It makes game unpredictable We use the ‘random’ library in Python Example: import random dice1 = random.randrange(6) + 1

randi
Download Presentation

Python Introduction continued…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Introductioncontinued… Professor Frank J. Rinaldo Creation Core - office 401

  2. Random Numbers • Random number are used extensive in games • It makes game unpredictable • We use the ‘random’ library in Python • Example: import random dice1 = random.randrange(6) + 1 • Randrange(6) is 0,1,2,3,4,5 • In other words, from 0 to <6

  3. IF Statement • Examples if mood == 0: print ‘it is a zero’ if mood == 0: print ‘it is a zero’ else: print ‘it is non-zero’ if mood == 0: print ‘it is zero’ elif mood < 0: print ‘it is negative’ else: print ‘it is positive’

  4. WHILE statement • Example while health != 0: trolls += 1 health = health – damage print ‘still in the WHILE loop’ print ‘now outside the WHILE loop’

  5. BREAK & CONTINUE statements • The ‘break’ command will exit a loop • The ‘continue’ statement will go to end of loop • Example: while True: count += 1 if count > 10: break if count == 5; continue print count

  6. AND and OR Statements • Examples: if x > 0 and y > 0: x++ print x,y if x > 0 or y > 0: y++ print x,y

  7. FOR loop statement • Examples: word = “Hello” count = 0 for letter in word: print letter count++ print ‘the word has ‘,count, ‘ letters’

  8. FOR loop continued for i in range(10): print i # will print 0,1,2,3,4,5,6,7,8,9 for i in range(0, 50, 5): print i # will print 0,5,10,15,20,25,30,35,40,45 for i in range(10, 0, -1): print i # will print 10,9,8,7,6,5,4,3,2,1

  9. Strings • Example: word = “index” high = len(word) low = -len(word) print “Word is “, word, “\n” for position in range(low,high): print “word[“, position, “]\t”, word[position] # output will be: index word[0] i word[1] n word[2] d word[3] e word[4] x

  10. Strings continued • Concatenation – combine strings newstring = string1 + string2 • Slicing – access substrings word = “pizza” print word[0,5] # prints ‘pizza’ print word[0,4] # prints ‘pizz’ print word[1,3] # prints ‘iz’

  11. Tuples • Tuples are a list ‘like a string’ • Strings can only contain letters • Tuples can contain other elements of any type • You can index, slice, and concatenate tuples

  12. Tuples continued • Example inventory = () # create an empty tuple if not inventory: print “You are empty handed” inventory = (“sword”, “armor”, “shield”, “healing potion”) for item in inventory: print item

  13. Creating a tuple # Hero's Inventory # Demonstrates tuple creation # Michael Dawson - 1/29/03 # create an empty tuple inventory = () # treat the tuple as a condition if not inventory: print "You are empty-handed." raw_input("\nPress the enter key to continue.") # create a tuple with some items inventory = ("sword", "armor", "shield", "healing potion") # print the tuple print "\nThe tuple inventory is:\n", inventory # print each element in the tuple print "\nYour items:" for item in inventory: print item raw_input("\n\nPress the enter key to exit.")

  14. Tuple 2 # Hero's Inventory 2.0 # Demonstrates tuples # Michael Dawson - 1/29/03 # create a tuple with some items and display with a for loop inventory = ("sword", "armor", "shield", "healing potion") print "Your items:" for item in inventory: print item raw_input("\nPress the enter key to continue.") # get the length of a tuple print "You have", len(inventory), "items in your possession." raw_input("\nPress the enter key to continue.") # test for membership with in if "healing potion" in inventory: print "You will live to fight another day."

  15. Tuple 2 … cont, # display one item through an index index = int(raw_input("\nEnter the index number for an item in inventory: ")) print "At index", index, "is", inventory[index] # display a slice begin = int(raw_input("\nEnter the index number to begin a slice: ")) end = int(raw_input("Enter the index number to end the slice: ")) print "inventory[", begin, ":", end, "]\t\t", print inventory[begin:end] raw_input("\nPress the enter key to continue.") # concatinate two tuples chest = ("gold", "gems") print "You find a chest. It contains:" print chest print "You add the contents of the chest to your inventory." inventory += chest print "Your inventory is now:" print inventory raw_input("\n\nPress the enter key to exit.")

  16. In-Class project • Write a program to process a Hero’s inventory • It must have the following features: • Insert an item into inventory • Display all items in inventory • Display a particular item in inventory • Check it the Hero has an item in inventory • Delete an item from the inventory

  17. Word Jumble # Word Jumble # # The computer picks a random word and then "jumbles" it # The player has to guess the original word # # Michael Dawson - 1/28/03 import random # create a sequence of words to choose from WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone") # pick one word randomly from the sequence word = random.choice(WORDS) # create a variable to use later to see if the guess is correct correct = word # create a jumbled version of the word jumble ="" while word: position = random.randrange(len(word)) jumble += word[position] word = word[:position] + word[(position + 1):]

  18. Word Jumble… cont. # start the game print \ """ Welcome to Word Jumble! Unscramble the letters to make a word. (Press the enter key at the prompt to quit.) """ print "The jumble is:", jumble guess = raw_input("\nYour guess: ") guess = guess.lower() while (guess != correct) and (guess != ""): print "Sorry, that's not it." guess = raw_input("Your guess: ") guess = guess.lower() if guess == correct: print "That's it! You guessed it!\n" print "Thanks for playing." raw_input("\n\nPress the enter key to exit.")

  19. Class Program • Write a Python Program in class to: • Create a game where the computer picks a random word and the player has to guess that word. The computer tells the player how many letters are in the word. Then the player gets 5 chances to ask if a letter is in the word. The computer can only respond with “yes” or “no”. Then, the player must guess the word. • Include: • Simple Specification Document • Simple Pseudocode • Python code • Demonstrate program in class

  20. Homework • Due Week 4 in Class • Write a Python Program to: • Write a Character Creator program for a role-playing game. The player should be given a pool of 30 points to spend on four attributes: Strength, Health, Wisdom, and Dexterity. The player should be able to spend points from the pool on any attribute and should be able to take points from an attribute an dput them back into the pool. • Include: • Simple Specification Document • Simple Pseudocode • Python code • Demonstrate program in class

More Related