Here is what is happening with the student’s code:
Once time Setup (before the draw loop):
- put the chicken in a random place.
- draw all the sprites (the chicken)
- draw the background (covering up everything)
- put the seed in a random place
- draw all the sprites (chicken and seed)
Over and over (Inside the draw loop):
- If key is pressed
- move the chicken
- draw all the sprites (seed and chicken)
There are a couple problems with this. First, the student does not need to draw your sprites so many times. They don’t need to be drawn in the setup (outside the draw loop) at all. Just put them where you want them to go, and they will be drawn inside the draw loop.
Second, the background is only being drawn once. That’s why the chick is showing up over and over. Every time you move it, it is redrawn, but the code is not using “background” to cover up the old versions of it.
Third, all the sprites should be drawn automatically by default in every iteration of the draw loop, not just if something has been moved.
In general, you want to draw a background at the beginning of the draw loop so that old drawings are all covered up. Then, after you make all of your changes, using the if statements, you want to draw all of your sprites.
drawSprites is usually only called once in the entire program.
One time Setup (outside draw loop):
- Create all the sprites, and give them their default properties.
Over and over (in the draw loop):
- draw the background (covering everything up)
- Use conditionals, counter patter, etc. to make any changes to sprites
- draw all the sprites.