Python Games. Session 1 By Declan Fox

Similar documents
Python Unit

Python. Session 6. By Declan Fox With thanks to Al Sweigart

This summary is located here:

Text Input and Conditionals

Invent Your Own Computer Games with Python 3 rd Edition

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

Fundamentals of Programming (Teacher Pack)

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

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

Invent Your Own Computer Games with Python

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

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

T H E I N T E R A C T I V E S H E L L

cs1114 REVIEW of details test closed laptop period

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

Getting started: 1. Join the wifi network Network: Password:

CSI Lab 02. Tuesday, January 21st

CSCE 110 Programming I Basics of Python: Variables, Expressions, Input/Output

CS1 Lecture 3 Jan. 22, 2018

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

Programming for Engineers in Python. Recitation 1

Fundamentals: Expressions and Assignment

MITOCW watch?v=0jljzrnhwoi

Starting. Read: Chapter 1, Appendix B from textbook.

CSE 115. Introduction to Computer Science I

CSCE 110 Programming I

06/11/2014. Subjects. CS Applied Robotics Lab Gerardo Carmona :: makeroboticsprojects.com June / ) Beginning with Python

Programming for Engineers in Python. Autumn

Introduction to: Computers & Programming: Review prior to 1 st Midterm

CS1 Lecture 3 Jan. 18, 2019

Programming in Python 3

LOOPS. Repetition using the while statement

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION

Invent Your Own Computer Games with Python

Downloaded from Chapter 2. Functions

Introduction to Python (All the Basic Stuff)

What did we talk about last time? Examples switch statements

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Introduction to Python Code Quality

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Lecture 1. Types, Expressions, & Variables

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Scripting Languages. Python basics

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Introducing: main Function, Comments, Statements

Expressions and Variables

MICROPROCESSOR SYSTEMS INTRODUCTION TO PYTHON

Lecture 2. Variables & Assignment

>>> * *(25**0.16) *10*(25**0.16)

Algorithms and Programming I. Lecture#12 Spring 2015

Programming with Python

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

printf( Please enter another number: ); scanf( %d, &num2);

Chapter 4: Control Structures I (Selection)

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

UNIVERSITY OF TECHNOLOGY SYDNEY FACULTY OF ENGINEERING AND IT. Let's code with! DOCUMENTATION, MATERIAL, RESOURCES. (version 2)

Introduction to Python and programming. Ruth Anderson UW CSE 160 Winter 2017

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

Lecture 3. Input, Output and Data Types

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

CSE 142 Su 04 Computer Programming 1 - Java. Objects

SPARK-PL: Introduction

Computer and Programming: Lab 1

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

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Unit 3. Operators. School of Science and Technology INTRODUCTION

The Big Python Guide

Part III Appendices 165

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

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

Introduction to Game Programming Lesson 4 Lecture Notes

Programming Language. Functions. Eng. Anis Nazer First Semester

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

(Python) Chapter 3: Repetition

CS 106 Introduction to Computer Science I

Discussion 1H Notes (Week 2, 4/8) TA: Brian Choi Section Webpage:

Introduction to programming with Python

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CMSC 201 Computer Science I for Majors

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

Simple Java Programming Constructs 4

Variables, expressions and statements

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Ch.2: Loops and lists

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS Summer 2013

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

Introduction to TURING

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

ENGR 101 Engineering Design Workshop

Transcription:

Python Games Session 1 By Declan Fox

Rules

General Information Wi-Fi Name: CoderDojo Password: coderdojowireless Website: http://cdathenry.wordpress.com/

Plans for this year Command line interface at first Number guessing game GUI games with Pygame Bunnies and badgers Possibly Hardware Lego Mindstorms

Sources We will be using some code from the book Invent your own computer games with Python by Al Sweigart.

Al Sweigart Albert Sweigart (but you can call him Al), is a software developer in San Francisco, California he is originally from Houston, Texas. The book is free online at http://inventwithpython.com

Installation As we will be moving on to graphical games we will need to install both Python and Pygame* * If you have Python 3.x.x and Pygame installed you can ignore the next two slides

Installation We will be using version 3.2 of Python as this is latest release that's compatible with Pygame Go to https://www.python.org/download/releases/3.2.5/ Scroll down to the list of downloads Click on Windows x86 MSI Installer (3.2.5) When this has downloaded click on it to install.

Installation To install Pygame Go to http://pygame.org/download.shtml Scroll down to the list of downloads Click on pygame-1.9.2a0.win32-py3.2.msi When this has downloaded click on it to install.

Programming Languages Input, Output & Store Data E.g. text, numbers Operate on Data E.g. add numbers, change text Loops Repeat commands several times Decisions Do something IF something else is true

IDLE Stands for Interactive DeveLopment Environment. The Interactive Shell The window that appears when you first run IDLE is called the interactive shell. A shell is a program that lets you type instructions into the computer.

Exercise Try typing some of these math problems into the shell, pressing Enter key after each one. 2+2+2+2+2 8*6 10-5+6 2 + 2 10 / 2

Math Operators 2 + 2 addition 2-2 subtraction 2 * 2 Multiplication 2 / 2 division

Storing Values in Variables spam = 15 types of numbers in Python int which is an integer example 3, 4, 7 float which is a floating point number 3.4, 7.2, 5.0

What is a Variable? In computer programming, a variable is a storage location and an associated symbolic name (an identifier) which contains some known or unknown quantity or information, a value.

Strings Another important data type is a string which are pieces of text. spam = hello String concatenation Complicated way of saying you can join string together using the + operator. coder + dojo will give you coderdojo

Our first program

The File Editor Let's write our first program! To write a programme we need to use the IDLE file editor. Click on the File menu at the top of the Python Shell window, and select New Window. A new blank window will appear for us to type our program in. This window is the file editor.

Comments #This programme says "Hello, World! This line is a comment Comments are used to explain the code to yourself or to anybody else who looks at it

Print Function print ('Hello, World! ) This line calls the print function Whatever is to be printed goes inside the parentheses We use parentheses to make it clear we are talking about a function called print not a variable called print

Input

Input myname = input() This line has an assignment statement with a variable myname and a function call input() When input() is called, the program waits for the user to enter text. The text string that the user enters (your name) becomes the function's output value

Guess the number game

Scratch Version

Import Statement import random This line is an import statement Python includes many built-in functions. Modules are Python programs that contain additional functions. In this case, we're importing the random module.

Random Function number = random.randint(1, 20) In this line we call a new function named randint(), and then store the return value in a variable named number. Because the randint() function is provided by the random module, we precede it with random. (don't forget the dot) to tell our program that the function randint() is in the random module.

Passing Arguments to Functions The numbers in the parentheses in the random.randint(1, 20) function call are called arguments. Arguments are the values that are passed to a function when the function is called. Arguments tell the function how to behave. Some functions require that you pass them values when you call them.

Loops Line 12 has something called a while statement, which indicates the beginning of a while loop. Loops are parts of code that are executed over and over again.

Loop in Scratch

Code Blocks A block is one or more lines of code grouped together with the same amount of indentation. You can tell where a block begins and ends by looking at the line's indentation (that is, the number of spaces in front of the line). A block begins when a line is indented by four spaces. Any following line that is also indented by four spaces is part of the block.

Blocks within Blocks A block within a block begins when a line is indented with another four spaces (for a total of eight spaces in front of the line). The block ends when there is a line of code with the same indentation before the block started

while guessestaken < 6: print('take a guess.') guess = input() guess = int(guess) guessestaken = guessestaken + 1 if guess < number: print('your guess is too low.') if guess > number: print('your guess is too high.') if guess == number: break

The Boolean Data Type The Boolean data type has only two values True or False. These values are case-sensitive They are not string values; do not put a ' quote character around them. We use Boolean values (also called bools) with comparison operators to form conditions.

Comparison Operators while guessestaken < 6: The comparison operator is used to compare two values and evaluate to a True or False Boolean value.

Comparison Operators Comparison operators Operator Sign Operator Name < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Is equal to!= Not equal to

Conditions A condition is an expression that combines two values with a comparison operator (such as < or >) and evaluates to a Boolean value. A condition is just another name for an expression that evaluates to True or False.

Conditions Conditions always evaluate to a Boolean value True or False. In the case of our Guess the Number program, in line 4 we stored the value 0 in guessestaken. Because 0 is less than 6, this condition evaluates to the Boolean value of True. Remember, a condition is just a name for an expression that uses comparison operators such as < or!=.

The int() Function guess = int(guess) In line 15, we call a new function called int(). The input() function returned a string of text that player typed. But in our program, we will want an integer, not a string. When the player enters their guess, the input() function will return the string value '5' and not the integer value 5.

The int() Function Remember that Python considers the string '5' and the integer 5 to be different values. So the int() function will convert the string value we give it and return the integer value of it.

Incrementing a Variable guessestaken = guessestaken + 1

The if statement Lines 19 and 20 check if the number that the player guessed is less than the secret random number that the computer came up with. If so, then we want to tell the player that their guess was too low by printing this message to the screen. if guess < number: print('your guess is too low.')

The break Statement if guess == number: break The line inside the if-block is a break statement that tells the program to immediately jump out of the while-block to the first line after the end of the while-block.

Next Week Ideas for improving our guessing game and we re going to look at an adventure game called Dragon s Lair