Pixels, Colors and Shapes

Slides:



Advertisements
Similar presentations
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Advertisements

Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
RAPTOR Syntax and Semantics By Lt Col Schorsch
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Image Processing & Perception Sec 9-11 Web Design.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Image Representation. Objectives  Bitmaps: resolution, colour depth and simple bitmap file calculations.  Vector graphics: drawing list – objects and.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
PyGame - Unit 2 PyGame Unit – – Animation.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Graphics Basic Concepts 1.  A graphic is an image or visual representation of an object.  A visual representation such as a photo, illustration or diagram.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
AP CSP: Pixelation – B&W/Color Images
By Melissa Dalis Professor Susan Rodger Duke University June 2011
Multidimensional Arrays
Sprites (Images) and Sounds
Sound and more Animations
CSS Box Model.
CSS Box Model.
CSS Layouts CH 13.
David Meredith Aalborg University
MOM! Phineas and Ferb are … Aims:
Animations.
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Keyboard Input.
9. Drawing.
Image Processing & Perception
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Compression (of this 8-bit 397,000 pixel image):
Building Java Programs
Building Java Programs
Example: Card Game Create a class called “Card”

9. Drawing Let's Learn Python and Pygame
Lesson One: The Beginning Chapter 1: Pixels Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from
Microsoft® Small Basic
Chapter 5 Working with Images
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Paddle Ball! We will begin creating, in steps, a video game similar to ‘brick breaker’ or ‘pong’, were we can move paddles to hit a bouncing ball. I hope.
Learning to program with Logo
Basic Graphics Drawing Shapes 1.
Colors.
Computer Graphics.
Learning Objective LO: We’re learning to understand when it is appropriate to use particular data types.
GRAPHING CALCULATOR Part 1
13. Sprites Let's Learn Python and Pygame
Chapter 2 Graphics Programming with C++ and the Dark GDK Library
Introduction to Media Computation
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Otasuke GP-EX! Chapter 2 Run State Screen.
Let’s begin our game!.
Chapter 15, Images …a few points
Non-numeric Data Representation
Chapter 1 Introducing Small Basic
Working with images and scenes
Chapter 7 The Game Loop and Animation
Presentation transcript:

Pixels, Colors and Shapes

Pixel Coordinates The window that the “Hello World” program created was just composed of little square dots on your screen called pixels. Each pixel starts off as black but can be set to a different color. Imagine that instead of a Surface object that is 400 pixels wide and 300 pixels tall, we just had a Surface object that was 8 pixels by 8 pixels. If that tiny 8x8 Surface was enlarged so that each pixel looks like a square in a grid, then a good representation of it could look something like this:

Pixel Coordinates If you’ve taken a math class and learned about Cartesian Coordinates, you might notice that the Y-axis starts at 0 at the top and then increases going down, rather than increasing as it goes up. This is just how Cartesian Coordinates work in Pygame (and almost every programming language). Each column of the X-axis and each row of the Y-axis will have an “address” that is an integer from 0 to 7 so that we can locate any pixel by specifying the X and Y axis integers. For example, in the above 8x8 image, we can see that the pixels at the XY coordinates (4, 0), (2, 2), (0, 5), and (5, 6) have been painted black, the pixel at (2, 4) has been painted gray, while all the other pixels are painted white. XY coordinates are also called points.

Functions Vs. Methods import whammy fizzy() // function call egg = Wombat() // Constructor function -returns an object egg.blahblah() // method call (associated w/ object) whammy.spam() // function – see import

A Bit about Surface Objects Surface objects are objects that represent a rectangular 2D image. The pixels of the Surface object can be changed by calling the Pygame drawing functions (more on these later…) and then displayed on the screen. The window border, title bar, and buttons are not part of the display Surface object.

A Bit about Surface Objects The Surface object returned by pygame.display.set_mode() is called the display Surface. Anything that is drawn on the display Surface object will be displayed on the window when the pygame.display.update() function is called. It is a lot faster to draw on a Surface object (which only exists in the computer’s memory) than it is to draw a Surface object to the computer screen. Computer memory is much faster to change than pixels on a monitor.

A Bit about Surface Objects Often your program will draw several different things to a Surface object. Once you are done drawing everything on the display Surface object for this iteration of the game loop (called a frame), it can be drawn to the screen. The computer can draw frames very quickly, and our programs will often run around 30 frames per second (that is, 30 FPS). This is called the “frame rate”

Colors

How is the Color of a PIXEL recorded? It is represented using the RGB (Red, Green, Blue) color model. In Python, we use tuples of 3 integers to represent the values of red, green, and blue each range from 0 to 255. 256 x 256 x 256 = 16,777,216 COLORS! Error if <0 or >255

Color PIXELS Combining color is not the same as mixing paint to make a color. You can make yellow by combining red and green! The computer uses light to display color, not paint. SEE CD for example!

Color PIXELS What RGB values do you think represents black? (?,?,?) What RGB values do you think represents white? (?,?,?) Only 16,777,214 colors Left!

Color - Opacity The RGB model sometimes includes an alpha value as well that indicates how transparent or opaque the color is. A color that is transparent will let you see some of the color beneath it. Think OPACITY in Alice Alpha value of 0 - completely transparent Alpha value of 255 – not transparent

Color - Opacity

Color Exercise Time to explore some colors! Follow the handout and explore COlOR_CHOOSER!

Color Objects One way to create a color is as a pygame.Color object. You can create Color objects by calling the pygame.Color() constructor function and passing either three or four integers. You can store this Color object in variables just like you can store tuples in variables.

Drawing Shapes

Rectangle Objects Pygame has two ways to represent rectangular areas (just like there are two ways to represent colors). The first is a tuple of four integers: 1. The X coordinate of the top left corner. 2. The Y coordinate of the top left corner. 3. The width (in pixels) of the rectangle. 4. Then height (in pixels) of the rectangle.

Rectangle Objects The second way is as a pygame.Rect object, which we will call Rect objects for short. For example, the code below creates a Rect object with a top left corner at (10, 20) that is 200 pixels wide and 300 pixels tall:

Rectangle Objects The handy thing about this is that the Rect object automatically calculates the coordinates for other features of the rectangle. For example, if you need to know the X coordinate of the right edge of the pygame.Rect object we stored in the spamRect variable, you can just access the Rect object’s right attribute:

Rectangle Objects The Pygame code for the Rect object automatically calculated that if the left edge is at the X coordinate 10 and the rectangle is 200 pixels wide, then the right edge must be at the X coordinate 210. If you reassign the right attribute, all the other attributes are automatically recalculated:

Attributes that Pygame.Rect objects Provide

Rectangle Objects

Primitive Drawing Functions

are called drawing primitives. Pygame provides several different functions for drawing different shapes onto a surface object. These shapes such as: Rectangles Circles Ellipses Lines Individual pixels are called drawing primitives.

Our First Drawing Program

Drawing.py

Drawing.py Notice how we make constant variables for each of the colors? Doing this makes our code more readable. GREEN in the source code is easier to understand than (0, 255, 0) !

Drawing.py

Drawing.py Which Surface object to draw on The drawing functions are named after the shapes they draw. The parameters you pass these functions tell them: Which Surface object to draw on Where to draw the shape (and what size) What color to use And how wide to make the lines.

Drawing Functions Please refer to handout to see a list of the functions in more detail.

Pixel Array Objects Unfortunately, there isn’t a single function you can call that will set a single pixel to a color (unless you call pygame.draw.line() with the same start and end point). The Pygame framework needs to run some code behind the scenes before and after drawing to a Surface object. If it had to do this for every single pixel you wanted to set, your program would run much slower. (Almost 2 to 3 times slower).

pygame.PixelArray object Pixel Array Objects Instead, you should create a pygame.PixelArray object (we’ll call them PixelArray objects for short) of a Surface object and then set individual pixels. Creating a PixelArray object of a Surface object will “lock” the Surface object. While a Surface object is locked, the drawing functions can still be called on it, but it cannot have images like PNG or JPG images drawn on it.

Pixel Array Objects The PixelArray object that is returned from pygame.PixelArray() can have individual pixels set by accessing them with two indexes. For example, line 28’s pixObj[480][380] = BLACK will set the pixel at X coordinate 480 and Y coordinate 380 to be black.

Pixel Array Objects To tell Pygame that you are finished drawing individual pixels, delete the PixelArray object with a del statement. See line 33 Deleting the PixelArray object will “unlock” the Surface object so that you can once again draw images on it. If you forget to delete the PixelArray object, the next time you try to blit (that is, draw) an image to the Surface, you will get an ERROR.

Pygame.display. Update() function After you are done calling the drawing functions to make the display Surface object look the way you want, you must call pygame.display.update() to make the display Surface actually appear on the user’s monitor.

Pygame.display. Update() function The one thing that you must remember is that pygame.display.update() will only make the display Surface (that is, the Surface object that was returned from the call to pygame.display.set_mode()) appear on the screen. If you want the images on other Surface objects to appear on the screen, you must “blit” them (that is, copy them) to the display Surface object with the blit() method. We will discuss this later…

PROJECT TIME!!!! Use your new found knowledge of shapes to create your own works of art! Be creative, you can create houses, snowmen, landscapes and MORE!

What is Next? Animations Animations