A Bouncing Ball

19
Slide: 1 Copyright © AdaCore A Bouncing Ball Presented by Quentin Ochem University.adacore.com

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

Page 1: A Bouncing Ball

Slide: 1Copyright © AdaCore

A Bouncing BallPresented by Quentin Ochem

University.adacore.com

Page 2: A Bouncing Ball

Slide: 2Copyright © AdaCore

{X = 0, Y = 0}

{X = 0, Y = 100}

{X = 0, Y = -100}

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

Page 3: A Bouncing Ball

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;

Page 4: A Bouncing Ball

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

Page 5: A Bouncing Ball

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

Page 6: A Bouncing Ball

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

Page 7: A Bouncing Ball

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)

Page 8: A Bouncing Ball

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

Page 9: A Bouncing Ball

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

Page 10: A Bouncing Ball

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

Page 11: A Bouncing Ball

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

Page 12: A Bouncing Ball

Slide: 12Copyright © AdaCore

Quiz

Page 13: A Bouncing Ball

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;

Page 14: A Bouncing Ball

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;

Page 15: A Bouncing Ball

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;

Page 16: A Bouncing Ball

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

Page 17: A Bouncing Ball

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

Page 18: A Bouncing Ball

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)

Page 19: A Bouncing Ball

Slide: 19Copyright © AdaCore

university.adacore.com