Homework: New coin toss. [Start dice game.]

Slides:



Advertisements
Similar presentations
Introduction to Web Design Lecture number:. Todays Aim: Introduction to Web-designing and how its done. Modelling websites in HTML.
Advertisements

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Introducing JavaScript
Cascading Style Sheets
Chapter 3 – Designing your web pages Dr. Stephanos Mavromoustakos.
Web Pages and Style Sheets Bert Wachsmuth. HTML versus XHTML XHTML is a stricter version of HTML: HTML + stricter rules = XHTML. XHTML Rule violations:
The Binary Numbering Systems
Programming games Classwork: Show Favorite Sites. Show coin toss. Review concepts. Crooked coin toss. Homework: Complete coin toss examples. Upload files.
Programming games More drawing. Text. Radian measure. Faces. Homework: Do your own drawings. Create index.html file. Upload work.
Programming games Reprise on dice game and alternative dice game Homework: [Catch up.]. Finish dice game.
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Chapter 3 Data Representation.
Chapter 3 Data Representation. Chapter goals Describe numbering systems and their use in data representation Compare and contrast various data representation.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
Information Technology Center Hany Abdelwahab Computer Specialist.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
Topic 4 Computer Mathematics and Logic
Connecting with Computer Science 2 Objectives Learn why numbering systems are important to understand Refresh your knowledge of powers of numbers Learn.
Programming Games Computer science big ideas. Computer Science jargon. Show virtual dog Homework: [Catch up: dice game, credit card or other form.] Plan.
 Using Microsoft Expression Web you can: › Create Web pages and Web sites › Set what you site will look like as you design it › Add text, images, multimedia.
Create Interfaces College exercise. Continue HTML5: HTML5 logo, drawing images. Coin toss. Homework: acquire video & audio.
Computers Organization & Assembly Language
Programming games Recap. Upload work (Filezilla, index.html). Drawing lines. Drawing stars. Homework: Drawing exercises.
Homework: Finish dice game.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Programming Games Formulas. Date. Representation in [computer] Storage. Credit Card. Homework: Finish slide show. Upload application.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Representing Data. Representing data u The basic unit of memory is the bit  A transistor that can hold either high or low voltage  Conceptually, a tiny.
Programming games Reprise: coin toss on canvas. Dice game rules. Global and local variables. Homework: [Catch up. Upload projects, including index.html.]
1 JavaScript in Context. Server-Side Programming.
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
HTML Basic IST2101. Keep In Mind Programming can be time consuming. Plan ahead!
Tutorial 8 Programming with ActionScript 3.0. XP Objectives Review the basics of ActionScript programming Compare ActionScript 2.0 and ActionScript 3.0.
Programming Games Show project. Refresher on coordinates. Scaling, translation. HTML5 logo. Refresher on animation. Bouncing ball. Homework: Do your own.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Programming Games Reprise: storage, datatypes. Geolocation/Google API example. Work session Homework: [Catch up. Upload work. Post proposal.] Work on your.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
Game Maker Terminology
Programming games Show shaking screen, quiz show. Calculations Homework: (Finish basic Javascript Projects) Make proposal. Work on project.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Programming games HTML/JavaScript basics Functions, events, forms Classwork: [Show favorite sites.] Coin toss. Homework: GET WEB SPACE. Complete coin toss.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
XP Tutorial 8 Adding Interactivity with ActionScript.
Data Representation The storage of Text Numbers Graphics.
Programming Games Credit cards. Forms. Homework: Catch up: dice game. Produce your own credit card application or something else making use of a form with.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Programming games Context of what we are doing. Drawing on canvas. Homework: [Complete coin toss examples.] Do your own drawings. Upload files to website.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Programming Games Logic. Slide show. Range input. Storage. Datatypes. Binary numbers. Homework: Catch up. This includes uploading projects to your server.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Computer Science I Storing data. Binary numbers. Classwork/homework: Catch up. Do analysis of image types.
Programming games Review concepts. Crooked coin toss. Drawing on canvas. Homework: Complete [static] drawings. Upload files to website.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
Data Representation. How is data stored on a computer? Registers, main memory, etc. consists of grids of transistors Transistors are in one of two states,
Programming Games Show slide show (on your site). Bouncing something. Video element. Bouncing video element. Bouncing video drawn on canvas. Bouncing video.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Programming games Show work on site. Work on slide show. Timed event for bouncing ball. Homework: [Finish slide show and upload to site.] Acquire a short.
Data Representation Binary Numbers Binary Addition
Programming Games Computer science big ideas and Computer Science jargon: review of things we have used in examples. Show virtual dog Homework: [Catch.
ICT Gaming Lesson 3.
Context of what we are doing.
Programming games Share your plans for your virtual something.
Web Programming and Design
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Programming games Reprise on dice game and alternative dice game
Presentation transcript:

Homework: New coin toss. [Start dice game.] Programming games Drawing! Debugging. New coin toss. Dice game rules. Storage (binary numbers) Homework: New coin toss. [Start dice game.]

Drawing EMAILed. Do exercises. Re-create and then modify. Recap: rectangles paths: arcs and lines Today: images and text

Debugging Make sure you are testing latest version you can make a small visible change Insert alert statements alert("at start of toss function"); alert("xxx is "+xxx); NOTE: if a function is not well-formed (for example, bracket problem), the browser does not display a message—it just doesn't work! Firefox: Tools/Error console Chrome: wench symbol/Tools/JavaScript Console 3

Debugging In TextWrangler (PC: TextPad) use Find command Check if dthrow defined AND called Check on consistent spelling of names Can use for brackets, closing </a>, etc. though you may need to print out and use pencil. Also use opening and closing…. 4

JavaScript if & if/else if ( logicalexpression ) { statements } if ( logicalexpression ) { else { 5

Switch statement switch (expression) { case value1: statements default: statements } If you do NOT want execution to continue (flow into) the next case, you need to insert a break statement. optional 6

Notation { and } are used to group statements together function definition, if and else clauses, and other statements we haven't done yet: switch, for, do while. ( and ) are used condition part of an if statement if (Math.random()>.5) set order of operations in a calculation total = total + (total*taxrate); specify arguments in a function definition and parameters in a function call Math.random()

Goal: new coin toss Replace the player move of clicking a button by clicking directly on the canvas. Place an image on the canvas at the place clicked. Put directions on the canvas. http://faculty.purchase.edu/jeanine.meyer/html5workshop/wkshopcoinflip.html

Remember: the context object ctx = document.getElementById('canvas'),getContext('2d'); You can have multiple canvas elements and one of these for each one. This line must be executed after body element is read in (loaded).

drawing image from file HTML supports Image objects using code. Find and download new images or re-use what you used before for the coin toss: var head = new Image(); head.src = "head.gif"; … drawImage(head, 10,20,100,100); draws this image at 10,20, with width of 100 and height of 100.

drawing text You can set color, that is the style Just to show one more way to specify a color (previous ways are the set of names and using the rgb function): you can specify an red-green-blue value using hexadecimal (base 16) values. values go from 00 to FF ctx.fillStyle = "#dd00ff";

drawing text, cont. You specify the font: ctx.font = "bold 10px Arial"; Open up Word on your computer and see what fonts are available. ctx.fillText("Hello", 100,200); Add images and text to one of your drawings. Experiment.

Mouse click on a canvas Need to set up the event on/for the canvas object canvas1=document.getElementById('canvas'); creates an object that JavaScript can use. canvas1.addEventListener('click',flip,false); sets up JavaScript to listen for the click event and when it occurs, invoke the function named flip.

Mouse cursor position One more thing: we want the coin head or tail to be placed on the canvas where the click is made. Challenge: the browsers handle this differently. The following code works for Firefox, Chrome, Safari: if ( ev.layerX || ev.layerX == 0) { mx= ev.layerX; my = ev.layerY;} else if (ev.offsetX || ev.offsetX == 0) { mx = ev.offsetX; my = ev.offsetY; }

Adjustment Images are drawn starting at the upper left corner. We want the image to be centered at the position where the player clicked on the screen. Assuming the mouse coordinates are mx,my, and the image 100 x 100, then we place the image at mx-50, my-50.

Summary <html><head><title> </title> <script> variables init function definition, including addEventListener to set up call to flip flip function definition </head> <body onload="init();"> canvas element </body>

<html> <head> <title>Coin flip</title> <script> var ctx; var canvas1; var head = new Image(); var tail = new Image(); head.src = "head.gif"; tail.src = "tail.gif"; var coinwidth = 100; var coinheight = 100;

function init() { ctx = document.getElementById('canvas').getContext('2d'); canvas1 = document.getElementById('canvas'); canvas1.addEventListener('click',flip,false); ctx.font = "italic 20px Accent"; ctx.fillStyle = "#dd00ff"; ctx.strokeRect(0,0,600,400); ctx.fillText("Click on canvas to flip a coin.",10,20); }

function flip(ev) { var mx; var my; ctx.clearRect(0,0,600,400); if ( ev.layerX || ev.layerX == 0) { mx= ev.layerX; my = ev.layerY; } else if (ev.offsetX || ev.offsetX == 0) { mx = ev.offsetX; my = ev.offsetY; } if (Math.random()>.5) { ctx.drawImage(head,mx-50,my-50,coinwidth,coinheight); } else { ctx.drawImage(tail,mx-50,my-50,coinwidth,coinheight);} ctx.strokeRect(0,0,600,400); ctx.fillText("Click on canvas to flip a coin.",10,20); }

</script> </head> <body onLoad="init();"> <canvas id="canvas" width="600" height="400"> Your browser doesn't support the HTML5 element canvas. </canvas> </body> </html>

Your task Get this working. Change colors, fonts, positions. Add scoring keeping! You will need to add var headcount = 0; var tailcount = 0; Add code to increase each of these in the right places. headcount++; or tailcount++ Add code to write out: ctx.fillText("Heads: "+String(headcount)+ " and tails: " + String(tailcount),10,100);

Dice game … aka craps. Rules: player rolls 2 dice. This is a first throw. In all cases, the value is the sum of the faces. On a first throw, 7 or 11 win and 2, 3, 12 lose. If it is neither of those (4, 5, 6, 8, 9, 10), then the status shifts to follow-up throw and the value just rolled is 'the point'. On follow throw, the point wins and 7 loses. If neither happen, play continues as followup throws. The game can keep going!

Dice game logic In pseudo-code / English If it is a first throw, then we use these rules, that is, work with these cases If it isn't (not a first throw, namely a followup throw), then we use these other rules, consider these cases. 23

Programming the Logic (game rules) for craps Your code maintains a global variable that is true if it is a first throw and false otherwise. So it starts out as true. It is global, meaning the var statement is outside of any function, so the value stays available. There is code that checks this value using an if statement and then applies the appropriate set of rules.

switch statement: mixture code and pseudo-code switch(sum) { case 7: case 11: show a win break: case 2: case 3: case 12: show a loss break: default: … set up to be follow-up throw }

Outline of logic: DO THIS if (condition) { switch () { statements } else { Do outline the logic and then fill in the details. 26

General programming concepts Places to hold data Variables: Issue of scope and access Global variables Local variables Properties of objects (also have issues of scope and access) Visible element on screen 27

JavaScript Global variables: defined outside of any one function, within the script tag. Can be accessed and modified by code within functions. <script> var score = 0; Local variables: within a function. Only lasts during invocation of function. Only accessible within function. 28

Overview Representation of information. How is everything/anything represented 'in' the computer (in storage)? ANSWER: everything is represented using 1s and 0s. What the 1s and 0s mean depends on what the information is, for example, the datatype whole number, number with fraction, true/false value, character string, other… Expressions and operators

Storage Everything (data and programs) is stored in the circuitry of 'the computer'. The circuitry includes transistors that are switches: on or off states, 0 or 1. Each switch is called a bit. So….numbers are stored using the binary (base 2) system Symbols (characters, letters, etc.) are stored using agreed upon systems of encoding ASCII: 8 bits per character UNICODE: 16 bits per character

Why? Why not use circuits that can more easily represent numbers using the decimal (base 10) system? Answer: Easier to make on/off switches than something with 10 states. Easier to build circuitry for calculations for the base 2 addition and base 2 times tables than the ones you remember…

Recall base 10 Recall 1s column, 10s column, 100s column Recall borrowing (re-grouping) and carrying Do problem(s)

Base 2 Same principle 1s column, 2s column, 4s column, ???? Do problem(s)

Joke Explain joke on shirt

Base 16 Hexadecimal: used for quick way to describe bits, mostly commonly for color coding Symbols used are 0, 1, 2, …, 9, A, B, C, D, E, F You have seen color coding: RGB (red, green blue) in hexadecimal FF0000 is red 00FF00 is green ??

Numbers with fraction part Aka numbers with a decimal point How to represent? ANSWER: floating point numbers aka scientific notation 3.4521 * 102 is the same as 345.21 * 100 Terminology: 3.4521 (or 345.21) is the mantissa or significand and the 2 (or 0) is the exponent. Computer format: use 2 or 16 in place of 10 Example using 32 bits: 1 bit for the sign (0 for +, 1 for -) 8 bits for the exponent 23 bits for the mantissa (width, i.e., 23, is the precision)

Characters ASCII coding The character A is represented by 01000001 The character a is represented by 01100001 The character 1 is represented by 00110001 The character 2 is represented by 00110010 …. Unicode is a 16 bit format big enough (hopefully) for all the world's languages

String of characters …such as a name or a label or a piece of text Fixed length: allocate 1 byte (8 bits) for each character UNICODE 2 bytes Variable length: store string in two parts One part holds length as a number and pointer (address) of the whole string String itself

Boolean Named after George Boole True / False Theoretically: 1 bit, 0 for false, 1 for true but The addressability requirement means it could be 1 byte or even bigger String of Booleans can be combined. A byte can represent the answer to 8 true/false questions.

Homework Catch up: favorite sites, first coin toss, crooked coin toss, static drawings, coin toss on canvas Work on dice game. Review charts Look up terms on-line and/or in my book.