Java for Video Games Review Introduction I. Definitions Compiler o The program that translates code in a high level language (such as java) into machine instructions (such as a bytecode for the Java virtual machine). Integrated Development Environment o The compiler in which you can write and test your programs (i.e. Eclipse). Source Code o Instructions in a programming language that need for be translated before execution on a computer. Compile Source Code o Translate the source code from java language into machine instructions. CPU o Central Processing Unit – the part of the computer that executes the machine instructions. Virtual Machine o A program that simulates a CPU that can be implemented efficiently on a variety of actual machines. o A given program in Java bytecode can be executed by any Java virtual machine, regardless of which CPU is used to run the virtual machine itself. Bytecode o Instructions for the Java virtual machine. Execute Byte Code o Execute the instructions for the Java virtual machine. Modify Source Code o Web browser o Software that interprets the markup of files in HTML, formats them into Web pages, and displays them to the user. Questions What is your webpage address? o www.duke.edu/~yiqingke/cps4 II. What directory is your webpage located in? o P:\public_html\cps4\index.html What file gets loaded when a web page contains a directory name? o Index.html What is a link? o A hyperlink is a navigation tool you can build into your page which enables your visitor to click on a word, phrase, or image, and be taken to the url you put into the link. What is an absolute link? An absolute link is a hyperlink that includes the complete URL, which is the: Domain name, folder or subdirectory name File name. o For example: http://www.pro-seo.com/tutorials/optimizationbasics.html What is a relative link? o o o A link that is defined by its relative position to the current URL. A relative link is a hyperlink that does not include an entire domain name, folder or subdirectory name and file name together in the URL. For example: optimizationbasics.html o Anatomy of a Class Data Types and Operations Object o An entity that you can manipulate in your program. You manipulate an object by calling one or more of its methods. “System.out” is an object. o You manipulate it by calling the “println” method. Primitive Type: o o A type whose values cannot be decomposed into simpler values. The fundamental, built-in types within the Java programming language, whose values have direct machine representations o Single value No methods In Java, the Number types are the Primitive types. They are not classes. Numbers are not objects. The number types have no methods. Examples Double – real numbers Int. Boolean o Reference Type (Object): o A type that refers to a specific location in memory, as opposed to value types. A type whose value is a reference. Multiple values Many methods Type = The type tells you what you can do with the values. Values = o Every value as a type. “Hello, World” has type String “13” has the type Int. Variable = o o o You often want to store values so that you can us them at a later time. To remember an object (value), you need to hold it in a variable. A variable is a storage location in a computer S memory that has a type, a name and contents. Integers o Whole numbers (13) Floating-Point Numbers o Numbers that can have fractional parts (1.3) Double – real numbers o Double-precision floating point numbers. o If you need to process numbers with a fractional part, you should use the type called double. o You can think of any number in a double format as a number that can appea in the display panel of a calculator. Boolean (Type) o A type with two possible values: True and False. Char o A single letter, number, space or punctuation. String o A sequence of characters. o A reference object in java.lang. Describe the naming conventions for Primitive Types All lowercase If we were going to create new types ourselves, they would be all in lowercase. (int, float) However, since we don’t typically create new types, use camel case. First letter lowercase, each successive word uppercase. Describe the naming conventions for Reference Types (class names) o Start with Uppercase. Each successive word starts with Uppercase. o For the following operators, describe what they do and given an example of their use in an expression and include the results of the expressions: o + Adds o o + 5+3 = 2 “Hello + World” = Hello + World / (integers) Divides 10/3 = 3 / (floating points such as doubles) o Retains the fractional part of the division o 10/3 = 3.3333333 * o o 5 * 3 = 15 Multiplies % o o 5%2=1 Finds the remainder after division. = o == o o Equivalence x == 3, 5+x == 8 Assignment dollar Explicit Parameter vs. Implicit Parameter o System.out.println(greeting) Greeting Explicit parameter System.out. Implicit parameter Static Method o A method with no implicit parameter. Video Game Package What is a Sprite? o A visual element on the screen. o Not all sprites move, so not all sprites need a tracker. Four walls, lines, ball all sprites Has instance variables, and mututator and accessor methods for: Shape Location Size Rotation Color What is a Tracker? o The tracker is what makes the BallSprite move (1) Tells what current location the Sprite is Point2D.Double getLocation() o Can give a scaling factor How much larger or smaller the ball should be since the last time it was displayed. 1 no change 1.1 Sprite should be 10% larger 0.9 Sprite should be 10% smaller o Rotation – tells how much, in radians, the sprite has rotated o Tells how much time which has transpired since the sprite was last displayed. Steps to Making a Game o Make Packages for all the parts of your game. o Organize your program’s resources in the packages. o Write the program’s classes. Packages are like folders. Graphics What does the monitor resolution describe? o The number of pixels a computer monitor is capable of displaying. o The resolution is described in terms of width x height. o 640 x 480 pixels 640: Distance across 480: How high What is a typical monitor refresh rate in Hz (frames per second)? o Monitors typically refresh at 75-85 Hz Cartesian o The components are x and y o X is real valued and indicates the horizontal direction to move (right for positive, left for negative). o Y is real valued and indicates the vertical direction to move (up for positive, down for negative). “Real valued” = can display fractions. Polar o The components are r, theta. r = distance from the origin. Real valued. Theta = angle made with the positive x axis. Increases in a counterclockwise direction. Theta is from 0 to 2П, or 360 degrees. Conversion o [x, y] = [r * cos (theta), r*sin(theta)] Screen (Graphics) o Rectangular o The components are x, y o X increases to the Right o Y is inverted. Y increases as you go DOWN. o Origin is at the upper-left corner of the screen. o X and y are non-negative integer valued. Not real-valued. Can’t have decimals. Graphics 2D o Rectangular o The components are x and y o X increases as you go to the right o Y increases as you go DOWN. o Origin at the upper-left. o Real Valued !!! (can have decimals – but this is approximated since computers are finite). Java2D (Graphics 2D) to Screen Conversion o Round the decimal to an integer. Graphics 2D Screen 6.3, 7.3 6, 7 Screen to Java2D o No conversion needed. When is it a bad idea to represent your graphics using only Screen Coordinates? o Give an example of when using Screen coordinates would cause problems that would not occur using the Graphics 2D coordinates. Integer values often need to be rounded when they are divided, which can lead to more calculation errors. - 5/6 = 0 - 0x6=0 - Loss of precision. Smoother motions in Graphics2D. Simpler to rotate and expand in Graphics 2D. When you have lots of division with integers, it is a bad idea to only use Screen coordinates. Graphics 2D has more precision so that you can fact keep precision when you are manipulating objects. What is Constructive Area Geometry? Give an example of when CAG could be useful. o Used to make a shape Uses Area. o Take a shape, put another shape, take the composite of the two shapes. o Finding intersections.