A Bouncing Ball

Post on 23-Jan-2016

110 views 2 download

description

A Bouncing Ball. Presented by Quentin Ochem. University.adacore.com. {X = 0, Y = 100}. {X = 0, Y = 0}. {X = -100, Y = 0}. {X = 100, Y = 0}. {X = 0, Y = -100}. References to the graphical library. Declaration of a shape object with an initial value. Parameter value given by name. - PowerPoint PPT Presentation

Transcript of A Bouncing Ball

Slide: 1Copyright © AdaCore

A Bouncing BallPresented by Quentin Ochem

University.adacore.com

Slide: 2Copyright © AdaCore

{X = 0, Y = 0}

{X = 0, Y = 100}

{X = 0, Y = -100}

{X = -100, Y = 0} {X = 100, Y = 0}

Slide: 3Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Slide: 4Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

References to the graphical library

Slide: 5Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Declaration of a shape object with an initial value

Slide: 6Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Parameter value given by name

Slide: 7Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

X is a float, so it needs a floating point literal (0.0) not integer (0)

Slide: 8Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Infinite loop

Slide: 9Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Wait for 1 milisecond

Slide: 10Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

If the ball gets out of the boundaries, then invert the step

Slide: 11Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if Get_X (Ball) > 100.0 then Step := -0.05; elsif Get_X (Ball) < -100.0 then Step := 0.05; end if; Set_X (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Move the X position of the ball

Slide: 12Copyright © AdaCore

Quiz

Slide: 13Copyright © AdaCore

Identify the Errors

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Shape_Id : Ball := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Float : Step := 0.05;begin loop if (Get_Y (Ball) > 100.0) then Step := -0.05; else if Get_Y (Ball) < -100 then Step := 0.05; end if; Set_Y (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Slide: 14Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Shape_Id : Ball := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Float : Step := 0.05;begin loop if (Get_Y (Ball) > 100.0) then Step := -0.05; else if Get_Y (Ball) < -100 then Step := 0.05; end if; Set_Y (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Slide: 15Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Shape_Id : Ball := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Float : Step := 0.05;begin loop if (Get_Y (Ball) > 100.0) then Step := -0.05; else if Get_Y (Ball) < -100 then Step := 0.05; end if; Set_Y (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

Variable aredeclared likename : type;

Slide: 16Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if (Get_Y (Ball) > 100.0) then Step := -0.05; else if Get_Y (Ball) < -100 then Step := 0.05; end if; Set_Y (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

elsif introducesan alternative

Slide: 17Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if (Get_Y (Ball) > 100.0) then Step := -0.05; elsif Get_Y (Ball) < -100 then Step := 0.05; end if; Set_Y (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

100 is not a float literal100.0 would be

Slide: 18Copyright © AdaCore

with Display; use Display; with Display.Basic; use Display.Basic;

procedure Main is Ball : Shape_Id := New_Circle (X => 0.0, Y => 0.0, Radius => 10.0, Color => Blue); Step : Float := 0.05;begin loop if (Get_Y (Ball) > 100.0) then Step := -0.05; elsif Get_Y (Ball) < -100.0 then Step := 0.05; end if; Set_Y (Ball, Get_X (Ball) + Step); delay 0.001; end loop; end Main;

These parenthesis are OK(although useless and not Ada-stylish)

Slide: 19Copyright © AdaCore

university.adacore.com