Top Banner
Coding Tips Andrew Haydn Grant Technical Director MIT Game Lab September 22, 2014 1
65

Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

May 28, 2018

Download

Documents

dangnguyet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Coding Tips

Andrew Haydn Grant Technical Director MIT Game Lab September 22, 2014

1

Page 2: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Iterate Everything

Experiment, analyze, repeat ● Paper prototypes ● Digital prototypes ● Team communication ● Baseball pitches ● Scientific Theories ● Romantic relationships ● Lasagna recipes ● Coding style

2

Page 3: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Quick Review

3

Page 4: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

All Software Sucks

But we still use it.

4

Page 5: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

All Software Sucks

Including yours.

5

Page 6: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

NOT Writing Code

● Coding Is Slow o think o implement o debug o integrate o debug o debug o debug

6

Page 7: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

● Coding Is Slow o think o implement o debug o integrate o debug o debug o debug

● Debugging Is Slower

NOT Debugging

7

Page 8: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

How To Debug Code

● Figure out the problem ● Change the code

8

Page 9: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Playtest Your Code

“I know that man page is clear! I wrote it.” ● Your gameplay is harder than you think it is. ● Your puzzles are harder than you think that are. ● Your instructions aren’t as clear as you think they are. ● Your documentation isn’t as clear as you think it is. ● Your code is not as comprehensible as you think it is.

It’s harder to read code than to write it.

9

Page 10: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Make It Easier

● Write your code to require as little knowledge as possible. ○ of the project ○ of the function ○ of the computer language ○ of the subject matter

10

Page 11: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Psychology

Miller’s Law: The number of objects an average human can hold in

working memory is 7 ± 2.

11

Page 12: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Psychology

Decision Fatigue: The deteriorating quality of decisions made by an

individual, after a long session of decision making

12

Page 13: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Simplicity

● Simplicity means easier to read. ● Simplicity means fewer bugs. ● Simple rarely means “fewer characters” ● Each step is simple. When there are too many steps to

hold in your head, clump them. ● Some language functionality is awesome, but still

dangerous. ○ In particular, be wary of “write once read never”

code.

13

Page 14: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Write Once, Read Never

14

Page 15: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Comments

● Write comments to explain WHY we are doing something.

● Write comments to explain WHAT we are doing only

when the briefest glance at the code is not enough. ● Name variables and functions so that they remove the

need for comments. ● If an algorithm is long enough that it's easy to lose track

of the steps, write an overview of the algorithm in plain language.

● When in doubt, add a comment. 15

Page 16: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Comments

// Is the enemy off the screen?

if (x < 0 || x >= width)

{

// Yes!

return null;

}

16

Page 17: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Code

if ((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1) < 900)

return 50;

17

Page 18: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Code

if ((x0-x1)*(x0-x1)+(y0-1)*(y0-y1) < 30*30)

return 50;

18

Page 19: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Code

if ((v0-v1).getLength() < 30)

return 50;

19

Page 20: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Code

// Did the player get a bulls eye?

if ((v0-v1).getLength() < 30)

{

// Yes! Return the bulls-eye score.

return 50;

}

20

Page 21: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Code

Vector2 offset_from_center = arrow_location - center_of_target;

bool is_bulls_eye = offset_from_center.getLength() < bulls_eye_radius;

if (is_bulls_eye)

{

return bulls_eye_score;

}

21

Page 22: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Shorter Functions

22

Page 23: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Make Wrong Code Look Wrong

float track_length = angle * radius; float expected_race_duration = track_length / speed;

23

Page 24: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Make Wrong Code Look Wrong

float cm_track_length = angle_in_degrees * radius_in_inches; float ms_expected_race_duration = km_track_length / mph_speed;

24

Page 25: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Variables, not Constants

Present options to game designer A constant you can tweak is only useful IF the tweaker can figure out which constant does what UNITY tips- Expose constants in the editor so people can play with them But then make them private if you find a global value that works

25

Page 26: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Variable Names

● Should be longer than you think ● Should be pronouncable ● Should look different: ClientRecs ClientReps ● Should be spelled correctly ● Should include the units of measurement ● Should not be reused

26

Page 27: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Function Names

● Should be longer than you think ● Should be pronouncable ● Should look different ● Should be spelled correctly ● Should include the units of measurement ● Are the primary way you teach other programmers

about your API

27

Page 28: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Variable Scope & Names

void FeedThePigs(int num_truffles) { this.numHungryPigs -= num_truffles; float total_cost = num_truffles * gCostInDollarsPerTruffle; total_cost += CalculateOverhead(total_cost); gCash -= total_cost; }

28

Page 29: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Parallel Arrays

string[] PlayerNames; int[] PlayerCash; Vector2[] PlayerLocation; AVOID. Player[] Players;

29

Page 30: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Order Of Operations

float y = 35 * x + (int) --fever / 4.0f + x ^ 2 % snarkle++; I use parenthesis.

30

Page 31: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Warnings Are Errors

● Warnings are often useful clues about a flaw in your

code. ● When you let warnings stick around, you won’t notice

the new one that actually matters.

31

Page 32: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Backwards Conditionals

if (player_spaceship == null) {} if (null == player_spaceship) {}

32

Page 33: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Backwards Conditionals

if (player_spaceship == null) {} if (null == player_spaceship) {} // Because if (player_spaceship = null) {} // valid code (in some languages) if (null = player_spaceship) {} // NOT valid code //Similarly, if (3 = num_players) {} if (“ferdinand” = player_name) {}

33

Page 34: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Split Up The Math

float foo = (x^2 - sqrt( visibility_threshhold - invisibility_factor / ECM_tech )); vs float adjusted_invisibility = invisibility_factor / ECM_tech; float visibility = visibility_threshhold - adjusted_invisibility; float foo = x^2 - sqrt(visibility);

34

Page 35: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Split Up The Math

x = foo + bar--; vs x = foo + bar; bar--;

35

Page 36: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Booleans

A boolean name should ask a question. That question should be unambiguously answered by TRUE or FALSE. Booleans IsHungry HasFood WasEaten Done (yay) Status (boo) IsSquare >> Square 36

Page 37: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Mysterious Constants

Never use string or numeric constants when you can use a variable // Bad if (status == “closed”) OR if (status == 5) // Good if (status == Status.Closed)

37

Page 38: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

foo ? bar : baz

38

Page 39: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

goto

● goto isn’t ALWAYS evil.

○ Just most of the time. ● Sometimes, it can make code MORE readable.

○ A convoluted pile of nested IF statements can be really hard to unravel.

39

Page 40: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Proximity

Keep related actions together: ● Allows reader to mentally clump code. ● Reduces frequency of stealth code ● Declare a variable right before you use it, not 30 lines earlier. Prepare(x); Prepare(y); Calculate(x); Calculate(y); Print(x); Print(y); 40

Page 41: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Proximity

Keep related actions together: ● Allows reader to mentally clump code. ● Reduces frequency of stealth code ● Declare a variable right before you use it, not 30 lines earlier. Prepare(x); Calculate(x); Print(x); Prepare(y); Calculate(y); Print(y);

41

Page 42: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

KISS

Keep It Simple, Stupid. Keep It Stupidly Simple. ● Do it the easy way first ● Then do it the cool/elegant/efficient way WHEN THE EASY WAY FAILS.

○ And run it by someone else on your team first.

42

Page 43: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Systems

Resist the temptation to build a system on day 1.

43

Page 44: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Recursion

● Recursion is really fun ● Recursion is really hard to debug

○ Never recurse when iterating will work just as well. ○ Never have two functions recursively calling each other.

■ It might be “elegant,” but you will tear your hair out getting it to work.

■ And you will forever be terrified of changing the code (rightly so)

44

Page 45: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Optimizing

Wait. Only optimize your code if it is actually, observably slow.

45

Page 46: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Address Bugs ASAP

● Fix bugs before writing new code

○ It will only get harder to fix them ○ You might build on top of the bugs ○ You cannot estimate bug fixes ○ You are always ready to ship

● Treat warnings as errors

46

Page 47: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Won’t Fix

Fixing bugs is only important when the value of having the bug fixed exceeds the cost of the fixing it. -Joel Spolsky

47

Page 48: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Debugging

● Use a debugger

○ Learn it. Love it. ● Talk to a team mate.

○ They probably will have no idea what you are talking about. ■ That doesn’t matter.

● Take a walk. ● Binary search

○ Perform a test that will cut out as many possibilities as you can. ● If the bug magically vanishes, you are not done.

○ But now you have a clue.

48

Page 49: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Source Control

● Check EVERYTHING into source control

○ Especially 3rd party libs ● Anyone should be able to do a get and build

○ The build should be one human step. ● Check in often

49

Page 50: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Daily Builds

● The checked-in build must never remain broken.

○ Fix it immediately. ● Do Check Builds

○ Have a second, clean checkout of the code on your machine

○ When you check in, immediately check out and build there!

● Daily builds force a code test ● Daily deliverables force a project status

check 50

Page 51: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Coding Standards

Why? ● We like tidy code ● It is an objective truth that opening braces belong on the same line as the

code

51

Page 52: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Coding Standards

Why? ● We like tidy code ● It is an objective truth that opening braces belong on the same line as the

code ● Uniform code can reduce the sense of “MY code” ● Rules reduce decision fatigue ● Encourages code that is easier to read ● Encourages code that is easier to debug

52

Page 53: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Sources

Code Complete by Steve McConnell

Joel is easy to read in blog-sized chunks. He talks as much about management and startups as about code. His writing style is fun and engaging. Steve has gigantic books that look really daunting. I’ve never read one from cover to cover. However, that’s because he has a lot to say and he says it well.

http://www.joelonsoftware.com/ http://www.stevemcconnell.com/books.htm

53

Page 54: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Coding Tips

Andrew Haydn Grant Technical Director MIT Game Lab October 2, 2013

54

Page 55: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Team Coding

55

Page 56: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Pair Programming

● Two programmers, one computer

○ The driver writes code ○ The observer or navigator reviews each line of code in real time.

○ Switch roles frequently! ● The driver considers tactical issues, focusing on the current task. ● The navigator considers larger issues, acting as a safety net.

56

Page 57: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

???

Art Placeholder art! Awesome. Timeboxing Photoshop pngs: save for web!! Sprite strips Audio Can kill your download size. Compress! Mono! mp3 is a pain, but ogg isn’t supported everywhere

57

Page 58: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

???

Slide from BBN presentation- these things are hard (in the slow sense)

Synchronizing music to user inputs

58

Page 59: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Late Changes

Trespasser Sorting

59

Page 60: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

The Iceberg Secret

● Very little of your software is visible to the player ● People judge software by how it looks ● So make sure that it looks about as done as it is

ed;hjsdf;kj

60

Page 61: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Beware Algorithms

Especially ones you create yourself. Wait, what? Cool ways of solving problems are one of the reasons that we all learned to

program. An Algorithm, in the textbook sense, is a particularly clever way of solving a problem. We want to make some of our own.

Okay, but only make one when you need to. This goes back to performance. Most algorithms are about solving some

problem QUICKLY. The first step you should take is solving the problem in the simplest, most readable, least bug-prone way possible.

Then run it. Computers are fast. This may be good enough. After the simple thing fails, then think about optimizations. After you’ve done

the easy ones, then consider creating an algorithm. No, wait, don’t! Look online first. It’s be faster and less bug prone to benefit from some poor shmuck who has solved (and debugged) this problem before.

What is an algorithm? Okay, there are simple algorithms that only take a few lines of text to explain. We’re not talking about those. We’re talking about something A* or harder, where the code reader has to THINK about why it works.

61

Page 62: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

Intelligent Design

Evolved code vs designed code. It’s stunning, really, how much the code of a long-running prject resembles DNA. There are huge swathes of it that are useless, or appear to be. But when you take them out, the program stops working. Rewriting is dangerous, but only when you are rewriting old, evolved code.

62

Page 63: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

???

● Keep functions short.

● Declare your variables as close as possible to the place where you will use them.

● Don’t use macros to create your own personal programming language.

● Don’t use goto.

● Don’t put closing braces more than one screen away from the matching opening brace.

63

Page 64: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

???

But also resist the temptation to cut&paste code everywhere. It’s already a

good idea to split your code up into functions just for readablilty. It’s also a good idea to put common code in functions to save debugging time. So make it a function call instead of a cut&paste, but wait for AT LEAST the

second instance of the code. I like to wait for the third instance, but at that point I’m risking forgetting one of the first two.

64

Page 65: Class on Creating Video Games, Lecture 6, Coding Tips · Beware Algorithms . Especially ones you create yourself. Wait, what? ... Class on Creating Video Games, Lecture 6, Coding

MIT OpenCourseWarehttp://ocw.mit.edu CMS.611J / 6.073 Creating Video GamesFall 2014 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.