Python 2.7 will also work for this workshop due to certain language rules, however I prefer working with the most current stable version.

Similar documents
Decisions, Decisions. Testing, testing C H A P T E R 7

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

A453 Task 1: Analysis: Problem: Solution:

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

(Python) Chapter 3: Repetition

Visualize ComplexCities

Introduction to Python (All the Basic Stuff)

Flow Control: Branches and loops

Computer and Programming: Lab 1

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

What We ll Be Seeing Today

Spring 2017 CS 1110/1111 Exam 2

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Text Input and Conditionals

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Lab 9: Pointers and arrays

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

COMP101: Final Review. With your boy(s), Mason and San

Programming to Python

Intro. Scheme Basics. scm> 5 5. scm>

Unit E Step-by-Step: Programming with Python

Iterators & Generators

DATA ABSTRACTION AND SEQUENCES 3

nostarch.com/pfk For bulk orders, please contact us at

Assignment 2 Due Thursday 7/3 at 10:00 AM

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

ASCII Art. Introduction: Python

Basics of Programming with Python

Python Problems MTH 151. Texas A&M University. November 8, 2017

Pupil Name. Year. Teacher. Target Level. Key Stage 3 Self-Assessment Year 9 Python. Spelling Test No 3. Spelling Test No 2. Spelling Test No 1

LOOPS. Repetition using the while statement


Math Day 2 Programming: How to make computers do math for you

Digital Technologies in focus:

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

SIMPLE INPUT and OUTPUT:

Quiz. Introduction: Python. In this project, you ll make a quiz game to challenge your friends. Activity Checklist.

Lab 8 CSE 3,Fall 2017

Getting Started with Amicus Document Assembly

Python for Non-programmers

CMPT 100 : INTRODUCTION TO

CS Summer 2013

OCR Pseudocode to Python

CS 61A/CS Mehrdad Niknami. University of California, Berkeley

Chapter 2 Writing Simple Programs

APCS Semester #1 Final Exam Practice Problems

If Statements, For Loops, Functions

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

CS 105 Lab As a review of what we did last week a. What are two ways in which the Python shell is useful to us?

1 Fall 2017 CS 1110/1111 Exam 3

Teacher Guide: Introduction to Computer Science

Built-in functions. You ve used several functions already. >>> len("atggtca") 7 >>> abs(-6) 6 >>> float("3.1415") >>>

In this project, you ll learn how to use a turtle to draw awesome shapes and patterns.

Activity Guide - Will it Crash?

RECURSION, RECURSION, (TREE) RECURSION! 3

CMSC201 Computer Science I for Majors

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Invent Your Own Computer Games with Python

Lab 2: Booleans, Strings, Random Numbers, Recursion, Variables, Input function

(the bubble footer is automatically inserted into this space)

STEAM Clown & Productions Copyright 2017 STEAM Clown. Page 1

Outline: Search and Recursion (Ch13)

4.2 Function definitions the basics

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

Final thoughts on functions F E B 2 5 T H

roboturtle Documentation

Computer Programming: C++

Senet. Language Reference Manual. 26 th October Lilia Nikolova Maxim Sigalov Dhruvkumar Motwani Srihari Sridhar Richard Muñoz

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

CS 115 Lecture 4. More Python; testing software. Neil Moore

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Python Unit

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

An Introduction to Python

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

Outline. Announcements. Homework 2. Boolean expressions 10/12/2007. Announcements Homework 2 questions. Boolean expression

Python Games. Session 1 By Declan Fox

COMP : Practical 8 ActionScript II: The If statement and Variables

Declaring and ini,alizing 2D arrays

Intro to Python Programming

Introduction to Scientific Python, CME 193 Jan. 9, web.stanford.edu/~ermartin/teaching/cme193-winter15

Table of Contents EVALUATION COPY

CMSC 201 Computer Science I for Majors

Welcome to CS61A! Last modified: Thu Jan 23 03:58: CS61A: Lecture #1 1

Parametric Lab. Thursday, September 17th, 8:00am - 5:10pm Phil Ringsmuth,

Beyond programming. CS 199 Computer Science for Beginners Spring 2009 Lois Delcambre Week 5/12/2009 1

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

1 Lecture 6: Conditionals and Exceptions

CS2304: Python for Java Programmers. CS2304: Advanced Function Topics

Lecture 27. Lecture 27: Regular Expressions and Python Identifiers

Magic Set Editor 2 Template Creation Tutorial

Algorithms and Programming Languages

: Intro Programming for Scientists and Engineers Final Exam

Python Programming: An Introduction to Computer Science

Transcription:

, webmaster@cse-club.com Fall 2014 In this workshop, we will be creating a basic text-based game to learn the basics Python programming language. By the end of this workshop, you will learn: The syntax of the Python programming language Looping structures Control statements Basic data structures Input and output To complete this workshop, you ll need the following installed: Your favourite text editor (gedit, Sublime Text, Notepad++, etc) Python 3.x.x (the more recent the better) Python 2.7 will also work for this workshop due to certain language rules, however I prefer working with the most current stable version. Every language has a version of a Hello World program. Start up your text editor, and write: print('hello world!') Save it as main.py and run it by running the following command in your terminal: python3 main.py You should see Hello world! printed in your terminal. Note that this will be the way we run our program throughout the workshop. Now let s try adding some personalization to this message. Clear your file, then let s ask the user for their name: name = input('what is your name? ') Here, we create a variable and put what is recieved as input into it. Notice that we don t have to define variables or a type for them. They can be changed to any type at any time, and declared and used at any time within scope. Next, let s add the name to a string and output it: 1

print('hello, {0}'.format(name)) This syntax may seem a little weird, so allow me to explain. Strings are container objects, which contain characters. Whenever you create a string, whether in a variable or in place as we have here, you can access it s operations and methods. The format method allows you to replace a bracketed expression with those in the arguments. Now, let s clear out our file again. We re going to be creating a simple text-based game, where the player explores a small dungeon and can possibly find a sword or magic, fight a dragon, and win by finding the treasure at the end. To do this, we ll first need to create our dungeon: dungeon = [ # 0 1 2 3 4 'x', 'x', 'x', 'x', 'x', # 0 'x', 'x', 'd', 't', 'x', # 1 'x', 'x', '0', 'x', 'x', # 2 'x', 'm', '0', '0', 'x', # 3 'x', 'x', 'x', '0', 'x', # 4 'x', 's', '0', '0', 'x', # 5 'x', 'x', 'x', 'x', 'x'] # 6 Here, we create a list. A list is a container that can contain elements of various types and can be iterated over. It s similar to an array or vector in C++. Each character represents a different tile in our game - for spaces that cannot be traversed for spaces that can be traversed for the location of the dragon for the location of the treasure for the location of the sword for the location of the magic As it s a single-dimensional list, we need a way to address individual spaces. Let s write a function to get the index of the desired space, based on the coordinates: def getindex(x, y): return y * 5 + x In Python, we use the def keyword to define that we re creating a function. Arguments don t need a given type, for the same reason that variables don t. Notice the colon. Everything in the next indentation block belongs to everything under that colon. This means that. The formula gets the right value by multiplying the y value by the size of the row, then adding the x value. I ve commented the coordinate system onto the list. Let s also create a function to get the value of the tile in the dungeon: def gettile(x, y): return dungeon[getindex(x, y)] Accessing values in a list is just like arrays in other languages - just pass the index into square brackets. Let s run and test this by adding a couple of prints to the end of our file and running it: 2

print(getindex(3, 5)) print(gettile(3, 5)) Try a few different coordinates to verify correctness, then remove these lines. Next, we need to define what makes our player: their hitpoints, whether or not they have the sword and the magic, their combat status, and their coordinates in the dungeon: player = { 'x' : 3, 'y' : 5, 'hp' : 10, 'sword' : False, 'magic' : False, 'combat' : False } This is called a dictionary. A dictionary is similar to a map or an associative array from other languages. However, each item in the dictionary can be defined with any other data type. We need to know which directions the player can do in, so let s write another function: def movabledirections(player): directions = [] if gettile(player['x'], player['y'] - 1)!= 'x': directions.append('n') if gettile(player['x'], player['y'] + 1)!= 'x': directions.append('s') if gettile(player['x'] + 1, player['y'])!= 'x': directions.append('e') if gettile(player['x'] - 1, player['y'])!= 'x': directions.append('w') return directions Here, we check in each cardinal direction to see which are available, and add them to the list if they are. At the end, we return the list. Now, let s create the main game loop: command = None gameover = False while not gameover: This loop will run only while the game is not over. Later on, we ll define what causes the game to end. For now, let s focus on movement: 3

for direction in movabledirections(player): print("{0} - move {0}".format(direction)) command = input('what would you like to do? ').upper() movecharacter(command) Here, we go through each direction the player can move through and output them. Then, we ask the player which direction to move in. Finally, we call the movecharacter function, which we will implement outside the loop: def movecharacter(direction): if direction in movabledirections(player): if direction == 'N': player['y'] -= 1 if direction == 'S': player['y'] += 1 if direction == 'E': player['x'] += 1 if direction == 'W': player['x'] -= 1 Here, we check to make sure the direction is able to be moved to by the player. The in operator checks for inclusion in a collection. Then, we check what the direction was, and modify the coordinates accordingly. Run your game, exploring your map. If you want, output the tile at the player location to see what s going on. Now that we are able to move our player around, lets allow them to pick up the sword when they encounter it. We re going to change our game loop a bit to accomodate different kinds of tiles: while not gameover: tile = gettile(player['x'], player['y']) index = getindex(player['x'], player['y']) if tile == 's': player['sword'] = True dungeon[index] = 'x' print('you have found the sword!') else: for direction in movabledirections(player): print("{0} - move {0}".format(direction)) command = input('what would you like to do? ').upper() movecharacter(command) 4

Now, instead of only asking for movement, we check first if the tile is the sword tile. If so, we set the player s sword value to true, and set the tile to be non-traversable. We also let the player know. Otherwise, we move as normal. We can do the same for the magic, putting this between the if and else statements: elif tile == 'm': player['magic'] = True dungeon[index] = 'x' print('you have found the hidden defensive magic!') elif is the same as else-if in other languages, just shorter. While we re adding these discoveries, lets make sure to add the most important one - treasure! This is one of two ways that the game ends - the other being death! elif tile == 't': gameover = True print('congratulations, you have found the treasure!') If we find the treasure, we tell the player they win, and set the gameover variable to True. This will cause the loop to end at the end of the current iteration, ending the program. We have one last component to take care of - fighting the dragon! First, let s add the dragon s HP to our declarations: dragonhp = 12 Let s also add another elif block to our main loop. elif tile == 'd': The rest of this section will happen in this block. First, let s check to make sure that we haven t started the fight yet. If we haven t let the user know that the dragon appears: if not player['combat']: player['combat'] = True print('the dragon appears before you!') While we re at it, let s be a little condescending to those who don t find the sword. They ll be in a lot of trouble! if not player['sword']: print('a weapon would\'ve been useful...') Every time we input a command, we should let the player know how the fight is going, so let s output each side s health: print('dragon HP: {0}'.format(dragonHp)) print('player HP: {0}'.format(player['hp'])) 5

Let s also show available options for the user in combat, and ask them for their choice: if player['sword']: print('s - stab with your sword') print('p - punch with your fist') command = input('what will you do? ').upper() We can t just have static damage, that d be boring! Let s add a bit of randomness. At the top, we ll import the random module: from random import randint When importing from another module, you can specify specific variables, classes, and functions to get. In this case, we re importing the function randint. Now, lets check our input that we just recieved: playerdamage = 0 if command == 'S': if player['sword']: playerdamage = randint(2, 5) dragonhp -= playerdamage print('you stab the Trogdor for {0} damage!'.format(playerdamage)) else: player['hp'] -= 2 print('while you try to stab with your pretend sword, you hurt yourself for 2 damage') elif command == 'P': playerdamage = randint(1, 2) dragonhp -= playerdamage print('you punch the dragon for {0} damage!'.format(playerdamage)) Here, we check our input, and if a sword is chosen, we ll make sure the player actually has the sword. Otherwise, let s penalize them for trying to pull a fast one. The randint function we imported earlier allows us to generate a number within a range of values. Now, before the dragon can attack, lets check to make sure the dead isn t attacking the living: if dragonhp <= 0: print('you have slain the burninator!') dungeon[index] = '0' If the dragon is dead, we ll make the tile a walkable tile. We re going to use the magic as a defense against the dragon, which has two attacks - a fire attack and a punching attack: 6

else: if randint(1, 4) == 1: if player['magic']: print('the dragon breathes fire, but the magic you found earlier protects you!') else: dragondamage = randint(4, 6) player['hp'] -= dragondamage print('the dragon breathes fire and burns you for {0} damage!'.format(dragondamage)) else : dragondamage = randint(2, 3) player['hp'] -= dragondamage print('the dragon punches you for {0} damage!'.format(dragondamage)) Finally, we ll check to make sure the dragon hasn;t slain the player. If so, let them know and break out of the loop: if player['hp'] <= 0: print('oh dear, you are dead!') gameover = True Save and test your program. If everything works correctly, then congratulations! You ve created your game, and have a working knowledge of the Python programming language. Here are a few things to try to help further your learning of the language, and improve your game along the way. Using classes, create a Player object. Import it, and have any operations. Have other monsters in the dungeon to fight. Randomly assign them hitpoints and power levels. Create a more sophisticated combat system. Randomly generate new dungeons for each run of the game. 7