Asteroids Tutorial- Spaceship

This game is written with an object-oriented approach. The spaceship, bullets, asteroids, and explosions are all classes.

Creating the Spaceship

The spaceship requires some variables: the initial x location, the initial y location, and the initial direction. The spaceship also needs to be able to move, so you will need a x-speed and a y-speed. As we know, in order to change velocity, you will need acceleration, so also include a thrust variable; however, we don’t want to spaceship to accelerate forever. Finally, players typically have several lives in video games, so include a lives variable as well. There is a self.alive variable, but don’t worry about that until later, self.lines is for future collision detection, and self.tipx and self.tipy are variables to assist in drawing the ship. 

Drawing the Spaceship

As you can see, the function was called draw_one. That is because we want the spaceship to wrap around the screen if it flies off. A simple way to implement this is by drawing five spaceships, but only one of them is visible at a time. When that one spaceship touches a border, one of its clones will be visible on the opposite edge. Note that if your game window changes, the numbers below must be changes as well. This will take some trial and error.

Moving the Spaceship

Moving the spaceship is easy enough. We simply need to multiply the speed the spaceship is going in the x and the y direction by the time it has been moving and add those values to the original x and y locations. Since we limited the thrust, the thrust life variable must be updated as well.  We want to change direction, so when the left and right arrow keys are pressed, let’s update the direction instance variable. Finally, we need to implement the actual thrust mechanism. Whenever the thrust key is pressed, we want to give the thrust some life to begin with as well as the direction we are thrusting in. We will update the x speed and the y speed accordingly. If you wish to limit the max speed of the spaceship, don’t allow the thrust from exceeding that limit.

Visit this link for the full code: https://github.com/HaimingXu679/Asteroids/blob/master/spaceship.py

Asteroids Tutorial- Explosions

The Explosions Class

Each explosion should have its own x and y location, future locations, moving directions, and a set of random speeds. A loop (of 12 iterations) adds the initial x and y location as a pair to a list, a random number between 0 and 2*π to another list, and a random number between 300 and 500 (these numbers can be adjusted to preference) to the speeds list. The speeds will decrease by a percentage per iteration of the explosion.

Moving the Explosion

For each of the copies of the initial x and y location (in the above screenshot, self.p), edit the x location by the speed multiplied by the time multiplied by the cosine of the corresponding direction and edit the y location by the speed multiplied by the time multiplied by the sine of the corresponding direction. Then, decrease the speed of the explosion by 10%. 

Drawing the Explosion

First, clear the screen of any residual marks. Then, go through the list of locations and draw a dot for each one.

Animation

The exploding animation is standard and self explanatory.

View this page for full code: https://github.com/HaimingXu679/Asteroids/blob/master/explosion.py

Asteroids Tutorial- Bullets

The Bullets Class

First, we need to create bullet class. There should be some variables that apply to all the bullets, such as its speed and how range. The starting location of each bullet should be different, so the initial x and y location should be instance variables.

Drawing the Bullet

This part can be varied by the programmer. Here, we made the bullet simply a dot. It should appear at the point of the spaceship, so move the bullet there. Be sure to only draw the bullet if the bullet still has range left.

Moving the Bullet

As the bullet moves, its range should fall (you don’t want the bullet moving forever). The amount you want to move is, as usual, speed multiplied by time. However, you must move the bullet in both the x direction and the y direction. The distance is speed * time, but you need to calculate the x and y component. See the code for the answer.

Execution

Animation

If you want to show the bullet moving, pass in short time interval to the move method (the smaller it is, the smoother the animation). After moving the bullet, clear the screen and draw the bullet again. If the bullet hits its maximum range,  stop the animation.

Firing

The list bullets contains all the bullets in the queue or currently animated. If the last bullet still has life left, you should not fire (to limit the amount of bullets). Otherwise, add a bullet to the list. At the moment, put it in a random location and add it to the list; when we combine the different files, we will make the bullet go to the  spaceship. Note that currently, everything under the execution header happens only if the code is run independently. After we combine the files, fire and animation will be slightly different.

View this link for the full code: https://github.com/HaimingXu679/Asteroids/blob/master/bullet.py

Asteroids Tutorial- Asteroids

Creating the Asteroids

The asteroids in this game are 12-sided polygons (dodecagons). The asteroid class should have five parameters (its size, x location, y location, speed, and tilt). The latter two parameters are default parameters. You will need to initialize the variables for each instance. After that, add 12 side lengths to a list that makes the dodecagon. 

Drawing the Asteroids

The asteroids are tilted in different directions. Also, you cannot just randomly connect consecutive points; this could lead to lines crossing and thus your shape is no longer a dodecagon. Start at the initial x and y position of the asteroid and increment that by the first side length multiplied by the tilt variable. Since a dodecagon has 12 points, there are 12 rays from the center of the dodecagon to each point; these 12 rays divide a circle of 360 degrees; thus, before you draw the line to the next point, you must increment the angle by 30 degrees (π/6). Take the next side length, multiply it by the cosine of the angle to get the x component of the vector and similarly multiply it by the sine of the angle to get the y component of the vector. Increment the x and y locations and add the values to the list of points.

Movement Off Screen

When the asteroids inevitably go beyond the screen, you want the asteroid to wrap around from the opposite side that it came from. Similar to the approach taken by the spaceship, simply draw five copies of the asteroid, four of them invisible (off-screen) and one in the middle. Whenever one goes off the screen, adjust which asteroid is in the middle.

Animation

The animation process is simple. Move the asteroid a bit, draw the new asteroid, then update the screen.

Independent Runs

When this file is run independent of the main game file, just animate a singular asteroid moving around in space, wrapping around the edges.

View this link for the full code: https://github.com/HaimingXu679/Asteroids/blob/master/asteroid.py

View this link for the full repository: https://github.com/HaimingXu679/Asteroids

Drawing A Chinese College Entrance Exam Problem

A YouTube channel MindYourDecisions posted a video about an interesting Chinese College Entrance exam question. The following is the screen of the problem posted by MindYourDecisions:

Draw the graph of the function ranging from [-3,3]. Also draw a horizontal line with y value -8/9.

Check out this project for how to draw general function graphs with Python Turtle.