Sie sind auf Seite 1von 18

Object

A thing

Variable

Used to save info

variable
Im an object!

self
Saving the result of a method

We are objects!
Im an object!

Method

Attached to an object
Does something

Argument

Goes in a method to
effect what it does

arguments

Object

Method

Attached by a
period

if /elif / else

Make the code work


Only when ___ is true

Boolean

True or
False

First it tries
No
Then it tries
No
Otherwise

Operators

Allow for better


If statements

String
This is a String
So is this

Represent
Text

in If x < 10 / else

In if enemy

In the Loop

Nested Loops

The Grid
Find the coin

To find my position use:


self.pos.x and self.pos.y

Find coins x and y


move to x, y

To pick me up use:
coin = self.findNearestItem()
x = coin.pos.x
y = coin.pos.y
self.moveXY(x, y)

This will move you right forever.


X

Loop:

x = self.pos.x + 10
y = self.pos.y
self.moveXY(x, y)

Comparing Coordinates
To find my position use:
enemy.pos.x and enemy.pos.y

#Check to see if an enemy is above you

#Build a fence 5m above an enemy

Her y position is 4
My y position is 1
She is above me

#Attack an enemy if it is between 10 and 20

Counting with While


Make a variable
Loop
Add 1 every loop
Say the number
Stop the loop at 5

1
2
3

4
5

Make a variable
Loop until 5
Add 1 every loop
Say the number

Counting with Lists


Numbers[0]

A list or array starts counting at 0


The first item on the list is:
List[0]

Numbers[4]

Make a list
Set an Index Variable
Loop once for every item
Say the number

0
1
2

Add 1 every loop

The first time this is:


numbers[0]
The second time it is:
numbers[1]

Make a list
Loop through each item in the list
Say the item

Picking Up 5 Flags
Set variable to 0
loop
Find and check flag

Get flag
Increment (add 1) to variable
Check variable value
Stop the loop
Say variable (5)

Checking by Type

Lists and Arrays

apple
orange
banana

Coordinate System
The enemy is at:
2,3

X: 4
Y: 6

X: 2
Y: 3
X: 6
Y: 2

The item is at:


4,6

I am at:
6,2

Sorting and Filtering

# makes a list of enemies without yaks

list.append(thing)
adds the thing to the list

# sorts friends by type

# shortcut filtering

If you want to know what I can do use:

Types
You will say my methods and properties

soldier

archer

thrower

witch

peasant

scout

brawler

librarian

paladin

ogre

warlock

ninja

headhunter

captain

shaman

griffin-rider

peon

fangrider

sand-yak

yeti

knight

Find Best Target


Find Closest (for)

Find Closest (while)

Comparing distance and value

Vectors
A describes a line segment starting from (0,0)
and going to (x,y).
Characters positions are expressed as vectors.

Addition
You can add 2 vectors together. This is
useful for moving your character,
among other things

Multiplication
You can multiply a vector by a number
to change its magnitude. Multiplying
by a negative number flips it to the
other side of (0,0)

Subtraction
Subtraction often used to find the
distance between 2 objects. Note that
you have to multiply the vector by -1 to
get it going in the correct direction. It is
often easier to remember just to
subtract from end to start.

Vectors

Finding Vectors
# longhand

Generally vectors are used to represent either a


point or a direction and magnitude (distance).
To make visualization easier, a vector
representing direction will be shown coming
from an object, rather than from origin.
# quick version

Moving with Vectors


Vector(2,3)

Always subtract end from start


Vector.subtract( end , start )
Vector(1,1)

How to Run Away

Normalize
Basically, it takes a triangle and makes another
triangle of the same shape, but with a
hypotenuse of 1
normalized

Always subtract end from start


Vector.subtract(end, start)

Multiply

x5

Going Around
Find the vector representing direction to your destination and
normalize it
If the danger is too close:
Find the vector from the danger to you and normalize it
Add that vector to the direction to your destination
Add the new vector to your position to create a position

Move to the position

moveTo
safeMove
meToCoin
yakToMe