Smoother Graphics Taking Control of Painting the Screen

Similar documents
SPRITES Moving Two At the Same Using Game State

Painting your window

SPRITES Making Things Move Around The Screen

HYPERSTUDIO TOOLS. THE GRAPHIC TOOL Use this tool to select graphics to edit. SPRAY PAINT CAN Scatter lots of tiny dots with this tool.

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Dice in Google SketchUp

Creative Sewing Machines Workbook based on BERNINA Embroidery Software V8

Instructions for Crossword Assignment CS130

SketchUp Tool Basics

SCRATCH MODULE 3: NUMBER CONVERSIONS

[ the academy_of_code] Senior Beginners

SNOWFLAKES PHOTO BORDER - PHOTOSHOP CS6 / CC

Adobe Flash CS3 Reference Flash CS3 Application Window

Photoshop Fundamentals

ACS-1805 Introduction to Programming (with App Inventor)

In this lesson, you ll learn how to:

Making Your First Character

Advanced Special Effects

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

Contents. How to use Magic Ink... p Creating Magic Revealers (with Magic Ink)... p Basic Containers... p. 7-11

Customizing DAZ Studio

S A M P L E C H A P T E R

1. Defining Procedures and Reusing Blocks

We created a few different effects and animations using this technique as applied to clones.

Art 486: Introduction to Interactive Media.

Google SketchUp Design Exercise 1

UV Mapping to avoid texture flaws and enable proper shading

Creative Effects with Illustrator

In this lecture we will briefly examine a few new controls, introduce the concept of scope, random numbers, and drawing simple graphics.

ONE HOUR ANIMATION. Will you be a Scratcher upon completion of this session? Definitely. Learn how to write a basic script to animate a sprite.

InfoSphere goes Android Flappy Bird

Making a maze with Scratch

Pong in Unity a basic Intro

Visual C# Program: Simple Game 3

Tutorial on Using Windows 8

Animations involving numbers

Menus. You ll find MenuStrip listed in the Toolbox. Drag one to your form. Where it says Type Here, type Weather. Then you ll see this:

Adobe illustrator Introduction

4.7 Approximate Integration

This lesson will focus on the Bouncing Ball exercise.

Polygons and Angles: Student Guide

SketchUp Quick Start For Surveyors

HOUR 4 Understanding Events

Using Flash Animation Basics

Word: Print Address Labels Using Mail Merge

Hello! ios Development

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

animation, and what interface elements the Flash editor contains to help you create and control your animation.

2. Click on the Freeform Pen Tool. It looks like the image to the right. If it s not showing, right click on that square and choose it from the list.

Making use of other Applications

SSEA Newbies Handout 09 Summer 2012 August 17 th, 2012 Assignment 4: Bouncing Ball and Bizz Bazz Buzz

Creating Breakout - Part 2

Animations that make decisions

The Fundamentals. Document Basics

HAPPY HOLIDAYS PHOTO BORDER

XNA Tutorials Utah State University Association for Computing Machinery XNA Special Interest Group RB Whitaker 21 December 2007

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Device: PLT This document Version: 3. For hardware Version: 1. For firmware Version: Date: 20 Oct 2017

We will start our journey into Processing with creating static images using commands available in Processing:

Create a memory DC for double buffering

Using Tab Stops in Microsoft Word

Making ecards Can Be Fun!

Simple Glass TNT Molecule Tutorial

OrbBasic Lesson 1 Goto and Variables: Student Guide

The first thing we ll need is some numbers. I m going to use the set of times and drug concentration levels in a patient s bloodstream given below.

Assignment 5: Part 1 (COMPLETE) Sprites on a Plane

Device: PLT This document Version: 1. For hardware Version: 1. For firmware Version: Date: 9 May 2014

Lost in Space. Introduction. Step 1: Animating a spaceship. Activity Checklist. You are going to learn how to program your own animation!

Introduction to Flash - Creating a Motion Tween

Creating an Animated Navigation Bar in InDesign*

Lost in Space. Introduction. Scratch. You are going to learn how to program your own animation! Activity Checklist.

Strategic Series-7001 Introduction to Custom Screens Version 9.0

Module 2, Add on lesson Introduction to the NXT and Mindstorms software. Teacher. 45 minutes

OrbBasic 1: Student Guide

Paint Tutorial (Project #14a)

Tips & Tricks for Microsoft Word

1 Getting started with Processing

Java Programming. Computer Science 112

Table of Contents. How to use this document. How to use the template. Page 1 of 9

Math Dr. Miller - Constructing in Sketchpad (tm) - Due via by Friday, Mar. 18, 2016

Let s Make a Front Panel using FrontCAD

Printing Envelopes in Microsoft Word

(0,0) (600, 400) CS109. PictureBox and Timer Controls

Chapter 13. Graphics, Animation, Sound and Drag-and-Drop The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Introduction. Create a New Project. Create the Main Form. Assignment 1 Lights Out! in C# GUI Programming 10 points

Interactive Powerpoint. Jessica Stenzel Hunter Singleton

Carol Freeman. Photographer

Installing and Configuring the Voice UPB Bridge updated 1-Jan-2019

Device: PLT This document Version: 3.0. For hardware Version: 4. For firmware Version: Date: April 2014

This lesson will focus on the Bouncing Ball exercise. Feel free to start from scratch or open one of the accompanying demo files.

ACTIVE PROCESSING Summary Learning Objectives How to Proceed Check your Understanding Learning Objectives 412

Fantasy Cartography with Adobe Photoshop: Adapted for Gimp

In this lesson you are going to create a drawing program similar to Windows Paint. 1. Start with a new project and remove the default cat sprite.

Table of Laplace Transforms

Computer and Programming: Lab 1

Platform Games Drawing Sprites & Detecting Collisions

Basic Computer and Mouse Skills Windows 10

Taskbar: Working with Several Windows at Once

Summer 2012 Animation

Topic Notes: Java and Objectdraw Basics

Transcription:

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 at the same time. And things sloooowwwwed down you ended up with a slide show rather than a smooth running game. So how to make the game run smoother is smoother actually a word? One likely reason your game was bogging down is that trying to animate a sprite using a PictureBox is really, really, really slow. PictureBoxes simply weren t designed for sprite animation, they were designed to surprise display pictures. One of the first steps we can take to have smoother sprite animation is to take control of drawing (actually referred to as painting) the window ourselves which immediately begs the question how does the window get painted? Hopefully by now you are familiar with the concept of events and event handlers. For example, you should now be familiar with the KeyDown event: this event is triggered every time a keyboard key is pushed down. You have successfully created a handler for this event by going to the properties tab for the Form and double-clicking on the KeyDown event line. You ve also played with timers and have created a handler for the timer Tick event. One great feature of event handlers is each provides parameters that contain essential information pertaining to the event. Again looking at the example of the KeyDown event, the handler provides two parameters: 1. object sender 2. KeyEventArgs e You ve used the KeyEventArgs e parameter to determine which key was pressed by referencing the KeyCode property like this: Well, there just so happens to be an event for painting the window. This event happens anytime the system determines the window needs to be repainted. Examples of when this will happen are: If the window is restored after being minimized If something (perhaps another window) was covering or partially covering our window and has been moved Now those are things over which we really have no control over in our program they are things that happen to our program. What we need is in our program, some way of saying we ve changed the stuff on the window, please repaint it. Well there is a very easy way to do this: we just have our Form call the Invalidate() method. The Invalidate method invalidates the entire surface of the control and causes the control to be redrawn. Boom, exactly what we need. Let s get to it then. Page 1 of 13

Create a new windows form app project (name it UserPaintExample). Hit F7 to get to the code window. now before we go any further, I want you to understand that we are now going to start writing by hand some of the code the IDE has been writing for us. This means you have to type things EXACTLY the way I show you. It isn t much, but you do have to pay attention to detail and get everything perfect. The first thing we need to do is tell Windows that we are taking control of painting our window, and are providing our own Paint event handler. In your Form1 constructor, enter the following: You will notice the red squiggly line under Form1_Paint in the code above that is the name of the event handler method and is okay because we haven t written it yet. There are some things in the above code that may look like hieroglyphics: Control.Styles.UserPaint: if true the form is responsible for painting itself rather than the system. Control.Styles.AllPaintingInWmPaint: if this and UserPaint are both true, the background is not erased in order to reduce flicker. Control.Styles.OptimizedDoubleBuffer: the form is drawn using double buffering the drawing is actually done to a memory buffer (which is quite fast), then the buffer is blasted to the screen as one. Much faster. At this point you don t really need a deep understanding of the above items. It is sufficient to understand that these settings/styles are what enable us to have faster graphics. Double buffering the graphics and not erasing the background all the time are huge wins for us. Page 2 of 13

Let s get rid of the red squiggly line under Form1_Paint: enter the following as a new method after the constructor: Notice that the red squiggly line under Form1_Paint in the constructor is now gone. That is because the event handler now exists. Also notice the 2 nd parameter of the event handler: PaintEventArgs e. This gives us access to everything necessary to draw stuff on the screen, because one of the things it provides is the graphics context for drawing. Think of the graphics context as the canvas we will be drawing and painting on. Page 3 of 13

Just for fun, add a line inside the Form1_Paint method and type e.graphics. and the IDE will pop-up the help dialog showing you all the features of the Graphics context. Scroll down in it and you will find things like DrawRectangle, DrawPolygon, DrawEllipse, among many others: These are your key to faster graphics, and are what we will use instead of PictureBox to draw sprites. Go ahead and erase what you just typed (e.graphics.). Page 4 of 13

Now if you recall, on your prior games you likely had a timer running that would move all the PictureBoxes on the screen every timer tick. To make things work with our new Paint event handler, all we need to do is have our timer tick tell Windows that our screen is invalidated and needs to be redrawn. As mentioned before, we do that by calling Invalidate(). Add a timer called refreshtimer only this time instead of dragging and dropping it into the Design window, let s add it by hand in our own code. Don t worry, this is super easy. We will add a new field to our Form1 class. Its type will be Timer and we ll initialize it in the Form1 constructor as follows: Set its interval to something like 25 Add a Tick event handler called refreshtimer_tick And enable the timer. Here is what it should look like: Again you will notice the red squiggly line under refreshtimer_tick which is the event handler method we haven t written it yet. Page 5 of 13

So let s write it now. It is super easy and super short; just make sure you get everything exact. I added it right before the Paint event handler; doesn t really matter where you put it as long as it s in the Form1 class. Right now all it needs to do is tell Windows it is time to repaint the screen by calling Invalidate(). If you build and run your program right now, you should see a blank window appear. That s okay because we re not drawing anything yet! Next, let s add a ball on the screen. Okay, it won t be a ball we re going to start off very simple and draw a square in the center of the screen. Page 6 of 13

There are a couple things we will need to keep track of for our ball that PictureBox did automatically for us: 1. The location of the ball 2. The size of the ball Let s add some fields to keep track of this info. The location can be a Point, the size can be a Size. We will initialize each in the constructor. We ll put the location in the center of the screen, and make the size 5 x 5: Excellent! Now, let s draw our square. Inside our Paint event handler, we just need to use the Graphics context to draw and fill a rectangle (remember from geometry that a square is just a special rectangle). Page 7 of 13

Add the following code in the Paint event handler: First we get a reference to the Graphics context with the line Next we draw the rectangle (creates the outline) and then we fill it (color it in). To do so we have to provide the location and size of the rectangle. Remember, we set the size to (5, 5) which will make this a square. Also notice the red squiggly lines under the words pen and brush this is where things start to get a little different. In the Graphics context, we draw with a pen and fill with a brush. They have the red squiggly lines because we haven t created the pen or brush yet. Let s do that now. Add them as fields and initialize them in the Form1 constructor: We create a blue brush and use that brush to create a blue pen. Now, compile and run. You should see a little blue box in the center of the window! Page 8 of 13

Cool but boring. Let s get the ball moving. Page 9 of 13

Nothing new here we ll use the same technique used to move PictureBoxes we ll just repeatedly add a delta to the X coordinate and a delta to the Y coordinate of the location. These deltas (let s call them dx and dy) will control the movement of the ball. The loc point controls the location of the ball. Create dx and dy fields (ints) and initialize them in the constructor. For now set the movement to 4 & 6: Page 10 of 13

If you compile and run, the ball won t be moving yet. In the refreshtimer_tick event handler (where we tell windows to repaint the screen) we will add the code to actually change the location of the ball. Again, this is exactly what we ve done before: simply add dx to the loc.x and same with Y. There is one trick we need to pull while we re painting the screen, we don t want or need the refresh timer to tick again. We will pause the refresh timer and then restart it when we re done drawing. Add the following to the Form1_Paint event handler: Compile and run, and you ll see the ball move diagonally downward and off the screen. We re moving! Page 11 of 13

We re basically there; let s just make the ball bounce off the edges of the screen as a final touch. Again, there is nothing new to this. Main trick is to figure out where to do this. It makes the most sense to check if the ball has hit the screen bounds right after moving it. That would mean we ll add this code in the refreshtimer_tick immediately after adding the deltas to the location, and before invalidating the screen: Page 12 of 13

Compile and run, the ball should now bounce off the sides of the screen. There is lots you can quickly play with. For instance see if you can figure out how to make the ball an actual circle. I ll give you a hint: just like a square is a special rectangle, a circle is a special ellipse. If you succeed in changing the ball to a circle, you may want to make it bigger so you can see its shape better; I d suggest making the size 15x15. So there you have it. Yell if you have any questions! Oh yeah make sure you save this project, we ll be modifying it in future lessons. Page 13 of 13