SPRITES Making Things Move Around The Screen

Similar documents
SPRITES Moving Two At the Same Using Game State

Smoother Graphics Taking Control of Painting the Screen

Creating Breakout - Part 2

2 SELECTING AND ALIGNING

QUICK EXCEL TUTORIAL. The Very Basics

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.

Lastly, in case you don t already know this, and don t have Excel on your computers, you can get it for free through IT s website under software.

Use Geometry Expressions to create and graph functions, and constrain points to functions.

Lutheran High North Technology The Finder

Form Properties Window

Flowcharts for Picaxe BASIC

Program and Graphical User Interface Design

Visual C# Program: Simple Game 3

Math 1525 Excel Lab 1 Introduction to Excel Spring, 2001

Use Parametric notation. Interpret the effect that T has on the graph as motion.

Computer Essentials Session 1 Step-by-Step Guide

Textures and UV Mapping in Blender

Notes 3: Actionscript to control symbol locations

textures not patterns

Photoshop Fundamentals

Civil Engineering Computation

Using Tab Stops in Microsoft Word

UV Mapping to avoid texture flaws and enable proper shading

Working with Images 1 / 12

COMP : Practical 9 ActionScript: Text and Input

Dice in Google SketchUp

We are going to use some graphics and found a nice little batman running GIF, off course you can use any image you want for the project.

Meet the Cast. The Cosmic Defenders: Gobo, Fabu, and Pele The Cosmic Defenders are transdimensional

Learning to use the drawing tools


The MathType Window. The picture below shows MathType with all parts of its toolbar visible: Small bar. Tabs. Ruler. Selection.

PARTS OF A COMPUTER. (Class III, IV and V)

LESSON 8 COPYING SELECTIONS

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

Making use of other Applications

MITOCW ocw f99-lec07_300k

How to lower a car in Paint.NET (PDN) -Tools you will be using:

This lesson introduces Blender, covering the tools and concepts necessary to set up a minimal scene in virtual 3D space.

-Using Excel- *The columns are marked by letters, the rows by numbers. For example, A1 designates row A, column 1.

Motion Creating Animation with Behaviors

Layers (Just the Basics) By Jerry Koons

Edge Television Advertisement. -Open the 3 images of TV characters and the network logo in Photoshop. Your images must be high resolution images!

Hey there, I m (name) and today I m gonna talk to you about rate of change and slope.

ORB Education Quality Teaching Resources

Taskbar: Working with Several Windows at Once

Welcome to Introduction to Microsoft Excel 2010

Creating a Template in WordPerfect

MAKING A TRIPTYCH IN PHOTOSHOP CC

Spectroscopic Analysis: Peak Detector

Basics of Adobe Premiere

Basic Texturing Tutorial by Gary Wu

Whole Group: Basics of Graphing on your TI Calculator

Lesson 5: Creating Heterogeneous Parts

4 TRANSFORMING OBJECTS

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

Making Tables and Graphs with Excel. The Basics

Pong in Unity a basic Intro

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

Chapter One: Getting Started With IBM SPSS for Windows

L E S S O N 2 Background

Windows users range in experience from people

MathML Editor: The Basics *

Citrix Connectivity Help. Table of Contents

Get comfortable using computers

Direct Variations DIRECT AND INVERSE VARIATIONS 19. Name

Fruit Snake SECTION 1

Part 1: Understanding Windows XP Basics

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

Touring the Mac. S e s s i o n 3 : U S E A N APPLICATION

Transforming Objects in Inkscape Transform Menu. Move

2. This is a cell; this cell is designated as A1.

Premium POS Pizza Order Entry Module. Introduction and Tutorial

Shorthand for values: variables

Computer Basics: Step-by-Step Guide (Session 2)

Fire TV Quick Start BJM **DISCLAIMER**

Creating a Brochure. The right side of your Publisher screen will now change to Brochures.

PowerPoint Basics: Create a Photo Slide Show

R-09HR ReleaseNote. R-09HR Operating System Version 2.00 RO9HRRN200

Please click double click on the icon labeled Internet Explorer, the picture is a blue letter E.

ENGL 323: Writing for New Media Repurposing Content for the Web Part Two

Creating a Website Using Weebly.com (July 2012 Update)

HAPPY HOLIDAYS PHOTO BORDER

Microsoft Excel 2010 Basic

Chapter 1. Getting to Know Illustrator

UAccess ANALYTICS Next Steps: Creating Report Selectors

EchoSub v1.2 EchoStyle

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

Navigate to Success: A Guide to Microsoft Word 2016 For History Majors

Excel 2013 Intermediate

The Fundamentals. Document Basics

GUI s and Keyboards. Larry Rudolph March 13, Pervasive Computing MIT SMA 5508 Spring 2006 Larry Rudolph

FrontPage. Directions & Reference

Creating a Box-and-Whisker Graph in Excel: Step One: Step Two:

HEADINGS & TOCS IN WORD 2007

REMEMBER TO BRING YOUR MEMORY STICK TO EVERY LAB!

Instructions for Crossword Assignment CS130

Roc Model and Density Dependence, Part 1

ICS 61 Game Systems and Design Introduction to Scratch

SIMPLE TEXT LAYOUT FOR COREL DRAW. When you start Corel Draw, you will see the following welcome screen.

Click on the empty form and apply the following options to the properties Windows.

Transcription:

Unless you re playing something like Zork (GREATEST game in the world BTW!), you will likely need to move images around the screen. In this lesson we re going to work with simple 2D images, normally called sprites. We re going to learn: 1. About GUI screen coordinates 2. How to add an image to your GUI 3. How to move an image (sprite) around on the screen 4. How to determine when a key has been pressed and which one was pressed In order to get there, we ll have to go back to our algebra class. Yes, math is everywhere! Page 1 of 14

Take a look below do you remember this from algebra 1? It is the Cartesian coordinate system represented in graph form. The horizontal (left-right) axis is the x-axis. It increases to the right (is positive) and decreases to the left (is negative). The vertical (up-down) axis is the y-axis. It increases going up (is positive) and decreases going down (is negative). Each point on the graph is represented as an ordered pair which simply means two numbers (pair), that are ordered (the first is the x-axis location; the second is the y-axis location). The origin (center or point at which the x-axis and y-axis cross) is at (0, 0). Page 2 of 14

It is not uncommon to work with only one section (quadrant) of the graph. Here is a picture of the lower-right quadrant which we call quadrant IV (four). Page 3 of 14

So, what does this have to do with our GUI and sprites? Glad you asked! Here is a picture of a bare-bone GUI nothing on it yet. Page 4 of 14

What happens if we lay the graph of quadrant IV over the GUI? This is great! It gives us a way of identifying and locating points on the screen! We re putting the origin of the graph in the top-left part of the GUI screen. The x-axis runs to the right. The y-axis runs down. Page 5 of 14

There is only one little change we need to make going downward with the normal graph means y is negative or gets smaller. It s kind of a pain to work with negative numbers all the time, so what they did was say when we re messing with the GUI, let s just say the y values get bigger as you go down. So here is what we get Instead of (2, -3) for example, we have (2, 3). This will make our lives a bit simpler we just have to remember this is not the normal graph we re used to working with in algebra. Now for the meat-and-potatoes let s actually get something moving around the screen Page 6 of 14

1. Create a new Windows Forms Application (GUI project). 2. Change the text of the Form to SpriteMovementTest (in the properties panel). Next we want to get our sprite on the screen. We will use a PictureBox control. This is found in the Common Controls section of the Toolbox. The nice thing about a PictureBox is it can tell you some key information such as where it is on the screen and how big it is. Not only that, you can easily change its position on the screen. It is a great entry level sprite! Let s drag a PictureBox control onto the screen. Page 7 of 14

Time to load an image into our PictureBox control! 1. Open the property panel for the PictureBox 2. Select the Image property and click on the chooser icon to the far right. 3. This opens the Select Resource dialog a. Click the Import button in the bottom left Page 8 of 14

Select an image make sure it isn t very big as we just want to move a small sprite around. If the image you select doesn t seem to fit well in the PictureBox, no worries, all is not lost! The one I selected is 350 x 350 which is rather large for these purposes. The PictureBox can adjust the image to fit. By the way, if your image fits perfectly, you can skip this b. Change the SizeMode property of the PictureBox to Zoom. c. Notice my image is now centered in the PictureBox control. d. Also notice the PictureBox control is much wider than the image I selected. e. All I now need to do is adjust the sides of the PictureBox control to fit the image. Page 9 of 14

Remember the screen coordinate system? Let s see how that works with a PictureBox. The PictureBox has a property named Location. This holds the coordinate of the top-left corner of the control on the screen. Remember the origin (0, 0) is in the top-left corner of the usable (light-grey) part of the screen. Notice below that my image is at the location point (102, 96). Now click and drag the PictureBox control somewhere else on the screen I moved it closer to the bottom-right of the screen. Notice the new location point is (201, 190). Page 10 of 14

Okay, now we have an idea of how to tell where the sprite is on the screen. The question is how do we move it around using code? Easy-peasy! Because the location point of the sprite is provided in the Location property of the PictureBox, we can get at it in code. Let s name our PictureBox object something useful like spritepixbox: 1. Select the PictureBox control in the design view and open the properties panel. 2. Change the (Name) property to spritepixbox. Let s have our sprite start in the top-left corner of the screen. Rather than dragging it around in the design view, let s have our code do this. Since we want this to happen when our GUI starts up, we need to add this code in the constructor of our GUI. This will be in the method of the GUI that has the same name as the GUI class: Form1. Remember, the constructor is the method used to bake the cookie. Now for a bit of magic please bear with me on this. If I want to refer to the x coordinate of my sprite s location I would do it like this: This is great but the problem is you can only read the coordinate, not directly set it. To change the location of the sprite, you need to reset the entire location point property, not just one of the coordinates. The Location property is represented as a Point in code. So here is how you change the sprite s location: 1. Create a temporary Point variable 2. If you want to just move the sprite to a known absolute position: just set the temporary Point variable s X and Y values to the new coordinates 3. If you want to move the sprite relative to where it currently is at: then copy the sprite s location point into the temporary Point variable and update/adjust the temporary Point variable s X and Y values 4. Copy the temporary Point variable back into the sprite s location point this will reposition the sprite on the screen s coordinate system Run the code, your sprite should show up in the top-left corner of the GUI. Page 11 of 14

Sweet! Next step let s use the keyboard to move our sprite around. Your form (the GUI) can tell when a key is hit. There will be an event generated every time a key goes down. Actually there will be a unique event generated every time: 1. A key is PRESSED (goes down then back up again) 2. A key first goes DOWN (the start of being PRESSED) 3. A key comes back UP (is released, the 2 nd part of being PRESSED) Nine times out of ten what we really care about is when the key first goes down. Let s set up an event handler for any key going down. Inside that event handler, we ll check which key was hit, and if it was one of the movement keys we ll move the sprite accordingly. A quick but important note about handling keyboard hits the GUI control that currently has focus handles the event. What the heck does that mean? Have you noticed that if you hit the TAB key on a GUI with buttons and text entry boxes that the cursor and highlight moves from control to control? When the cursor/highlight is on a control, that control is said to have focus. Handling keyboard hits gets messy if you have controls on the GUI that can get focus. What we will need to do is make sure the basic GUI form is always notified that a key was hit. To do this, all you need to do is set the KeyPreview property for the form to True. Problem solved! Here we go let s use A, S, W, D to move left, down, up and right respectively. 1. In the design view select the form (main GUI window) 2. Open the Properties panel set the KeyPreview property to True 3. Open the Events panel double click on KeyDown this will create the event handler and open the code window with the cursor inside the event handler 4. Notice the 2nd of the two items inside the parenthesis of the event handler method line: Its name is e and is a variable of type KeyEventArgs This e variable is information provided to the event handler; it holds all the key press information. You can tell which key was hit by checking e.keycode. Now lets add an if statement to check if the key pressed (e.keycode) is the A key (Keys.A). Inside the curly braces for the if we will add code to move the sprite to the left. Not sure how to do this? Well, read on! Page 12 of 14

Okay, to figure out how to do this, go back two pages and review the process to move a sprite using code. It s at the bottom of page 11. Cheap right? Be quiet and go figure it out. Done? Good. Really? You re lying right? Yup do it go back and review the process. Ready? Okay Page 13 of 14

Since we can t directly change the sprite s location point coordinates, we ll have to create a temporary Point variable, copy the sprite s current location into it, then modify the temporary Point coordinates based on what key was pressed. I ll do it for the A key you get to do it for the rest. Make sure you pay attention to the comments in this code! Run your code. Your image should start off in the very top-left corner of the GUI. Now hit the A key your sprite should shift one pixel to the left. Keep hitting A and watch your sprite slowly move off the screen! Add code to handle moving right, up and down. Remember, we ll use: D to move right S to move down W to move up Run it and experiment. Notice that it is possible to move the sprite completely off the screen (it will disappear). Is it completely gone? Move it all the way off the screen and see if you can move it back. You should find that the sprite is still there, it has just moved off the visible portion of the GUI. Our next step will be how to manage the GUI screen bounds the edges of the screen. We ll learn how to keep the sprite completely on the GUI not let it go past the edge at all as if it hits a wall at the edge. We ll also learn how to make the sprite wrap around to the other side of the screen go off the right side and reappear on the left for instance. Page 14 of 14