× Didn't find what you were looking for? Ask a question
Top Posters
Since Sunday
g
1
New Topic  
S.v S.v
wrote...
Posts: 54
Rep: 3 0
A year ago
While completing this assignment you will learn important concepts of the software design:
1. Using list variables
2. Keyboard binding
3. Randomization
4. While-loops
5. Collision detection
Copy the code provided in the template.
To earn the mark in the respective range:
Level B
1. Change the graphics.
2. Use at least 2 external images, for example, the background and one of the ‘turtles’.
3. Change the boundaries.
4. Relocate the score.
5. Create a win message.
Level A
In addition to Level B requirements:
1. Use at least 3 external images, for example, the background and two ‘turtles’, including the list.
2. Create next level.
3. Use font(s).
Level A+
In addition to Level A and B requirements:
1. Use extra turtle(s) – make them not only ‘chase’ but also ‘escape’ with the deduction of the score.
2. Create a ‘losing’ screen.
3. Include the results of independent research to provide the user with outstanding experience.

my code is:

import turtle
import math
import random
import time

wn = turtle.Screen()
wn.setup(1000, 667)
wn.tracer(2)
wn.bgpic("space.gif")

wn.register_shape("ship.gif")
wn.addshape("ship.gif")

wn.register_shape("bigship.gif")
wn.addshape("bigship.gif")

mypen = turtle.Turtle(visible=False)
mypen.hideturtle()
mypen.pensize(3)
mypen.color('white')

player = turtle.Turtle()
player.shape("bigship.gif")
player.penup()
player.speed (0)

maxAims = 6
aims = []

for count in range(maxAims):
    aims.append(turtle.Turtle())
    aims[count].shape("ship.gif")
    aims[count].penup()
    aims[count].speed(0)
    aims[count].setposition(random.randint (-280, 280), random.randint (-280, 280))

speed = 1
score = 0

def turnleft():
    player.left(30)

def turnright():
    player.right(30)

def increasespeed():
    global speed
    speed += 0.5

def decreasespeed():
    global speed
    if speed > 0: 
        speed -= 1


turtle.listen()
turtle.onkey (turnleft, 'Left')
turtle.onkey (turnright, 'Right')
turtle.onkey (increasespeed, 'Up')
turtle.onkey (decreasespeed, 'Down')


while True:
    player.forward(speed)

    if player.xcor() > 480 or player.xcor() < -480:
        player.right(180)
    if player.ycor() > 310 or player.ycor() < -310:
        player.right(180)
       
    for count in range(maxAims):
        aims[count].forward(3)

        if aims[count].xcor()>480 or aims[count].xcor()<-480:
            aims[count].right(180)

        if aims[count].ycor()>480 or aims[count].ycor()<-480:
            aims[count].right(180)

        d = math.sqrt(math.pow(player.xcor()-aims[count].xcor(),2)+math.pow(player.ycor()-aims[count].ycor(),2))

        if d < 70: 
            aims[count].hideturtle()
            aims[count].setposition(-300, -300)
            score += 1
            mypen.undo()
            mypen.penup()
            mypen.hideturtle()
            mypen.color('white')
            mypen.setposition(-500, 300)
            scorestring = "Score: %s" %score
            mypen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))

        if d < 40: 
            aims[count].hideturtle()
            aims[count].setposition(-300, -300)
            score += 1
            mypen.undo()
            mypen.penup()
            mypen.color('white')
            mypen.hideturtle()
            mypen.setposition(-500, 300)
            scorestring = "Score: %s" %score
            mypen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))

        if score == 6:
            player.hideturtle()
            break

I'm having the issue where when I angle the player and it moves and hits certain parts in the bottom it adds to the score. I'd also like some help if anyone could start doing the other part (A and A+).
 Attached file(s) 
Thumbnail(s):
You must login or register to gain access to these attachments.
Read 2554 times
8 Replies

Related Topics

Replies
Anonymous
wrote...
A year ago
Currently, you're using two conditions for collision detection.

Code:
if d < 70:
    # Collision detected, do something

if d < 40:
    # Collision detected, do something else

Use only one condition for collision detection and adjust the distance threshold accordingly.

Code:
if d < 30:
    # Collision detected
    aims[count].hideturtle()
    aims[count].setposition(-300, -300)
    score += 1
    mypen.undo()
    mypen.penup()
    mypen.hideturtle()
    mypen.color('white')
    mypen.setposition(-500, 300)
    scorestring = "Score: %s" % score
    mypen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))

By adjusting the threshold (from 40 to 30), you ensure that the collision is detected accurately without overlapping with other collision conditions.
S.v Author
wrote...
A year ago
import turtle
import math
import random
import time

wn = turtle.Screen()
wn.setup(1000, 667)
wn.tracer(2)
wn.bgpic("space.gif")

wn.register_shape("ship.gif")
wn.addshape("ship.gif")

wn.register_shape("bigship.gif")
wn.addshape("bigship.gif")

mypen = turtle.Turtle(visible=False)
mypen.hideturtle()
mypen.pensize(3)
mypen.color('white')

player = turtle.Turtle()
player.shape("bigship.gif")
player.penup()
player.speed (0)

maxAims = 6
aims = []

for count in range(maxAims):
    aims.append(turtle.Turtle())
    aims[count].shape("ship.gif")
    aims[count].penup()
    aims[count].speed(0)
    aims[count].setposition(random.randint (-280, 280), random.randint (-280, 280))

speed = 1
score = 0

def turnleft():
    player.left(30)

def turnright():
    player.right(30)

def increasespeed():
    global speed
    speed += 0.5

def decreasespeed():
    global speed
    if speed > 0: 
        speed -= 1


turtle.listen()
turtle.onkey (turnleft, 'Left')
turtle.onkey (turnright, 'Right')
turtle.onkey (increasespeed, 'Up')
turtle.onkey (decreasespeed, 'Down')


while True:
    player.forward(speed)

    if player.xcor() > 480 or player.xcor() < -480:
        player.right(180)
    if player.ycor() > 310 or player.ycor() < -310:
        player.right(180)
       
    for count in range(maxAims):
        aims[count].forward(3)

        if aims[count].xcor()>480 or aims[count].xcor()<-480:
            aims[count].right(180)

        if aims[count].ycor()>480 or aims[count].ycor()<-480:
            aims[count].right(180)

        d = math.sqrt(math.pow(player.xcor()-aims[count].xcor(),2)+math.pow(player.ycor()-aims[count].ycor(),2))

        if d < 30:
            # Collision detected
            aims[count].hideturtle()
            aims[count].setposition(-300, -300)
            score += 1
            mypen.undo()
            mypen.penup()
            mypen.hideturtle()
            mypen.color('white')
            mypen.setposition(-500, 300)
            scorestring = "Score: %s" % score
            mypen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))

        if score == 6:
            player.hideturtle()
            break


ok so I've tested this out a little bit and im still getting the same issue where somewhere in the bottom left the bigship adds a point
Anonymous
wrote...
A year ago
Can you add these print statements to your code so that I can debug it better?

Code:
while True:
    print("Player position:", player.position())
    player.forward(speed)

    if player.xcor() > 480 or player.xcor() < -480:
        player.right(180)
    if player.ycor() > 310 or player.ycor() < -310:
        player.right(180)

S.v Author
wrote...
A year ago Edited: A year ago, S.v
import turtle
import math
import random
import time

wn = turtle.Screen()
wn.setup(1000, 667)
wn.tracer(2)
wn.bgpic("space.gif")

wn.register_shape("shipright.gif")
wn.addshape("shipright.gif")

wn.register_shape("shipleft.gif")
wn.addshape("shipleft.gif")

wn.register_shape("bigship.gif")
wn.addshape("bigship.gif")

wn.register_shape("bigshipdown.gif")
wn.addshape("bigshipdown.gif")

wn.register_shape("bigshipleft.gif")
wn.addshape("bigshipleft.gif")

wn.register_shape("bigshipright.gif")
wn.addshape("bigshipright.gif")


mypen = turtle.Turtle(visible=False)
mypen.hideturtle()
mypen.pensize(3)
mypen.color('white')

player = turtle.Turtle()
player.shape("bigship.gif")
player.penup()
player.speed (0)

maxAims = 6
aims = []

for count in range(maxAims):
    aims.append(turtle.Turtle())
    aims[count].shape("shipright.gif")
    aims[count].penup()
    aims[count].speed(0)
    aims[count].setposition(random.randint (-280, 280), random.randint (-280, 280))

speed = 1
score = 0

def turnleft():
    player.left(30)
    player.shape("bigshipleft.gif")
   
def turnright():
    player.right(30)
    player.shape("bigshipright.gif")

def shipup():
    player.setheading(90)
    player.shape("bigship.gif")

def shipdown():
    player.setheading(-90)
    player.shape("bigshipdown.gif")
   
def increasespeed():
    global speed
    speed += 0.5

def decreasespeed():
    global speed
    if speed > 0: 
        speed -= 1


turtle.listen()
turtle.onkey (turnleft, 'Left')
turtle.onkey (turnright, 'Right')
turtle.onkey (shipup, 'Up')
turtle.onkey (shipdown, 'Down')
turtle.onkey (increasespeed, 'Shift_L')
turtle.onkey (decreasespeed, 'Control_L')

while True:
    print("Player position:", player.position())
    player.forward(speed)

    if player.xcor() > 480 or player.xcor() < -480:
        player.right(180)
    if player.ycor() > 310 or player.ycor() < -310:
        player.right(180)
       
    for count in range(maxAims):
        aims[count].forward(3)

        if aims[count].xcor()>480 or aims[count].xcor()<-480:
            aims[count].right(180)

        if aims[count].ycor()>480 or aims[count].ycor()<-480:
            aims[count].right(180)

        d = math.sqrt(math.pow(player.xcor()-aims[count].xcor(),2)+math.pow(player.ycor()-aims[count].ycor(),2))

        if d < 30:
            # Collision detected
            aims[count].hideturtle()
            aims[count].setposition(-300, -300)
            score += 1
            mypen.undo()
            mypen.penup()
            mypen.hideturtle()
            mypen.color('white')
            mypen.setposition(-480, 280)
            scorestring = "Score: %s" % score
            mypen.write(scorestring, False, align="left", font=("Comic Sans MS", 20, "normal"))

        if score == 6:
            player.hideturtle()
            break

hey so I've updated my code as well as added in the part of the code you wanted me to add
Post Merge: A year ago

ok so I changed the code:

import turtle
import math
import random
import time

wn = turtle.Screen()
wn.setup(1000, 667)
wn.tracer(2)
wn.bgpic("space.gif")

wn.register_shape("shipright.gif")
wn.addshape("shipright.gif")

wn.register_shape("shipleft.gif")
wn.addshape("shipleft.gif")

wn.register_shape("bigship.gif")
wn.addshape("bigship.gif")

wn.register_shape("bigshipdown.gif")
wn.addshape("bigshipdown.gif")

wn.register_shape("bigshipleft.gif")
wn.addshape("bigshipleft.gif")

wn.register_shape("bigshipright.gif")
wn.addshape("bigshipright.gif")


mypen = turtle.Turtle(visible=False)
mypen.hideturtle()
mypen.pensize(3)
mypen.color('white')

player = turtle.Turtle()
player.shape("bigship.gif")
player.penup()
player.speed (0)

maxAims = 6
aims = []

for count in range(maxAims):
    aims.append(turtle.Turtle())
    aims[count].shape("shipright.gif")
    aims[count].penup()
    aims[count].speed(0)
    aims[count].setposition(random.randint (-280, 280), random.randint (-280, 280))

speed = 1
score = 0

def turnleft():
    player.left(30)
    player.shape("bigshipleft.gif")
   
def turnright():
    player.right(30)
    player.shape("bigshipright.gif")

def shipup():
    player.setheading(90)
    player.shape("bigship.gif")

def shipdown():
    player.setheading(-90)
    player.shape("bigshipdown.gif")
   
def increasespeed():
    global speed
    speed += 0.5

def decreasespeed():
    global speed
    if speed > 0: 
        speed -= 1


turtle.listen()
turtle.onkey (turnleft, 'Left')
turtle.onkey (turnright, 'Right')
turtle.onkey (shipup, 'Up')
turtle.onkey (shipdown, 'Down')
turtle.onkey (increasespeed, 'Shift_L')
turtle.onkey (decreasespeed, 'Control_L')

while True:
    print("Player position:", player.position())
    player.forward(speed)

    if player.xcor() > 480:
        player.right(180)
        player.shape("bigshipleft.gif")
    if player.xcor() < -480:
        player.right(180)
        player.shape("bigshipright.gif")
       
    if player.ycor() > 310 or player.ycor() < -310:
        player.right(180)
       
    for count in range(maxAims):
        aims[count].forward(3)

        if aims[count].xcor()>480:
            aims[count].right(180)
            aims[count].shape('shipleft.gif')
        if aims[count].xcor()<-480:
            aims[count].right(180)
            aims[count].shape('shipright.gif')

        if aims[count].ycor()>480 or aims[count].ycor()<-480:
            aims[count].right(180)

        d = math.sqrt(math.pow(player.xcor()-aims[count].xcor(),2)+math.pow(player.ycor()-aims[count].ycor(),2))

        if d < 30:
            # Collision detected
            aims[count].hideturtle()
            aims[count].setposition(-300, -300)
            score += 1
            mypen.undo()
            mypen.penup()
            mypen.hideturtle()
            mypen.color('white')
            mypen.setposition(-480, 280)
            scorestring = "Score: %s" % score
            mypen.write(scorestring, False, align="left", font=("Comic Sans MS", 20, "normal"))

        if score == 6:
            player.hideturtle()
            break

Anonymous
wrote...
A year ago
If you still have the  issue where the player ship gains a point when hitting certain areas, try this:

Code:
if player.xcor() > 480:
    player.right(180)
    player.shape("bigshipleft.gif")
if player.xcor() < -480:
    player.right(180)
    player.shape("bigshipright.gif")

if player.ycor() > 310 or player.ycor() < -310:
    player.right(180)

Try adjusting the y-coordinate boundary checks to ensure they are aligned properly with the screen boundaries.

Code:
if player.ycor() > 330:
    player.sety(330)
elif player.ycor() < -330:
    player.sety(-330)

Also, ensure that the boundary checks for the enemy ships (aims[count]) are similarly adjusted if needed.
S.v Author
wrote...
A year ago Edited: A year ago, S.v
Code:
Ok so I've tried fiddling around with the barrier on the x axis but I still cant get it to work. I also tried your y axis idea but that just made it so that the player gets stuck at that point

if player.ycor() > 330:
    player.sety(330)
elif player.ycor() < -330:
    player.sety(-330)



Post Merge: A year ago

Ok so for some reason I think everything is fixed now what I did was split it up:

I did this
if player.ycor() > 310:
        player.right(180)
if player.ycor() < -310:
        player.right(180)

instead of

if player.ycor() > 310 or if player.ycor() < -310:
        player.right(180)
       
   
Anonymous
wrote...
A year ago
Nice Smiling Face with Open Mouth!

Coding is sometimes just guessing and checking until it works, then figuring out why it didn't work before
S.v Author
wrote...
A year ago
Yep!
New Topic      
Explore
Post your homework questions and get free online help from our incredible volunteers
  729 People Browsing
Related Images
  
 162
  
 2122
  
 135
Your Opinion