Cse.sc.edu



Lab 10: Space Invaders Game in Python – Part 1Academic HonestyThe work you turn in is to be your work, not copied from someone else, from the web, or generated by a program.Never allow anyone access to your files.Never give anyone your password.Never share your USB memory or email your files to anyone else.Never give anyone a printed copy of your file or an electronic copy.Never allow anyone to copy your work.PurposeIn the next few labs we will create Space Invaders Game using python and Turtle which you can play like the video attached. The Space Invaders Lab is divided into several labs and this is the first part of it. In this part, you will set up the playing area for space invaders.13258801574800Setting up the ScreenYou need to import the following modules: random and turtle. Set up the screen now. To create a new screen use wn = turtle.Screen()Set window size to 700 by 700 using wn.setup(700, 700)Make the background color to “black” using bgcolor command wn.bgcolor(put your color here )Set the title of window to “Space Invaders” using title command wn.title(put your title here)Download the image files for invader, player and background from the following link will have downloaded three files ‘invader.gif’, ‘player.gif’ and ‘space_invaders_background.gif’. Put them in the same folder as your python file is.Set the background picture of the window to ‘space_invaders_background.gif’ using bgpic command wn.bgpic(put background picture filename here)If you noticed the invader and player pictures, you should set up your turtles’ shapes to look like them. In order to do that, you will have to register them as shapes using register_shape command turtle.register_shape("invader.gif")and also register ‘player.gif’ the same way.center30514700After this your window should look like this:Drawing the Playing AreaInstead of using the same turtle for everything, we will be using different turtle to do draw different turtle. Get a new turtle to draw border of playing area as follows border_pen = turtle.Turtle()Set the pencolor to ‘white’.We want the border of playing area to be visible, so set the pensize to larger size may be 3 or 4 using border_pen.pensize(3 or 4)Our playing area will a square of size 600 x 600 with center at (0,0). So, the four corners of the square would be (-300, -300), (-300, 300), (300, -300) and (300, 300). The best way to draw this square would be to go to one of the corners and draw square of side 600. You can go to any of the corners using setpos(x,y). border_pen.setpos(x, y) where x and y are coordinates of corners.You already know how to draw a square using while or for loop. Do not forget to do the penup() before going to corner. Since this pen was just being used for drawing border, we will not use this pen now and hide it using border_pen.hideturtle()Now your screen should look something like thiscenter992900Displaying ScoreWe will need a variable to keep track of score. Create a variable named score and set it to 0.Create a new turtle pen to write score value like previous step. You will put that score on the left most corner at position (-290, 270) on the screen. So, much the cursor to that position using setpos() and also change the pen color to white.After moving to that position, the next step is to write the current score. The score string by concatenating “Score: “ with score value. score_text = “Score: “ + str(score)Now, write that score text using turtle.write()score_pen.write(score_text, False, align="left", font=("Arial", 14, "normal"))Then, hide the score pen for now as in previous example. We will show it again while updating score. The window should look like this center12585Creating PlayerCreate new turtle turtle.Trutle() and initialize player variable. player = turtle.Turtle()Since you already registered the ‘player.gif’ as shape, you set the shape of player turtle to be ‘player.gif’ so that play looks like shooter. player.shape(your gif filename here)Set the position of player turtle to (0, -250) as you want to start the game with player at center x-axis wise but at bottom y-axis wise. Also, set player heading to 90 as you will be shooting upwards. The playing arena should look something liker the picture below, notice the red shooter at bottom.center4982Creating Enemies/InvadersSet the number of enemy turtles you want to create and save to a variable. You approach should be generic enough so that you can create more enemies just by changing a variable. Create variable enemies which contains list of turtles. Then, append the created turtles to that list. This concept is explained on set the random position of enemies, you need to generate x and y coordinate using random.randomint(lower_range, upper_range), which you have already used in previous labs. Get x coordinate to be a random number between -250 and 250 and y coordinate between 100 and 200. Then, set the position of enemy turtles to that random value.For each enemy set the shape to ‘invader.gif’ using the same code as you did for player.At the end of this step, your game window should look like this.Creating Player BulletCreate a new turtle for player bullet. Set the shape of bullet to triangle. Check this video on how to create shapes. the heading of bullet to 90 degrees as we want to fire upwards.Set the pencolor to ‘yellow’ for the bullet.Set the bullet shape size to 0.5 using the following command bullet.shapesize(0.5, 0.5)Then, hide the bullet turtle as we only need to see it only when we fire.Submitting your filesCopy your .py file and move it to your X:\101Labs directory for grading.Make sure your files are named Lab10XY.py where XY are your initialsPrint your code and submit it to your lab instructor at the beginning of your next lab class. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download