coding is fun! v3

49
Coding is FUN! School of IT Lawrence Tham 7 Jan 2021 [email protected] © Nanyang Polytechnic

Upload: others

Post on 22-May-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Coding is FUN! v3

Coding is FUN!School of IT

Lawrence Tham7 Jan 2021

[email protected]

© Nanyang Polytechnic

Page 2: Coding is FUN! v3

• What is Programming?• What will i Learn?• What will i Achieve?• Where do i Need?• Let’s Start!

Page 3: Coding is FUN! v3

• How to make tea?• Sequence of Steps• Loops• Conditions

• Programming is creating a set of instructions given to a computer to perform a task

Page 4: Coding is FUN! v3

• Your very own Swat the Fly game

Page 5: Coding is FUN! v3

• Programming with JavaScript and HTML

• HTML helps display things on browsers

• JavaScript enables interactive web pages

• Tryit Editor v3.6 (w3schools.com) • a free tool that allows you to try JavaScripts easily

• https://www.w3schools.com/js/tryit.asp?filename=tryjs_editor

Page 6: Coding is FUN! v3

• A Web Browser

• Internet Connection

- to use the online Tryit Editor

Page 7: Coding is FUN! v3

• With HTML you can create your own web page

Page 8: Coding is FUN! v3

• Hello World

https://icode.sg/coding-is-fun-2

Page 9: Coding is FUN! v3

• Let’s create spaces for

1. Canvas to draw graphics

2. Button to start game

3. Header for “My first game”Canvas of size 600 by 300 px

Button

Header

Page 10: Coding is FUN! v3

Canvas of size 600 by 300 px

Button

Header

Place holder for JavaScript

Page 11: Coding is FUN! v3

• Canvas

• Buttons

• Header

• Script

Page 12: Coding is FUN! v3

• A Canvas is like a piece of paper

• You will need Instructions to the computer to draw lines, shapes, images and Text using JavaScript

Y

0 600

300

0

X

Place holder for JavaScript

Page 13: Coding is FUN! v3

Y

0 600

300

0• How would you ask

someone to draw a rectangle on this paper?

• Where to start? (x,y)

• What’s the dimension? (width, height)

• Empty rectangle or Filled?

width

height

x

y

.

Page 14: Coding is FUN! v3

• Drawing a Filled Rectangle

Y

0

0

X

x = 50

y = 100

width = 200

height =100

Page 15: Coding is FUN! v3

• Drawing a Empty Rectangle

Y

0

0

X

x = 50

y = 100

width = 200

height =100

Page 16: Coding is FUN! v3

Put instructions to draw 2 rectangles in

script

Page 17: Coding is FUN! v3

• Draw Rectangles

Page 18: Coding is FUN! v3

Y

0

0• How would you ask

someone to draw a circle on this paper?

• Where to start? (x,y)

• What’s the radius?

• Draw Clockwise or Contraclockwise?

• Draw a complete circle?

x

y

.

.

radius

cwccw

Page 19: Coding is FUN! v3
Page 20: Coding is FUN! v3

• Draw Circle

Page 21: Coding is FUN! v3

Y

0 600

300

0

X• How would you ask someone

to write on this paper?

• Where to write? (x,y)

• What to write? (text)

• What is the font and size?x

y

Hello

text

= 200

= 100

Page 22: Coding is FUN! v3
Page 23: Coding is FUN! v3

• Draw Text

Page 24: Coding is FUN! v3

Y

0 600

300

0• What I need for my game?

• Fly

• Swatter

• Time Bar

• Score

• Game Over

Time Bar

Swatter

Fly

ScoreGame Over

Page 25: Coding is FUN! v3

Y

X• What I need to draw

• A Black Circle for Body

• 2 Grey Circles for Wings

x

y

radius 8 px

(x+10, y-10)radius 10 px

(x-10, y-10)radius 10 px

Page 26: Coding is FUN! v3

Y

X• What I need to draw

• 4 Empty Rectangles for Net

• a Grey Filled Rectangle for Handle

Rectangle 1strokeRect(x, y, 20, 20);

Rectange 2strokeRect(x, y-20, 20, 20);

fillRect(x - 5, y + 23, 10, 15);

x

y

Rectangle 3strokeRect(x-20, y-20, 20, 20);

Rectangle 4 strokeRect(x-20, y, 20, 20);

Page 27: Coding is FUN! v3

Y

X• What I need to draw

• A Red Rectangle starting from (0,0) with fixed height and increasing width

(x=0, y=0)

As timer increase, the width of the bar increases and bar lengthens

Fixed height of 10 px

Page 28: Coding is FUN! v3
Page 29: Coding is FUN! v3

• Draw Fly

• Draw Swatter

• Draw Time Bar

• Draw Score

• Draw Game Over!

Page 30: Coding is FUN! v3

• Have you played with a Flip Book before?

Y

0 600

300

0

Page 31: Coding is FUN! v3

Y

0 600

300

0

• We can simulate change of page by repeating 2 steps per Frame:

1. Update:• Change something

2. Render:• Clear• Draw

t = 100t = 330t = 450t = 600

run frame every 50 ms to produce animation on canvas

Page 32: Coding is FUN! v3
Page 33: Coding is FUN! v3

• Animate Life Bar

Page 34: Coding is FUN! v3

Y

0

0

mouse_X

• Track the mouse movement

• mousemove event listener

• Draw swatter with mouse x,yposition

mouse_Y

Page 35: Coding is FUN! v3
Page 36: Coding is FUN! v3

• Add Swatter

Page 37: Coding is FUN! v3

• Firstly, we must be able to generate random rumbers

• Math.random() returns random value between 0 to 1

• max is the maximum number you want

• Math.floor() round value to whole numbers

• If max = 600, you will get a value between and including 0 and 600

• Maximum fly_X is 600 and maximum fly_Y is 300

Page 38: Coding is FUN! v3

flyTimer = 0flyTimer = 1flyTimer = 2 flyTimer = 3

Y

0 600

300

0• Let’s create random fly_X and

fly_Y every 15 frames(60,30)

(120,60)

flyTimer = 14 flyTimer = 15 flyTimer = 0

Page 39: Coding is FUN! v3
Page 40: Coding is FUN! v3

• Adding Random Fly

Page 41: Coding is FUN! v3

Y

0

0

mouse_X

mouse_Y

• Let’s make every mouse click an attempt to hit the fly

• click event listener

Page 42: Coding is FUN! v3

Y

0

0

(fly_X, fly_Y)

(mouse_X, mouse_Y)20 px

• If fly body falls into one of the 4 squares of swatter, it’s a hit!

• Calculate absolute difference between fly_X and mouse_X, and fly_Y and mouse_Y

• If both less than 20px, it’s a hit!

absX = Math.abs(fly_X - mouse_X);absY = Math.abs(fly_Y - mouse_Y);if ((absX <= 20) && (absY <= 20)){

hit = true;}

xx

x

x

x

Every time I click the mouse, I check if I hit the fly

Page 43: Coding is FUN! v3

Y

0

0

• Score increase by 1

• Blood Splatters

• Fly changes position

SCORE: 01

Page 44: Coding is FUN! v3

• Game is over if timer reaches timeout of 600

• When timer reaches timeout, we will display Game over and stop the looping of frame

Page 45: Coding is FUN! v3

• Click to Hit

• Check if swatter hit fly

• If swatter hits fly

• If time out

Page 46: Coding is FUN! v3

• Click to Hit

• Check if swatter hit fly

• If swatter hits fly

• If time out

Page 47: Coding is FUN! v3
Page 48: Coding is FUN! v3

• NYP School of IT

https://www.nyp.edu.sg/schools/sit.html

• Our 5 Diplomas and Common ICT Programme

https://www.nyp.edu.sg/schools/sit/full-time-courses.html

• Our Scholarships

https://www.nyp.edu.sg/schools/sit/scholarships.html

• Live Talk Registration

• Get a Scholarship! (Fri Jan 8, 9:00 PM)https://zoom.us/webinar/register/WN_g8M5QJLpRpiNI-4YFLCNqg

• Become a Successful IT Professional (Sat Jan 9, 4:45 PM) https://zoom.us/webinar/register/WN_MsgJN7KhTqKc4nNi-HzpsA

Page 49: Coding is FUN! v3

Thank You