Random Background Generator

artillery-gameIn this challenge we will use a Python script (using Python Turtle) to generate a random background for a 2D video game.

artillery-game-background

To create a mountain range skyline we will use a polynomial function:

Polynomial Function of Degree 2

Polynomial Function of Degree 2

Polynomial Function of Degree 3

Polynomial Function of Degree 3

Polynomial Function of Degree 4

Polynomial Function of Degree 4

Polynomial Function of Degree 5

Polynomial Function of Degree 5

Polynomial Function of Degree 6

Polynomial Function of Degree 6

Polynomial Function of Degree 7

Polynomial Function of Degree 7

 

In our example (see trinket below) we will use a polynomial function of degree 3:

y = a*x3 + b*x2 + c*x + d

The coefficients a,b,c,d of our polynomial function will be randomly generated. This will hence create a different background/skyline every time the program is executed.

We will then use this function to place some trees on the skyline. The x coordinate of the tree will be randomly generated whereas the polynomial equation will be used to calculate the y coordinate of each tree.

Python Code


Your Challenge


Update this code to add another set of trees. The trees could be positioned randomly anywhere on the grass area of the landscape. (Anywhere between the bottom of the screen and the skyline)

Extension Task


Update this code to add some clouds. The clouds should be positioned randomly anywhere in the sky. (Anywhere between the top of the screen and the skyline)

Share Button
Posted in Computer Science, Python - Advanced, Python Challenges Tagged with: ,

Our Latest Book

Follow this blog