Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

python - The Canvas Object in list when called, gets an increased speed than the previous one, even without any such call (to increase speed)

So, this is an edited question (although it's not related to the previous one), well coming back to the topic, here's, my code:

# Importing the required Modules
from tkinter import *
import tkinter.messagebox as Alert
import numpy # ? Use import random if numpy not availale
import math

class Set_Game:
    
    # Defining the __init__ function of the class
    def __init__(self, Main_Frame):
        
        self.Start_Game()
        
        # Creating empty arrays for Enemies and Bullets
        self.Missiles = []
        self.Enemies = []
        
        # Setting a number of Enemies
        self.Number_Of_Enemies = 5
        
        # Setting the enemies' speed
        self.Enemies_Speed = [2, 2, 2, 2, 2]
        
        self.Missile_Speed = -3
        
        self.Set_Enemies()
        self.Move_Enemies()
        
        Window.after(3000, self.Move_Enemy_Down)
    
    def Move_Enemies(self):
        
        for Enemy in self.Enemies:
            
            x1, y1, x2, y2, x3, y3 = Can.coords(self.Enemies[self.Enemies.index(Enemy)])
            
            if x3 > 1000 or x1 < 0:
                self.Enemies_Speed[self.Enemies.index(Enemy)] = -self.Enemies_Speed[self.Enemies.index(Enemy)]
            
            Can.move(self.Enemies[self.Enemies.index(Enemy)], self.Enemies_Speed[self.Enemies.index(Enemy)], 0) 
            
            if y2 > 630:
                Alert.showinfo("Space Shooter", "The Game is Over!")
                Can.delete("all")
                return
            
        Window.after(18, self.Move_Enemies)
    
    # Set the Initial Position of Enemies    
    def Set_Enemies(self):
        for Enemy in range(self.Number_Of_Enemies):
            x = numpy.random.randint(960) # ? Use x = random.randint(0, 960) if numpy not available
            y = numpy.random.randint(20, 270) # ? Use y = random.randint(20, 270) if numpy not available
            Enemy = Can.create_polygon([x, y, x + 20, y + 20, x + 40, y], outline = "Red", fill = "Red")
            self.Enemies.append(Enemy)
        
    # Start the Game
    def Start_Game(self):
        # Placing the Widgets
        Can.pack()

        # Binding the Keys to move the Player
        Window.bind("<Key>", self.Move_Player)

    def Move_Player(self, event):
        Direction = event.keysym
        
        # Checking if the Left Arroe button id clicked
        if Direction == "Left":
            
            # Moving the Player to Left Direction
            Can.move(Player, -10, 0)
            
        # Checking if the Right Arroe button id clicked
        elif Direction == "Right":
            
            # Moving the Player to Left Direction
            Can.move(Player, 10, 0)
        
        elif Direction == "Up":
            
            self.Fire()    
            
    def Move_Enemy_Down(self):
    
        for Enemy in self.Enemies:
            Can.move(Enemy, 0, 30)
            
        Window.after(3000, self.Move_Enemy_Down)

    def Fire(self):
        
        x1, y1, x2, y2, x3, y3 = Can.coords(Player)
        
        Missile = Can.create_oval(x2 - 3, y2 - 6, x2 + 3, y2, outline = "Yellow", fill = "Yellow")
        self.Missiles.append(Missile)
        
        self.Move_Missile()
        
    def Move_Missile(self):
            
        for Missile in self.Missiles:
            
            Can.move(Missile, 0, self.Missile_Speed)
            
            Mx1, My1, Mx2, My2 = Can.coords(Missile)
            
            if My1 < 0:
                Can.delete(Missile)
                self.Missiles.pop(self.Missiles.index(Missile))
            
            for Enemy in self.Enemies:
                Ex1, Ey1, Ex2, Ey2, Ex3, Ey3 = Can.coords(Enemy) 
                
                if My1 >= Ey1 and My1 <= Ey3 or My1 >= Ey2 and My1 <= Ey3:
                    if Mx1 >= Ex1 and Mx1 <= Ex3 or Mx2 >= Ex1 and Mx2 >= Ex3:
                        Can.delete(Missile)
                        self.Missiles.pop(self.Missiles.index(Missile))
                        
                        Can.delete(Enemy)
                        self.Enemies.pop(self.Enemies.index(Enemy))
                        self.Enemies_Speed.pop(self.Enemies.index(Enemy))   # ValueError: (1-5) is not in list
            
        Window.after(18, self.Move_Missile)
    
# Initializing Game Window
Window = Tk()
Window.title("Space Shooter")

# Defining the Game Canvas
Can = Canvas(Window, background = "Black", height = 630, width = 1000)
Player = Can.create_polygon(280, 620, 300, 600, 320, 620, fill = "Blue", outline = "Blue")

# Calling the Set_Game Class
Game = Set_Game(Window)

# Adding Mainloop
Window.mainloop()

Now I have a of problems here, the speed of the missile gets increased each time than the previous one, after firing nearly 20 bullets, it catches in insane speed. I don't know why it's happening, but I have a suspect in the

for Missile in self.Missiles:

line. Any help would be appreciated, thanks in advance!

question from:https://stackoverflow.com/questions/65516815/the-canvas-object-in-list-when-called-gets-an-increased-speed-than-the-previous

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...