SlideShare a Scribd company logo
Introduction to Programming
Class 6
Paul Brebner
Exercise
• Create a cannon trajectory game
– Place a cannon at lower left of screen, with a target on lower right.
– Set initial cannon angle (0-90 degrees, 0 is horizontal, 90 is vertical), and
velocity, and cannon ball location (x and y, same as cannon).
– Calculate velocity of the cannon ball, vx and vy, initially:
• float vx = velocity * cos(radians(angle));
• float vy = velocity * sin(radians(angle));
– Each frame, change x and y position (and draw ball) and vy as follows:
• x += vx; // add vx to get next cannon ball x position
• y -= vy; // subtract vy to get next cannon ball y position
• vy -= g; // g is gravity acting down, try 1
– Remember that in processing x increases from 0 to width, and y from 0 to
height (starting at top left, so increases as ball goes down!)
– Use frameRate(1) (or slower) to make debugging easier
– How will you change the angle and velocity? How will you fire? How will you
check if the ball hits the target? What happens when it hits?
– Can you put another cannon on the right and make the game 2 player?
– Can you work out how load images for the cannon, ball, target, explosion etc?
– Next page is a simple online example
Try this example
http://phet.colorado.edu/sims/projectile-motion/projectile-motion_en.html
Example program structure, add detail to TODOs
// global variables for cannon and cannon ball position and velocity go here ...
TODO
boolean cannonFired = false; // true after cannon fired until ball lands
void setup() // set size, background (sky colour), cannon position, and frame rate
{
TODO
}
void draw() // everything done once per frame
{
// draw cannon
TODO
if (cannonFired)
{
// move and draw the ball until it hits the ground again, then set cannonFired to false
TODO
}
else
{
// change angle and velocity and firing of cannon, set cannonFired to true and cannon ball x and y to cannon
location
TODO
}
}

More Related Content

PPTX
Day 36 Ppt Batfink Angle Launched Projectile Motion
PPT
William
PDF
Introduction to programming - class 2
PDF
Introduction to programming class 12
PPT
Radacina alcatuire externa final
PDF
Introduction to programming - class 5
PDF
Past Experiences and Future Challenges using Automatic Performance Modelling ...
Day 36 Ppt Batfink Angle Launched Projectile Motion
William
Introduction to programming - class 2
Introduction to programming class 12
Radacina alcatuire externa final
Introduction to programming - class 5
Past Experiences and Future Challenges using Automatic Performance Modelling ...

Viewers also liked (12)

PDF
Introduction to programming - exercises 1
PDF
Introduction to programming class 13
PDF
Introduction to programming - class 10
PDF
Introduction to programming - class 11
PDF
Introduction to programming - exercises 2
PDF
Introduction to programming - class 8
PDF
Introduction to programming class 11 exercises
DOC
Sanjay New RESUME
PDF
Automatic Performance Modelling from Application Performance Management (APM)...
PPT
Radacina alcatuire externa final
PDF
Introduction to programming - class 1
PPT
Prezentare ppt - Sistemul respirator
Introduction to programming - exercises 1
Introduction to programming class 13
Introduction to programming - class 10
Introduction to programming - class 11
Introduction to programming - exercises 2
Introduction to programming - class 8
Introduction to programming class 11 exercises
Sanjay New RESUME
Automatic Performance Modelling from Application Performance Management (APM)...
Radacina alcatuire externa final
Introduction to programming - class 1
Prezentare ppt - Sistemul respirator
Ad

More from Paul Brebner (20)

PPTX
Streaming More For Less With Apache Kafka Tiered Storage
PDF
30 Of My Favourite Open Source Technologies In 30 Minutes
PDF
Superpower Your Apache Kafka Applications Development with Complementary Open...
PDF
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
PDF
Architecting Applications With Multiple Open Source Big Data Technologies
PDF
The Impact of Hardware and Software Version Changes on Apache Kafka Performan...
PDF
Apache ZooKeeper and Apache Curator: Meet the Dining Philosophers
PDF
Spinning your Drones with Cadence Workflows and Apache Kafka
PDF
Change Data Capture (CDC) With Kafka Connect® and the Debezium PostgreSQL Sou...
PDF
Scaling Open Source Big Data Cloud Applications is Easy/Hard
PDF
OPEN Talk: Scaling Open Source Big Data Cloud Applications is Easy/Hard
PDF
A Visual Introduction to Apache Kafka
PDF
Massively Scalable Real-time Geospatial Anomaly Detection with Apache Kafka a...
PDF
Building a real-time data processing pipeline using Apache Kafka, Kafka Conne...
PDF
Grid Middleware – Principles, Practice and Potential
PDF
Grid middleware is easy to install, configure, secure, debug and manage acros...
PPTX
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
PPTX
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
PPTX
Melbourne Big Data Meetup Talk: Scaling a Real-Time Anomaly Detection Applica...
PPTX
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
Streaming More For Less With Apache Kafka Tiered Storage
30 Of My Favourite Open Source Technologies In 30 Minutes
Superpower Your Apache Kafka Applications Development with Complementary Open...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Architecting Applications With Multiple Open Source Big Data Technologies
The Impact of Hardware and Software Version Changes on Apache Kafka Performan...
Apache ZooKeeper and Apache Curator: Meet the Dining Philosophers
Spinning your Drones with Cadence Workflows and Apache Kafka
Change Data Capture (CDC) With Kafka Connect® and the Debezium PostgreSQL Sou...
Scaling Open Source Big Data Cloud Applications is Easy/Hard
OPEN Talk: Scaling Open Source Big Data Cloud Applications is Easy/Hard
A Visual Introduction to Apache Kafka
Massively Scalable Real-time Geospatial Anomaly Detection with Apache Kafka a...
Building a real-time data processing pipeline using Apache Kafka, Kafka Conne...
Grid Middleware – Principles, Practice and Potential
Grid middleware is easy to install, configure, secure, debug and manage acros...
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
Melbourne Big Data Meetup Talk: Scaling a Real-Time Anomaly Detection Applica...
Massively Scalable Real-time Geospatial Data Processing with Apache Kafka and...
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Lesson notes of climatology university.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
STATICS OF THE RIGID BODIES Hibbelers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GDM (1) (1).pptx small presentation for students
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharma ospi slides which help in ospi learning
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O7-L3 Supply Chain Operations - ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
RMMM.pdf make it easy to upload and study
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Microbial diseases, their pathogenesis and prophylaxis
Chinmaya Tiranga quiz Grand Finale.pdf
Lesson notes of climatology university.
O5-L3 Freight Transport Ops (International) V1.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Presentation on HIE in infants and its manifestations
Pharmacology of Heart Failure /Pharmacotherapy of CHF

Introduction to programming - class 6

  • 2. Exercise • Create a cannon trajectory game – Place a cannon at lower left of screen, with a target on lower right. – Set initial cannon angle (0-90 degrees, 0 is horizontal, 90 is vertical), and velocity, and cannon ball location (x and y, same as cannon). – Calculate velocity of the cannon ball, vx and vy, initially: • float vx = velocity * cos(radians(angle)); • float vy = velocity * sin(radians(angle)); – Each frame, change x and y position (and draw ball) and vy as follows: • x += vx; // add vx to get next cannon ball x position • y -= vy; // subtract vy to get next cannon ball y position • vy -= g; // g is gravity acting down, try 1 – Remember that in processing x increases from 0 to width, and y from 0 to height (starting at top left, so increases as ball goes down!) – Use frameRate(1) (or slower) to make debugging easier – How will you change the angle and velocity? How will you fire? How will you check if the ball hits the target? What happens when it hits? – Can you put another cannon on the right and make the game 2 player? – Can you work out how load images for the cannon, ball, target, explosion etc? – Next page is a simple online example
  • 4. Example program structure, add detail to TODOs // global variables for cannon and cannon ball position and velocity go here ... TODO boolean cannonFired = false; // true after cannon fired until ball lands void setup() // set size, background (sky colour), cannon position, and frame rate { TODO } void draw() // everything done once per frame { // draw cannon TODO if (cannonFired) { // move and draw the ball until it hits the ground again, then set cannonFired to false TODO } else { // change angle and velocity and firing of cannon, set cannonFired to true and cannon ball x and y to cannon location TODO } }