The Point of the game is to make all the circles disappear when they Collide but for some reason some circles aren't disappearing? - Thank you for your help in advance!
import turtle import random import math import time
Setting up the Screen ms = turtle.Screen() ms.bgcolor("red") ms.title("Space Rocket Minigame @Rafa94")# Using class
functions/Methods
subclass class Border(turtle.Turtle):
def __init__(self):# class constrcutor turtle.Turtle.__init__(self)# adding our Objects attributes all starting with "self" self.penup() self.hideturtle() self.speed(0) self.color("silver") self.pensize(5) def draw_border(self): self.penup()# getting our pen to start drawing self.goto(-300, -300) self.pendown() self.goto(-300, 300) self.goto(300, 300) self.goto(300, -300) self.goto(-300, -300)
class Player(turtle.Turtle):# since it got inherited this class becomes a Superclass
def __init__(self):# self is our only arguement here but it will have muiltple attributes turtle.Turtle.__init__(self)# since we are using the Turtle module we are able to use it's built in functions self.penup()# our attributes self.speed(0) self.shape("triangle") self.color("black") self.speed = 0.1 def move(self): self.forward(self.speed) #Border Checking if self.xcor() > 290 or self.xcor() < -290:# Left side is -290 Right side is 290 we also want the coordinates x and y to be below 300
to not go over our border self.left(60) if self.ycor() > 290 or self.ycor() < -290: self.left(60)
def turnleft(self): self.left(30) def turnright(self): self.right(30) def increasespeed(self): self.speed += 1
class Goal(turtle.Turtle): # Sub Class
def __init__(self): turtle.Turtle.__init__(self) # since we are using the Turtle module we are able to use it's built in functions self.penup() # our attributes self.speed(0) self.shape("circle") self.color("green") self.speed = 3 #xcor #ycor self.goto(random.randint(-250, 250), random.randint(-250, 250))# we are making our turtle "go to" X & Y coordinates by -250 and
250 only randomly. We also have our random module here aswell self.setheading(random.randint(0,360))# setting the heading to see in which direction i want it to go
def jump(self):# Jump = Collidee self.goto(random.randint(-250, 250), random.randint(-250, 250))#"jump" stands for Collidee so if the circle "jumps" with player
it will move to random postion by 250 and -25 self.setheading(random.randint(0,360))# from where it collidee's it goes 360 moves location 360 Right
def move(self):# we copyed the same method cause it will be doing the same movements as the player we want it to go "forward" with our
set "speed" & also check for our borders we set self.forward(self.speed)
# Border Checking if self.xcor() > 290 or self.xcor() < -290: # Left side is -290 Right side is 290 we also want the coordinates x and y to be below 300 to not go over our border self.left(60) if self.ycor() > 290 or self.ycor() < -290: self.left(60)
Collision checking function/Method
Uses the Pythgorean Theorem to measure the distance between two objects def isCollision(t1, t2):#t1 = turtle1 t2 = turtle also when a
function starts with "is" isCollision most likely it will be a Boolean of True or False a = t1.xcor()-t2.xcor()# xcor = Right -xcor = Left/ when they collide the xcor is 0 b = t1.ycor()-t2.ycor()# ycor = Right -ycor = Left/ when they collide the ycor is 0 distance = math.sqrt((a ** 2) + (b ** 2))
if distance < 30: return True else: return False
Create class instances player = Player()# after creating a class must make an instances to call it in other words make an Object of the
class border = Border()#sub class
goal = Goal()#sub class
Draw our border border.draw_border()
create multiple goals goals = []#Creating a list of goals for count in range(6):#We are making the code repeat 6 times
goals.append(Goal())#each time the code runs it puts a goal the end 6 times
Set keyboard bindings ms.listen() ms.onkey(player.turnleft, "Left") ms.onkey(player.turnright, "Right") ms.onkey(player.increasespeed,
"Up")
speed game up ms.tracer(0.1)
main loop while True:
ms.update() player.move()# these two are class methods #goal.move()# the reason we copyed like we said is cause it's gunna have the exact same movements as our player! # we want the goal to be True to in our while loop in order for the code to be excuted for goal in goals: goal.move() # Basically saying If there is a collision between the player and goal we the want the goal to "jump" / Function
in our while True loop if isCollision(player, goal): goal.jump()# baiscally saying if the goal collide's move or "jump" to other location
time.sleep(0.005)
Blockquote
Aucun commentaire:
Enregistrer un commentaire