Introduction to Game Programming Lesson 4 Lecture Notes

Size: px
Start display at page:

Download "Introduction to Game Programming Lesson 4 Lecture Notes"

Transcription

1 Introduction to Game Programming Lesson 4 Lecture Notes Learning Objectives: Following this lecture, the student should be able to: Define frame rate List the factors that affect the amount of time a game cycle takes Define pixel, screen resolution List the work that is accomplished inside a gaming loop List the game resources and give one reason they are set up before the gaming loop Relate the frame rate to the gaming loop List what happens at the following stages of the game loop: getting input from the user, updating game entities, refreshing the screen Define the import and initialize step of the framework and give examples of code that would go here Define the display configuration step of the framework and give examples of code that would go here Define the entities step of the framework and give examples of code that would go here Define the assign values step of the framework and give examples of code that would go here. Define the main loop step of the framework and give examples of code that would go here Define the timing step of the framework and give examples of code that would go here Define the handle events step of the framework and give examples of code that would go here Define the refresh step of the framework and give examples of code that would go here Define the terms blit, double-buffering, and flip Define RGB color and give the range of values for R,G, and B; be able to give a rough idea of a color given RGB values Motivation (This needs no motivation.) We're going to start using Pygame now, which is the game programming library that gives us graphics, audio, collision handling, and ways to process input, among other functions. Frame rate Slide #5 Frame rate is the speed at which the visual display updates. This is measured in frames per second. It is important to maintain a constant frame rate in a game. A game executes in a loop that runs over and over. Each loop, the game processes input from the user, makes the necessary adjustments to the data of the game and the display, and redraws the display. The frame rate must allow enough time for the slowest processing to occur, and it must slow the game down if there is less processing so it runs at a constant speed. Bring up tetrash.py Copyright 2010 Margaret Burke. All rights reserved. 1

2 This is an example that is more complicated than what we're going to be doing this chapter. Even the stuff in this chapter might make your head swim, learning new terminology and wondering what is pygame and what is python and what everything is doing. But I want to show you this example so you have an idea of what the gaming loop is all about so we can talk about timing. Go over and explain the code in terms of an image drawn at a location, each cycle the image is updated (just dropped) and also based on user input, then redrawn. So the speed at which the game repeats is the frame rate. That is how frequently the game is updated. TV has a frame rate of 30 frames/sec, and is a good range for a game. Less than 10 frames/second and your game will look like choppy. Change to block_y+=10, show at 30 frames/sec, then change to 5 frames/sec, see the choppiness. (This starts at block_y+=2, 30 frames/sec.) So I'm going to have a frame rate of 30 frames/sec for all of our games. You don't want it to be so fast that when the drawing gets complicated, you can't get all the drawing done in time. So basically, what's happening is the processor is doing these calculations, including getting what's ready to draw to the screen drawn on a surface. More calculations take more time. Less calculations take less time. But you don't want it to run quickly sometimes and more slowly other times. So by setting the frame rate, you basically are having it do its computations, then letting it wait around until the frame rate time runs out, if there's any extra, so that the frame rate is consistent. The screen Let's talk a little about the screen. Copyright 2010 Margaret Burke. All rights reserved. 2

3 Logically and physically, our monitors are composed of picture elements, or pixels, that are arranged in a grid. The higher the resolution, the more pixels in the grid. We'll talk about this in more detail in Chapter 5. For now what I want to mention is that we're going to create our game in a window, with our own interface, or way of interacting with the user. The smaller the window, the less time it will take to draw, and the faster our game will be. Let's talk about how those pixels are arranged on the screen, though. Applets_files/image001.gif Okay, so this is different from the Cartesian coordinate system you learned in geometry in that the y coordinate increases as you move down the screen. That's just something to keep in mind. If we want something to move down the screen, we increase its y coordinate, we don't decrease it. If we want something to move right, we increase its x coordinate. If we want something to move left, we decrease its x coordinate. Game structure On to the details of the structure of the game, which I talked about briefly when I showed you tetrash.py and discussed timing. Slide 8, 9 Computer programs manipulate data. We have talked about the kind of data that's stored for a game. We can call this the game state. Before the gaming loop, all of these objects need to be created. For example, images and sounds need to be loaded in. These are called game assets. It takes time to load the assets in, so this is done once, Copyright 2010 Margaret Burke. All rights reserved. 3

4 before the loop, and then the assets can be accessed quickly in the loop. This is what's happening while a game is "loading." Because games need to be fast, you want to do as much as possible before the game begins. Slide 10, 11, 12 In the gaming loop, the program gets input from the user, alters the game state based on the user's actions, and redraws the screen. This all happens at the frame rate that you set. So one loop of the game happens each frame. Slide 13, 14 Things that can happen during the loop include the user changing the speed or direction of motion, collisions, things exploding or changing size or moving out of bounds. So you update your data. In other words, you change your variables. Let's go back to tetrash.py and look at the code for a minute. I have the block, which has the variable name "block," and this is a type we haven't seen yet, which is a pygame type, which is a Surface. A Surface in pygame is the type we use for images. But then there are two variables of an Integer (int) type, block_x and block_y, and these are the upper left coordinates of the block. Now down here is the game loop (while keepgoing:), and in the game loop I look first for user input, and what I'm doing here is checking for key presses. Cold call What happens if the user presses the Subtract 10 from block_x left arrow key (Indicate code)? Cold call What happens if the user presses the right arrow key (Indicate code)? Add 10 to block_x Now in the next line I'm doing something a little fancier, once again I'm using pygame and I'm using a pygame method to rotate the block if the user presses the up key. But after we're done checking events, which is how we get user input, then we update the rest of the game state. Cold call What's happening here? Adding 2 to an integer, (block_y=block_y+2) adding 2 to the y Hands And can anyone figure out the next line? coordinate Checking for bottom screen boundary Right. So if the user presses an arrow key, I'm updating an integer variable. Just adding or subtracting 10 from it. After that I'm adding 2 to the y variable. So how does it happen that the block appears to move on the screen, both down automatically, and to the right or left if the user presses a key? Copyright 2010 Margaret Burke. All rights reserved. 4

5 Well, it's the next part of the code. I have a teal image, just a plain teal background, that happens to be the size of the window. And first I copy that to the screen. Background and screen are just two images. So I'm copying the background image to the screen image at 0,0, which completely covers the screen, because they're the same size. Then I copy the block onto the background. And I copy it to the coordinates that we just changed. Cold call So if the old y coordinate was 20, 22 what's the new y coordinate? Cold call And if the old x coordinate was 100 and the use pressed the left arrow, what's the new x coordinate? 90 Right. So the last time the block was copied to the screen, it was copied at 100,20. This time it's copied at 90,22. This happens quickly, 30 frames per second, so it appears the block has moved. We're going to go into this loop in great detail and I'm going to explain everything that's in there. I want you to understand the general idea of how it works, first. Slide 15, 16, 18 Okay, so I wouldn't want to do this without pygame. Games like that little tretrash thing I wrote would've taken days to write without an API, or application programming interface that is designed specially for writing games. We're going to use the Pygame library, which is built on top of SDL (Simple DirectMedia Layer) and allows us to work with graphics, sound, and sprites. This is 2D. Hopefully you have already installed Pygame at home. It's very simple. I believe I have a video on the website in which I show you the install I did last Thanksgiving. You can attempt another configuration if you like. That configuration took me days to put together in such a way that I could build a game for distribution. Getting Python and Pygame to work together is easy; getting a configuration and build file that will allow you to build for distribution is not so easy. Slide 19, 20, 26 / idea.py For the rest of this lesson we're going to go over the gaming loop in detail. Then we're going to work on a moving box program that is the basis for your project. There isn't much you can do for a lab until we start working on the moving box code. So the author has come up with this mnemonic for remembering the parts of the gaming loop, and it's IDEA/ALTER. I used to make my class memorize this and write it down for the Lesson 4 quiz. You'll note we don' t have a Lesson 4 quiz. You should still memorize this. Read slide 20 and then turn to the code and go over the parts. Copyright 2010 Margaret Burke. All rights reserved. 5

6 import: Remember before we had to import the random library. We do any imports at the top of the file. So we import pygame here, and if we were using a random function we'd import random here too, and if we're using any other libraries, such as the math library, we'd import those here as well. Okay, and then luckily "initialize" also begins with an i. Think of it as i 2. You need to initialize the pygame module. Display. This is all new. We're not running in the console window any more; so we have to set up our own graphical interface. If you're going to have any text telling the user how many lives she has left, if you're going to have menus, buttons, etc., you're going to have to set them up yourself. For now we're just going to look at the simplest possible program and then we'll slowly build on it, and that's going to be new enough. So the first thing to know is that a Surface is a new data type. Cold call What are the data types we've seen int, float, str, list before? Good. Okay and remember that string and list types had libraries of methods that we could call. Well Surface is the first pygame type we're going to look at, and it's the Pygame graphics type. So if we have an image that we want to load in, or if we want to just draw using Pygame's drawing methods (which we'll learn in the next lesson), then we create a Surface variable. Now there are different ways to make a Surface. The first we see is this one: screen = pygame.display.set_mode((640, 480)) Anything you see that is part of pygame, you can look up in the pygame documentation. First, what is display? This is the object that we're sending the set_mode message to. This module offers control over the pygame display. Pygame has a single display Surface that is either contained in a window or runs full screen. Once you create the display you treat it as a regular Surface. Changes are not immediately visible onscreen, you must choose one of the two flipping functions to update the actual display. Okay, so the display is basically the window that your game runs in. You can draw to it like a regular Surface, and eventually you will use a flipping function so that it's shown to the user. So you're drawing on it backstage and then when you're ready, you show it to the user. Now let's look at set_mode: Copyright 2010 Margaret Burke. All rights reserved. 6

7 pygame.display.set_mode(resolution=(0,0), flags=0, depth=0): return Surface This function will create a display Surface. The arguments passed in are requests for a display type. The actual created display will be the best possible match supported by the system. The Surface that gets returned can be drawn to like a regular Surface but changes will eventually be seen on the monitor. If no resolution is passed or is set to (0, 0) and pygame uses SDL version or above, the created Surface will have the same size as the current screen resolution. If only the width or height are set to 0, the Surface will have the same width or height as the screen resolution. Okay. So this returns a Surface. It is that important Surface that is going to be the display the user sees. Now this is on the right hand side of an assignment statement. Cold call So the Surface that gets returned is screen going to be stored in what variable? Right. So when this is done, screen is a Surface variable, something that can be drawn to like a regular Surface, but that will eventually be seen on the monitor. Okay, and what's next is set_caption, which sets the text in the title bar. So that sets up the Display, which is the "D" in IDEA. On to E for Entities. This is where you create all of the assets that are in your game. I guess if he had called it "assets" then it would've been IDAA. But we should really use the terminology that's used in the industry. So if you have sounds and images and other data structures, you create them here. Looking at the very simple idea.py, all he creates here is the background, which is the Surface that he copies to the screen eventually. I remember finding this confusing when I first read this chapter, I don't know if you're finding it confusing. But the background is just one picture. It gets copied to the screen and then the other things will get copied on top of it. So it's just a picture. In this case, it's a plain obnoxiously teal rectangle. So in this case, the Surface is created in a different way. Any time you create an image, you're going to create a Surface. That's an image variable. So he creates a Surface by using pygame.surface and passing in a size. He wants the Surface to cover the whole screen, so he passes in screen.get_size(). This might be confusing. get_size is a method of a Surface. So similar to calling append for a list, we can call get_size for a Surface and we get back the size of the image. That returns (width, height). In fact, I'm going to add a line to print that to the console so we can see what gets returned from that: Copyright 2010 Margaret Burke. All rights reserved. 7

8 print screen.get_size() (Then execute.) Okay? So let's look at Surface creation methods. These are called Constructors, because they create a variable of this type: pygame object for representing images pygame.surface((width, height), flags=0, depth=0, masks=none): return Surface pygame.surface((width, height), flags=0, Surface): return Surface So it looks like you always must pass in what's called a tuple, which is variables enclosed in parentheses, with width first and then height. A tuple is an immutable list. So it's a read-only list. There are no tuple methods, and you use parentheses and not brackets, but otherwise it's the same. You can index it and slice it. Now he's using a graphics method for Surfaces, called fill, to fill the background Surface with a color. Okay, that gets us past the setting-up part of IDEA. The next part is "A" for "Action," and that's when we have the ALTER part of the mnemonic. This is the game loop now. Slide 26 / idea.py Okay, let's start with variables we need to manage the loop. First the clock, which will be used to set the frame rate. Then a Boolean variable, which will be used to control loop execution. When keepgoing is False, we'll stop the game. So then the game loop, which is a while loop. So while keepgoing is True, this loop will execute. Let's see what's in there: Okay, tick the clock. This is Timing. There are other ways to keep the frame rate constant in pygame, but this is the one we use in this class. You are welcome to read about and try other methods. This one has worked fine for our purposes, but I understand it's not always accurate on all platforms, according to the pygame documentation. Okay, the next part is really important, not just for this class, but if you are going to learn any kind of graphical user interface programming. All graphical user interfaces execute in an event loop. That's really what this gaming loop is. An event is something created by the system when the user accesses input hardware, and passed to your program. The system digests it a little bit before it gets passed to you, so you only have to deal with hardware events that have something to do with your program and that have been put into an Event object that you can look at and work with. Events are very cool. Copyright 2010 Margaret Burke. All rights reserved. 8

9 So we'll talk about events in more detail in the next lesson. For now, you get an event from pygame and you look at its type. The system puts events in a queue for us and we're just pulling the events out of the queue, looking at the type, and handling the ones we're interested in. For example, in the tetrash.py game, note that 'm only interested in keydown and quit events, and in the case of keydown events, I only deal with three keys. (Pull up tetrash.py.) We're only interested in quit events right now, so we set keepgoing to False if the user has chosen to quit the game. Setting KeepGoing to false will get us out of the loop, so we know this loop can end. List of event types here: Last part! "R" for Refresh display. Okay, so this is where we take everything we've done in the background and show it to the user. Here we've done nothing at all, so we just copy the background to the screen and then call display.flip, which takes the screen and shows it to the user. One thing I want to mention is pygame.quit(). If you don't put this in, you won't be able to run your games from IDLE. Two students discovered this last semester. Until then, I thought you couldn't run your program in IDLE, because that's what the text says. Slide 33, 34 Explain double-buffering and bit block transfer. Look at tetrash.py again just to show a little bit more being done in the Refresh part. Slide 35, 36 Talk about color. Go to Okay, so you can see in this idea.py program we set a background color. So one thing you can do is download this program from the class website and play with the color there. It's something. Moving Box So we're going to plow through, and then the rest of the lesson you can work on the lab, which is designed to help you with your project. Let's add a moving box to this idea.py program. Cold call So any graphics that we're going to Surface add are going to be of what type? Cold call Right, and we have a line of code where we create a rectangular Surface variable in this code. What Background=pygame.Surface Etc. Copyright 2010 Margaret Burke. All rights reserved. 9

10 Cold call Hands line is that? In what part of IDEA ALTER would we create the box we're going to move around? Does anybody want to attempt a line of code to create a box? Entities See below, for example #E - Entities (just background for now) background = pygame.surface(screen.get_size()) background = background.convert() background.fill((0, 125, 130)) box=pygame.surface((25,25)) box.fill((255,0,0)) Great, now we have a box. So suppose we want the box to start about mid-way down the screen on the left, and then we want the box to move to the right until it reaches the right edge, and then wrap around so it starts at the left again. (Run Andy Harris' movebox.py) Okay, so let me tell you what you need to know -- you need to know how to get the edges of the screen. We have screen.get_width() and screen.get_height(). Cold call And screen.get_width() will give us Right edge what edge? Cold call And screen.get_height() will give us Bottom edge what edge? Cold call And what is the left edge? 0 Hands And the top? 0 Okay, good! That's everything you need to know to program this. Let's do it. Hands Write code to move the box. They work on labs. Copyright 2010 Margaret Burke. All rights reserved. 10

11 """ movebox.py illustrates basic motion in the IDEA/ALTER framework moves a rect across the screen """ #Initialize import pygame pygame.init() #Display screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("move a box") #Entities #yellow background background = pygame.surface(screen.get_size()) background = background.convert() background.fill((255, 255, 0)) #make a red 25 x 25 box box = pygame.surface((25, 25)) box = box.convert() box.fill((255, 0, 0)) # set up some box variables box_x = 0 box_y = 200 #ACTION #Assign clock = pygame.time.clock() keepgoing = True #Loop while keepgoing: #Time clock.tick(30) #Events for event in pygame.event.get(): if event.type == pygame.quit: keepgoing = False #modify box value box_x += 5 #check boundaries if box_x > screen.get_width(): box_x = 0 #Refresh screen screen.blit(background, (0, 0)) screen.blit(box, (box_x, box_y)) pygame.display.flip() pygame.quit() Copyright 2010 Margaret Burke. All rights reserved. 11

""" idea.py simplest possible pygame display demonstrates IDEA / ALTER model Andy Harris, 5/06 """

 idea.py simplest possible pygame display demonstrates IDEA / ALTER model Andy Harris, 5/06 """ idea.py simplest possible pygame display demonstrates IDEA / ALTER model Andy Harris, 5/06 """ #I - Import and initialize import pygame pygame.init() #D - Display configuration screen = pygame.display.set_mode((640,

More information

The Stack, Free Store, and Global Namespace

The Stack, Free Store, and Global Namespace Pointers This tutorial is my attempt at clarifying pointers for anyone still confused about them. Pointers are notoriously hard to grasp, so I thought I'd take a shot at explaining them. The more information

More information

Introduction to Game Programming Lesson 5 Lecture Notes, Part 1: The draw Module

Introduction to Game Programming Lesson 5 Lecture Notes, Part 1: The draw Module Introduction to Game Programming Lesson 5 Lecture Notes, Part 1: The draw Module Learning Objectives: Following this lecture, the student should be able to: Draw a line with pygame, using all parameters

More information

PyGame Unit ?

PyGame Unit ? PyGame Unit 1 1.1 1.? 1.1 Introduction to PyGame Text Book for Python Module Making Games With Python and PyGame By Al Swiegert Easily found on the Internet: http://inventwithpython.com/pygame/chapters

More information

PYTHON NOTES (drawing.py and drawstuff.py)

PYTHON NOTES (drawing.py and drawstuff.py) PYTHON NOTES (drawing.py and drawstuff.py) INTRODUCTION TO PROGRAMMING USING PYGAME STEP 1: Importing Modules and Initialization All the Pygame functions that are required to implement features like graphics

More information

MITOCW watch?v=0jljzrnhwoi

MITOCW watch?v=0jljzrnhwoi MITOCW watch?v=0jljzrnhwoi The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

MITOCW watch?v=rvrkt-jxvko

MITOCW watch?v=rvrkt-jxvko MITOCW watch?v=rvrkt-jxvko The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. Guide to and Hi everybody! In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology. This guide focuses on two of those symbols: and. These symbols represent concepts

More information

Linked Lists. What is a Linked List?

Linked Lists. What is a Linked List? Linked Lists Along with arrays, linked lists form the basis for pretty much every other data stucture out there. This makes learning and understand linked lists very important. They are also usually the

More information

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between MITOCW Lecture 10A [MUSIC PLAYING] PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between all these high-level languages like Lisp and the query

More information

Multimedia-Programmierung Übung 6

Multimedia-Programmierung Übung 6 Multimedia-Programmierung Übung 6 Ludwig-Maximilians-Universität München Sommersemester 2018 Ludwig-Maximilians-Universität München Multimedia-Programmierung 6-1 Today Sprites, Sprite Groups and Sprite

More information

SPRITES Moving Two At the Same Using Game State

SPRITES Moving Two At the Same Using Game State If you recall our collision detection lesson, you ll likely remember that you couldn t move both sprites at the same time unless you hit a movement key for each at exactly the same time. Why was that?

More information

MITOCW watch?v=se4p7ivcune

MITOCW watch?v=se4p7ivcune MITOCW watch?v=se4p7ivcune The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

MITOCW watch?v=kz7jjltq9r4

MITOCW watch?v=kz7jjltq9r4 MITOCW watch?v=kz7jjltq9r4 PROFESSOR: We're going to look at the most fundamental of all mathematical data types, namely sets, and let's begin with the definitions. So informally, a set is a collection

More information

MITOCW watch?v=flgjisf3l78

MITOCW watch?v=flgjisf3l78 MITOCW watch?v=flgjisf3l78 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To

More information

BEGINNER PHP Table of Contents

BEGINNER PHP Table of Contents Table of Contents 4 5 6 7 8 9 0 Introduction Getting Setup Your first PHP webpage Working with text Talking to the user Comparison & If statements If & Else Cleaning up the game Remembering values Finishing

More information

pygame Lecture #5 (Examples: fruitgame)

pygame Lecture #5 (Examples: fruitgame) pygame Lecture #5 (Examples: fruitgame) MOUSE INPUT IN PYGAME I. Detecting Mouse Input in pygame In addition to waiting for a keyboard event to precipitate some action, pygame allows us to wait for a mouse

More information

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

Smoother Graphics Taking Control of Painting the Screen

Smoother Graphics Taking Control of Painting the Screen It is very likely that by now you ve tried something that made your game run rather slow. Perhaps you tried to use an image with a transparent background, or had a gazillion objects moving on the window

More information

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5 Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5 [talking head] Formal Methods of Software Engineering means the use of mathematics as an aid to writing programs. Before we can

More information

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

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Multimedia-Programmierung Übung 5

Multimedia-Programmierung Übung 5 Multimedia-Programmierung Übung 5 Ludwig-Maximilians-Universität München Sommersemester 2009 Ludwig-Maximilians-Universität München Multimedia-Programmierung 5-1 Today Sprite animations in Advanced collision

More information

Slide 1 CS 170 Java Programming 1

Slide 1 CS 170 Java Programming 1 CS 170 Java Programming 1 Objects and Methods Performing Actions and Using Object Methods Slide 1 CS 170 Java Programming 1 Objects and Methods Duration: 00:01:14 Hi Folks. This is the CS 170, Java Programming

More information

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto CS 170 Java Programming 1 Working with Rows and Columns Slide 1 CS 170 Java Programming 1 Duration: 00:00:39 Create a multidimensional array with multiple brackets int[ ] d1 = new int[5]; int[ ][ ] d2;

More information

CHAPTER 1. Command Arguments Description Example. Prints something to the console. A value can be text in quotes or a variable name.

CHAPTER 1. Command Arguments Description Example. Prints something to the console. A value can be text in quotes or a variable name. GAME PROGRAMMING L LINE These are the tokens from the end of each chapter of Game Programming, the L Line I ve recombined these charts into one handy document you can and use as a reference. Thanks to

More information

slide 1 gaius Python Classes you can use theclass keyword to create your own classes here is a tiny example of a class

slide 1 gaius Python Classes you can use theclass keyword to create your own classes here is a tiny example of a class Python Classes slide 1 you can use theclass keyword to create your own classes here is a tiny example of a class Python Classes slide 2 tinyclass.py #!/usr/bin/python import math class vector: def init

More information

Variables One More (but not the last) Time with feeling

Variables One More (but not the last) Time with feeling 1 One More (but not the last) Time with feeling All variables have the following in common: a name a type ( int, float, ) a value an owner We can describe variables in terms of: who owns them ( Processing

More information

MITOCW ocw f99-lec07_300k

MITOCW ocw f99-lec07_300k MITOCW ocw-18.06-f99-lec07_300k OK, here's linear algebra lecture seven. I've been talking about vector spaces and specially the null space of a matrix and the column space of a matrix. What's in those

More information

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09 Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09 Hello, today we will create another application called a math quiz. This

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

MITOCW watch?v=4dj1oguwtem

MITOCW watch?v=4dj1oguwtem MITOCW watch?v=4dj1oguwtem PROFESSOR: So it's time to examine uncountable sets. And that's what we're going to do in this segment. So Cantor's question was, are all sets the same size? And he gives a definitive

More information

MITOCW ocw f99-lec12_300k

MITOCW ocw f99-lec12_300k MITOCW ocw-18.06-f99-lec12_300k This is lecture twelve. OK. We've reached twelve lectures. And this one is more than the others about applications of linear algebra. And I'll confess. When I'm giving you

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Lecture 23 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality, educational resources for free. To make a

More information

contain a geometry package, and so on). All Java classes should belong to a package, and you specify that package by typing:

contain a geometry package, and so on). All Java classes should belong to a package, and you specify that package by typing: Introduction to Java Welcome to the second CS15 lab! By now we've gone over objects, modeling, properties, attributes, and how to put all of these things together into Java classes. It's perfectly okay

More information

Lab 2: Conservation of Momentum

Lab 2: Conservation of Momentum 3 Lab 2: Conservation of Momentum I. Before you come to lab... II. Background III. Introduction A. This lab will give you an opportunity to explore the conservation of momentum in an interesting physical

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Recitation 1 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make

More information

Coursework Lab A. Open the coursework project

Coursework Lab A. Open the coursework project Coursework Lab A The aim of this lab session is for you to learn the basics of how the coursework framework works. In this first of two lab sessions you will learn about drawing backgrounds and handling

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Recitation 2 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make

More information

PROFESSOR: So far in this course we've been talking a lot about data abstraction. And remember the idea is that

PROFESSOR: So far in this course we've been talking a lot about data abstraction. And remember the idea is that MITOCW Lecture 4B [MUSIC-- "JESU, JOY OF MAN'S DESIRING" BY JOHANN SEBASTIAN BACH] PROFESSOR: So far in this course we've been talking a lot about data abstraction. And remember the idea is that we build

More information

What's the Slope of a Line?

What's the Slope of a Line? What's the Slope of a Line? These lines look pretty different, don't they? Lines are used to keep track of lots of info -- like how much money a company makes. Just off the top of your head, which of the

More information

6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

More information

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5 Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5 [talking head] This lecture we study theory design and implementation. Programmers have two roles to play here. In one role, they

More information

Post Experiment Interview Questions

Post Experiment Interview Questions Post Experiment Interview Questions Questions about the Maximum Problem 1. What is this problem statement asking? 2. What is meant by positive integers? 3. What does it mean by the user entering valid

More information

CircuitPython 101: Working with Lists, Iterators and Generators

CircuitPython 101: Working with Lists, Iterators and Generators CircuitPython 101: Working with Lists, Iterators and Generators Created by Dave Astels Last updated on 2018-11-01 12:06:56 PM UTC Guide Contents Guide Contents Overview List Functions Slicing Filtering

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Recitation 4 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To make

More information

append() function, 66 appending, 65, 97, 296 applications (apps; programs), defined, 2, 296

append() function, 66 appending, 65, 97, 296 applications (apps; programs), defined, 2, 296 Index Note: Page numbers followed by f, n, or t indicate figures, notes, and tables, respectively. Symbols += (addition and assignment operator), 100, 187 + (addition operator), \ (backslash), 240 / (division

More information

Skill 1: Multiplying Polynomials

Skill 1: Multiplying Polynomials CS103 Spring 2018 Mathematical Prerequisites Although CS103 is primarily a math class, this course does not require any higher math as a prerequisite. The most advanced level of mathematics you'll need

More information

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1

IDM 232. Scripting for Interactive Digital Media II. IDM 232: Scripting for IDM II 1 IDM 232 Scripting for Interactive Digital Media II IDM 232: Scripting for IDM II 1 PHP HTML-embedded scripting language IDM 232: Scripting for IDM II 2 Before we dive into code, it's important to understand

More information

Python Games. Session 1 By Declan Fox

Python Games. Session 1 By Declan Fox 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

More information

Intro. Classes & Inheritance

Intro. Classes & Inheritance Intro Functions are useful, but they're not always intuitive. Today we're going to learn about a different way of programming, where instead of functions we will deal primarily with objects. This school

More information

(Refer Slide Time: 06:01)

(Refer Slide Time: 06:01) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 28 Applications of DFS Today we are going to be talking about

More information

Lesson 1. Importing and Organizing Footage using Premiere Pro CS3- CS5

Lesson 1. Importing and Organizing Footage using Premiere Pro CS3- CS5 Lesson 1 Importing and Organizing Footage using Premiere Pro CS3- CS5 When working with a video editor the video source will come from either a capturing process or importing video clips into the editing

More information

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

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming Intro to Programming Unit 7 Intro to Programming 1 What is Programming? 1. Programming Languages 2. Markup vs. Programming 1. Introduction 2. Print Statement 3. Strings 4. Types and Values 5. Math Externals

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

6.001 Notes: Section 17.5

6.001 Notes: Section 17.5 6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Lecture 2 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To make a donation

More information

(Refer Slide Time: 02.06)

(Refer Slide Time: 02.06) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 27 Depth First Search (DFS) Today we are going to be talking

More information

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships

Instructor: Craig Duckett. Lecture 04: Thursday, April 5, Relationships Instructor: Craig Duckett Lecture 04: Thursday, April 5, 2018 Relationships 1 Assignment 1 is due NEXT LECTURE 5, Tuesday, April 10 th in StudentTracker by MIDNIGHT MID-TERM EXAM is LECTURE 10, Tuesday,

More information

Lesson 6: Manipulating Equations

Lesson 6: Manipulating Equations Lesson 6: Manipulating Equations Manipulating equations is probably one of the most important skills to master in a high school physics course. Although it is based on familiar (and fairly simple) math

More information

Note: Please use the actual date you accessed this material in your citation.

Note: Please use the actual date you accessed this material in your citation. MIT OpenCourseWare http://ocw.mit.edu 18.06 Linear Algebra, Spring 2005 Please use the following citation format: Gilbert Strang, 18.06 Linear Algebra, Spring 2005. (Massachusetts Institute of Technology:

More information

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

Most of the class will focus on if/else statements and the logical statements (conditionals) that are used to build them. Then I'll go over a few With notes! 1 Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few useful functions (some built into standard

More information

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we have to talk about the way in which we represent the

More information

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise If you re not already crazy about Scheme (and I m sure you are), then here s something to get

More information

CS12020 for CGVG. Practical 1. Jim Finnis

CS12020 for CGVG. Practical 1. Jim Finnis CS12020 for CGVG Practical 1 Jim Finnis (jcf1@aber.ac.uk) About me 20 years in the games industry (more or less) Windows, PS2, Xbox, Gamecube, Wii development experience DirectX (more than I like to think

More information

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables Instructor: Craig Duckett Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables 1 Assignment 1 is due LECTURE 5, Tuesday, April 10 th, 2018 in StudentTracker by MIDNIGHT MID-TERM

More information

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7 CPS109 Course Notes 7 Alexander Ferworn Unrelated Facts Worth Remembering The most successful people in any business are usually the most interesting. Don t confuse extensive documentation of a situation

More information

Transcriber(s): Aboelnaga, Eman Verifier(s): Yedman, Madeline Date Transcribed: Fall 2010 Page: 1 of 9

Transcriber(s): Aboelnaga, Eman Verifier(s): Yedman, Madeline Date Transcribed: Fall 2010 Page: 1 of 9 Page: 1 of 9 0:00 1 R1 The color s not going to show a little bit, but okay. Okay. So, um, a plus b quantity cubed, you said, means Stephanie a plus b times a plus b times a plus b /R1 3 R1 Okay, so you

More information

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller Table of Contents Introduction!... 1 Part 1: Entering Data!... 2 1.a: Typing!... 2 1.b: Editing

More information

How to Improve Your Campaign Conversion Rates

How to Improve Your  Campaign Conversion Rates How to Improve Your Email Campaign Conversion Rates Chris Williams Author of 7 Figure Business Models How to Exponentially Increase Conversion Rates I'm going to teach you my system for optimizing an email

More information

Instructions for Crossword Assignment CS130

Instructions for Crossword Assignment CS130 Instructions for Crossword Assignment CS130 Purposes: Implement a keyboard interface. 1. The program you will build is meant to assist a person in preparing a crossword puzzle for publication. You have

More information

Notes from the Boards Set # 5 Page

Notes from the Boards Set # 5 Page 1 Yes, this stuff is on the exam. Know it well. Read this before class and bring your questions to class. Starting today, we can no longer write our code as a list of function calls and variable declarations

More information

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page]

Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page] Quiz 3; Tuesday, January 27; 5 minutes; 5 points [Solutions follow on next page] 1. Does the Java expression x + y == z have a side-effect? If so, what is it? 2. Write a function named add that can add

More information

Intro. Speed V Growth

Intro. Speed V Growth Intro Good code is two things. It's elegant, and it's fast. In other words, we got a need for speed. We want to find out what's fast, what's slow, and what we can optimize. First, we'll take a tour of

More information

It Might Be Valid, But It's Still Wrong Paul Maskens and Andy Kramek

It Might Be Valid, But It's Still Wrong Paul Maskens and Andy Kramek Seite 1 von 5 Issue Date: FoxTalk July 2000 It Might Be Valid, But It's Still Wrong Paul Maskens and Andy Kramek This month, Paul Maskens and Andy Kramek discuss the problems of validating data entry.

More information

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return Recursion CS 1 Admin How's the project coming? After these slides, read chapter 13 in your book Yes that is out of order, but we can read it stand alone Quizzes will return Tuesday Nov 29 th see calendar

More information

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions. Handout 2 Functions, Lists, For Loops and Tuples [ ] Functions -- parameters/arguments, "calling" functions, return values, etc. Please make sure you understand this example: def square(x): return x *

More information

Load your files from the end of Lab A, since these will be your starting point.

Load your files from the end of Lab A, since these will be your starting point. Coursework Lab B It is extremely important that you finish lab A first, otherwise this lab session will probably not make sense to you. Lab B gives you a lot of the background and basics. The aim of the

More information

Slicing. Open pizza_slicer.py

Slicing. Open pizza_slicer.py Slicing and Tuples Slicing Open pizza_slicer.py Indexing a string is a great way of getting to a single value in a string However, what if you want to use a section of a string Like the middle name of

More information

Pyganim Documentation

Pyganim Documentation Pyganim Documentation Release 0.9.0 Al Sweigart Oct 30, 2017 Contents 1 Installation 3 1.1 Background Information......................................... 3 1.2 Installation................................................

More information

Java Programming. Computer Science 112

Java Programming. Computer Science 112 Java Programming Computer Science 112 Review: Problem solving Class 4 is the Whole Point of Programming. You can keep the papers; we gonna go through them on the board. If you are desperately confused

More information

P1_L3 Operating Systems Security Page 1

P1_L3 Operating Systems Security Page 1 P1_L3 Operating Systems Security Page 1 that is done by the operating system. systems. The operating system plays a really critical role in protecting resources in a computer system. Resources such as

More information

AN INTRODUCTION TO SCRATCH (2) PROGRAMMING

AN INTRODUCTION TO SCRATCH (2) PROGRAMMING AN INTRODUCTION TO SCRATCH (2) PROGRAMMING Document Version 2 (04/10/2014) INTRODUCTION SCRATCH is a visual programming environment and language. It was launched by the MIT Media Lab in 2007 in an effort

More information

B - Broken Track Page 1 of 8

B - Broken Track Page 1 of 8 B - Broken Track There's a gap in the track! We need to make our robot even more intelligent so it won't get stuck, and can find the track again on its own. 2017 https://www.hamiltonbuhl.com/teacher-resources

More information

Excel Basics Fall 2016

Excel Basics Fall 2016 If you have never worked with Excel, it can be a little confusing at first. When you open Excel, you are faced with various toolbars and menus and a big, empty grid. So what do you do with it? The great

More information

Programming with Python

Programming with Python Programming with Python Dr Ben Dudson Department of Physics, University of York 21st January 2011 http://www-users.york.ac.uk/ bd512/teaching.shtml Dr Ben Dudson Introduction to Programming - Lecture 2

More information

The following content is provided under a Creative Commons license. Your support

The following content is provided under a Creative Commons license. Your support MITOCW Lecture 10 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make a

More information

Instructor (Mehran Sahami):

Instructor (Mehran Sahami): Programming Methodology-Lecture26 Instructor (Mehran Sahami): All right. Welcome back to what kind of day is it going to be in 106a? Anyone want to fun-filled and exciting. It always is. Thanks for playing

More information

Molecular Statistics Exercise 1. As was shown to you this morning, the interactive python shell can add, subtract, multiply and divide numbers.

Molecular Statistics Exercise 1. As was shown to you this morning, the interactive python shell can add, subtract, multiply and divide numbers. Molecular Statistics Exercise 1 Introduction This is the first exercise in the course Molecular Statistics. The exercises in this course are split in two parts. The first part of each exercise is a general

More information

Pygame In a Few Minutes

Pygame In a Few Minutes Pygame In a Few Minutes By: Paul W. Yost Updated: 22 January 2016 Using Pygame: To use pygame, it must be imported and then initialized. import pygame pygame.init() # load the module # must be initialized

More information

3.7. Vertex and tangent

3.7. Vertex and tangent 3.7. Vertex and tangent Example 1. At the right we have drawn the graph of the cubic polynomial f(x) = x 2 (3 x). Notice how the structure of the graph matches the form of the algebraic expression. The

More information

MITOCW watch?v=9h6muyzjms0

MITOCW watch?v=9h6muyzjms0 MITOCW watch?v=9h6muyzjms0 The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high quality educational resources for free. To

More information

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using the API: Introductory Graphics Java Programming 1 Lesson 8 Using Java Provided Classes In this lesson we'll focus on using the Graphics class and its capabilities. This will serve two purposes: first

More information

Video Games. Writing Games with Pygame

Video Games. Writing Games with Pygame Video Games Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by-nc-sa/3.0

More information

Combinatorics Prof. Dr. L. Sunil Chandran Department of Computer Science and Automation Indian Institute of Science, Bangalore

Combinatorics Prof. Dr. L. Sunil Chandran Department of Computer Science and Automation Indian Institute of Science, Bangalore Combinatorics Prof. Dr. L. Sunil Chandran Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 5 Elementary concepts and basic counting principles So, welcome

More information

There we are; that's got the 3D screen and mouse sorted out.

There we are; that's got the 3D screen and mouse sorted out. Introduction to 3D To all intents and purposes, the world we live in is three dimensional. Therefore, if we want to construct a realistic computer model of it, the model should be three dimensional as

More information

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION

NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION LESSON 15 NESTED IF STATEMENTS AND STRING/INTEGER CONVERSION OBJECTIVE Learn to work with multiple criteria if statements in decision making programs as well as how to specify strings versus integers in

More information

PyGame Sprites. an excellent turorial exists for PyGame sprites here kai.vm.bytemark.co.uk/ piman/writing/spritetutorial.shtml.

PyGame Sprites. an excellent turorial exists for PyGame sprites here  kai.vm.bytemark.co.uk/ piman/writing/spritetutorial.shtml. PyGame Sprites slide 1 an excellent turorial exists for PyGame sprites here http:// kai.vm.bytemark.co.uk/ piman/writing/spritetutorial.shtml. these notes are derived from this tutorial and the examples

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment

Working with Objects. Overview. This chapter covers. ! Overview! Properties and Fields! Initialization! Constructors! Assignment 4 Working with Objects 41 This chapter covers! Overview! Properties and Fields! Initialization! Constructors! Assignment Overview When you look around yourself, in your office; your city; or even the world,

More information

Shadows in the graphics pipeline

Shadows in the graphics pipeline Shadows in the graphics pipeline Steve Marschner Cornell University CS 569 Spring 2008, 19 February There are a number of visual cues that help let the viewer know about the 3D relationships between objects

More information