Younus Rahman
Younus Rahman

Reputation: 11

Cannot move 2 turtles at same time in Python

I am new to Python. I'm trying to create a space game where two space ships fight each other. The user controls one and the other will fly and fire randomly. The user has to protect their self and fire on the other ship. If either ship can sucessefully hit the other 10 times, then that ship wins.

Problem: I can not see both ships at the same time. When I control one turtle, then the other turtle does not show. Please help.

from turtle import * 
import random
import pygame

class Application:
    space_ship = Turtle()
    window = Screen()
    window_X = 640
    window_Y = 640
    ship_x = space_ship.xcor()
    Clock = pygame.time.Clock()



    def __init__(self):
        self.game_config()
        self.enemy_config()


        
    def enemy_config(self):    
        enemy = Turtle()
        enemy.penup()
        enemy.speed(0)
        enemy.shapesize(2)
        enemy.setposition(0,-250)
        while True:
            self.Clock.tick(20)
            enemy.fd(10)

            if enemy.ycor() >= 340:
                enemy.right(50)
                enemy.sety(-320)
                
            elif enemy.ycor() <= -340:
                enemy.right(50)
                enemy.sety(320)


            elif enemy.xcor() >= 340:
                enemy.right(50)
                enemy.setx(-320)


            elif enemy.xcor() <= -340:
                enemy.right(50)
                enemy.setx(320)
            

    def movefd(self):       
        while True:
            self.Clock.tick(20)
            self.space_ship.fd(10)

            if self.space_ship.ycor() >= 340:
                self.space_ship.sety(-320)
            
            elif self.space_ship.ycor() <= -340:
                self.space_ship.sety(320)

            elif self.space_ship.xcor() >= 340:
                self.space_ship.setx(-320)

            elif self.space_ship.xcor() <= -340:
                self.space_ship.setx(320)

            

    def moverg(self):
        self.space_ship.right(15)

    def movelf(self):
        self.space_ship.left(15)


    def game_config (self):
        bgcolor("white")
        self.space_ship.shapesize(2)
        self.space_ship.speed(0)
        self.space_ship.color("green")
        self.window.setup(self.window_X,self.window_Y)
        self.space_ship.left(90)
        self.space_ship.penup()
        onkeypress(self.moverg, "Right")
        onkeypress(self.movelf, "Left")
        onkeypress(self.movefd, "Up")
        listen()
        done()
if __name__ == '__main__':
    Application()

Upvotes: 1

Views: 157

Answers (1)

cdlane
cdlane

Reputation: 41872

Problem: can not see both ship same time. when use control one turtle then another turtle does not show.

For me, the bigger problem is you've completely missed the point of object-oriented programming. Let's address both our problems with a complete rework of your code. But before we do, repeat over and over, "while True: has no business in an event-driven world like turtle."

from turtle import Screen, Turtle

class SpaceShip(Turtle):
    def __init__(self):
        super().__init__()

        self.shapesize(2)
        self.color("green")
        self.speed('fastest')
        self.penup()
        self.setheading(90)

    def movefd(self):
        self.forward(10)

        if self.ycor() >= 340:
            self.sety(-320)
        elif self.ycor() <= -340:
            self.sety(320)

        if self.xcor() >= 340:
            self.setx(-320)
        elif self.xcor() <= -340:
            self.setx(320)

    def moverg(self):
        self.right(15)

    def movelf(self):
        self.left(15)

class EnemyShip(SpaceShip):
    def __init__(self):
        super().__init__()

        self.hideturtle()
        self.color("black")
        self.setheading(-50)
        self.sety(-250)
        self.showturtle()

class Application:
    SCREEN_X = 640
    SCREEN_Y = 640

    def __init__(self):
        self.screen = Screen()
        self.screen.setup(self.SCREEN_X, self.SCREEN_Y)

        self.space_ship = SpaceShip()
        self.enemy = EnemyShip()

        self.screen.onkeypress(self.space_ship.moverg, "Right")
        self.screen.onkeypress(self.space_ship.movelf, "Left")
        self.screen.onkeypress(self.space_ship.movefd, "Up")
        self.screen.listen()

        self.movefd()

        self.screen.mainloop()

    def movefd(self):
        self.enemy.movefd()
        self.screen.ontimer(self.movefd, 100)

if __name__ == '__main__':
    Application()

Upvotes: 1

Related Questions