Question
CHALLENGING don't know who to ask , Problem with code below, instructions : Lets make a video game. Have you ever played whack-a-mole? Well, say
CHALLENGING don't know who to ask, Problem with code below, instructions: Lets make a video game. Have you ever played whack-a-mole? Well, say hello to the new HIT GAME that EVERYONE will want to play..... Whack-a-mole 3D with high-speed motion sensor controls and multi-user online social play mode! We havent started making the game yet, but we need to hurry because we already promised investors a 2018 rollout. Your job is to create a simple prototype version of whack-a-mole for testing and feedback.
Ok, so heres the project specifications. I want you to make a single python file called Project 3 that can be used as a module, with everything wrapped up in function definitions. At the top of your file (below your name) you must import the random and turtle modules. The functions you need to define are as follows. Dont be too intimidated, I am writing a lot of words here mainly so its clear what Im asking you to do. Read the instructions carefully and let me know if you have any questions.
0) First, an overview. I want you to make a whack-a-mole prototype that behaves as follows. First, the game screen will be generated (using turtle) and will include a 5x5 grid of dots. Some of the dots will be red, some black, and one will be blue. The black dots are the holes the moles live in, the red dots are the moles poking their heads out of the hole, and the blue dot is your mallet with which to whack the moles. By calling the appropriate functions, I want to be able to move the blue dot to where the red dots are, removing the red dots from the game until all the dots are black (except for the blue dot).
1) You can start your code by initializing two global variables: moles = [] and mallet = (). The first one will be used as a list of mole locations while the second will keep track of the mallet location. For instance, moles = [(0, 0), (0, 80)] and mallet = (-80, 0) means that there are two moles at coordinates (0, 0) and (0, 80) and the mallet is at (80, 0).
2) You should make a function named rand coord() which takes no inputs and simply returns a random grid coordinate (like (0, 80), or something) using the random module. You want the 5x5 grid to be centered on (0,0), and you want the spacing between adjacent grid points to be about 80 (feel free to change the spacing if this is too big or small on your moniter). Thus, you want rand coord() to return a tuple of the form (r1, r2) where r1 and r2 are each a random choice of -160, -80, 0, 80, or 160.
3) You should have a function named make mole(t) which takes one input t, which is assumed to be a turtle object (we will create a turtle object t in the next function, which will be fed to this one). This function first picks out a random position on the grid using the previous function. If the random position is already in the moles list, or equals the mallet position, it will repeatedly re-randomize until it finds a random position that isnt in the moles list. Then it uses t to draw a red dot at that position, and appends the coordinates of that position to the moles list. To draw a dot, use for instance t.dot(20,red) to draw a red dot of radius 20 wherever t is located (you have to move t to the random position first before drawing the dot). You can use whatever radius works best for your screen.
4) You must have a function named create game() which takes no inputs. This function must create the turtle object t using t = turtle.Turtle() (which opens a turtle window), draws the 5x5 grid of dots on the screen, 9 of which will be red, 1 of which will be blue, and the rest will be black. Which dots are which color must be chosen at random. For instance, you could first draw a 5x5 grid of black dots, then get a random coordinate (using rand coord()) and make that dot blue, and then use the make mole(t) function 9 times to get 9 random moles. Your goal in the next function is to provide the means for the mallet to be moved to where the moles are.
A few notes. Make sure to use t.hideturtle(), t.up(), and t.speed(0) to make sure the pointer doesnt show, the turtle doesnt draw lines (you only want to draw dots), and to make sure the turtle draws as fast as possible. Again, you should try to center the 5x5 grid on the screen and use spacing of about 80 between the centers of adjacent dots. Also, make sure to store the location of the blue dot in the global mallet variable.
One final note: the function create game must return the turtle object t by ending the function with return t. This way, t can be passed as an argument to the next and previous functions without making t a global variable.
5) You must have a function named move(t, direc) which takes two inputs: a turtle object t and a direction direc. This function must move the blue dot along the grid in the desired direction. You can assume only four directions are possible: up, down, left, and right. The way turtle methods work, the up direction is direc = 90, the left direction is direc = 180, etc. When move(t, 90) is called, for instance, you want to blue dot to move up one level on the grid. Also, just assume the user doesnt try to move out of the grid (otherwise you might need extra code to deal with that). Three more things. If the blue dot moves onto a red dot (a mole), you should remove that mole from the mole list. Also, you should update the mallet variable with the new position of the mallet. Finally, after moving the blue dot, you must create with 40% probability a new mole using the make mole(t) function again.
Notice that turtle drawings overwrite each other. Thus, when I want to move the blue dot, what I really want to do is draw a black dot on top of the blue dot and then draw a blue dot on top of whatever dot Im moving into. In that sense, there is no need to erase anything in this project.
MY CODE:
import turtle import random import math randx = [-160, -80, 0, 80, 160] speed = 0 mallet = () #This creates the screen wm = turtle.Screen() wm.bgcolor("Pink") #This creates the border border = turtle.Turtle() border.penup() border.speed(11000) #fast speed so you dont have to wait for border border.setposition(-160, -160) border.pendown() for x in range (4): #makes the large black box border.forward(320) border.left(90) border.hideturtle() #hides the arrow holes = [] #list where hole cords are hidden totalholes = 150 for count in range(totalholes): #this code holes.append(turtle.Turtle()) holes[count].color("Black") holes[count].shape("square") holes[count].penup() holes[count].speed(0) holes[count].setposition(random.choice(randx), random.choice(randx)) max_moles = 9 #max number of moles moles = [] #moles list for count in range(max_moles): moles.append(turtle.Turtle()) #attach mole to list moles[count].color("Red") #moles color moles[count].shape("square") # moles shape moles[count].penup() #moles dont draw moles[count].speed(0) #moles dont move moles[count].setposition(random.choice(randx), random.choice(randx)) #moles get randomly placed #Creates our mallet mallet = turtle.Turtle() mallet.shape("square") #assigns shape mallet.color("blue") # assigns color mallet.penup() #makes it not mark down lines mallet.setposition(0,0) #centers mallet at start #keyboard controls def right(): mallet.right(90) def left(): mallet.left(90) def forward(): mallet.forward(80) def backward(): mallet.back(80) def collide(t1, t2): #collision Defenition d = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2) + math.pow(t1.ycor() - t2.ycor(), 2)) if d < 20: return True else: return False turtle.listen() turtle.onkey(forward, "Up") turtle.onkey(right, "Right") turtle.onkey(left, "Left") turtle.onkey(backward, "Down") while True: mallet.forward(speed) if mallet.xcor() > 161 or mallet.xcor() < -161: mallet.backward(80) if mallet.ycor() > 161 or mallet.ycor() < -161: mallet.backward(80) for count in range(max_moles): if collide(mallet, moles[count]): moles[count].setposition(random.choice(randx), random.choice(randx)) PROBLEMS: MOLES STACKING UP ON EACH OTHER, IS THERE A BETTER WAY TO PUT BLACK BOXES INSTEAD OF HAVING 250 RANDOMLY GENERATED COORDINATES, 40 PERCENT CHANCE EVERY TIME MALLET MOVES THERE'S A CHANCE FOR MOLE TO BE CREATED. For the most part it works, just those small errors. (Used Python)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started