By learning python programming through the lectures so far, I wanted to make something that works easily, so I made this.
qiita.rb
import tkinter as tk
import math
class Application(tk.Frame):
def __init__(self,master):
super().__init__(master)
self.pack()
self.width=self.height=500
master.geometry(str(self.width)+"x"+str(self.height))
master.title("Chasing game")
def main():
win = tk.Tk()
app = Application(master = win)
app.mainloop()
if __name__ == "__main__":
main()
qiita.rb
def enemyVec(self,player,enemy,speed):#Enemy movement(x,y)Returns a vector of
rad=np.arctan((player.y-enemy.y)/(player.x-enemy.x))#Orientation (calculation of angle)
if player.x-enemy.x >= 0:
vx=np.cos(?)*speed
vy=np.sin(?)*speed
else:
vx=np.cos(?)*(-1*speed)
vy=np.sin(?)*(-1*speed)
return [vx,vy]
I'm passing a player object and an enemy object as arguments. This is because each center coordinate is used in the calculation. Now, I think the calculation of the orientation of these two objects gets a little confusing. In other words, it is the calculation of the angle between two points. Since we know the coordinates of the two points, we can find the x and y components of the vector. (Example) In the case of A (x1, y1), B (x2, y2) AB→=x2−x1y2−y1
And the angle θ between the x-axis and the vector can be expressed using the inverse function of tan below. θ=tan−1(yx) Since the range of tanθ is -π / 2 to π / 2, the case is divided by the positive or negative of the difference between the x coordinate of player and enemy, and the direction is changed by multiplying speed by -1.
qiita.rb
import tkinter as tk
import math
class Circle():
def __init__(self,canvas,x,y,r,color,tag=None):
self.canvas = canvas
self.x = x #X coordinate of the center of the circle
self.y = y #Y coordinate of the center of the circle
self.r = r #Radius of circle
self.color = color
self.tag = tag
def createCircle(self):
self.canvas.create_oval(self.x-self.r,self.y-self.r,self.x+self.r,self.y+self.r,fill=self.color,tag=self.tag)
class Application(tk.Frame):
def __init__(self,master):
super().__init__(master)
self.pack()
self.width=self.height=500
master.geometry(str(self.width)+"x"+str(self.height))
master.title("Chasing game")
self.canvas = tk.Canvas(master,width=self.width,height=self.height,bg="black")
self.canvas.pack()
self.player = Circle(self.canvas,250,250,30,"red","player") #Instance player generation
self.enemy = Circle(self.canvas,0,0,30,"blue","enemy") #Generating an instance enemy
self.canvas.bind("<Motion>",self.mouseEvent)
self.master.after(50,self.update)
def update(self):
if self.judgeflag(self.player,self.enemy):
eV = self.enemyVec(self.player,self.enemy,10)
self.enemy.x += eV[0]
self.enemy.y += eV[1]
self.canvas.delete("player")
self.canvas.delete("enemy")
self.player.createCircle()
self.enemy.createCircle()
self.master.after(50,self.update)
def enemyVec(self,player,enemy,speed): #Enemy movement(x,y)Returns a vector of
rad=math.atan((player.y-enemy.y)/(player.x-enemy.x)) #Orientation (calculation of angle)
if player.x-enemy.x >= 0:
vx=math.cos(rad)*speed
vy=math.sin(rad)*speed
else:
vx=math.cos(rad)*(-1*speed)
vy=math.sin(rad)*(-1*speed)
return [vx,vy]
def judgeflag(self,player,enemy): #collision detection
if math.sqrt((player.x-enemy.x)**2+(player.y-enemy.y)**2) > player.r+enemy.r: return True
else: return False
def mouseEvent(self,event):
self.player.x = event.x
self.player.y = event.y
def main():
win = tk.Tk()
app = Application(master=win)
app.mainloop()
if __name__ == "__main__":
main()
Since it did not work with the original source code, I made a trial and error by referring to Python standard library. Almost the source code is based on [Python] Tkinter's 70-line GUI app "Chasing game". I was able to deepen my understanding of this program during this production. I still feel that it takes a lot of time and experience to make things from scratch myself, so I will study during the summer vacation. I can experience the sense of accomplishment that I did not feel when I was copying the textbook, and I would like to continue learning by turning the sense of accomplishment into motivation.
I want to make my own site someday. Recently, I was thinking of going on a trip and opening the site to make a reservation, but every time I made a mistake, I returned to the beginning, so I found it difficult to use. So, through future learning, I would like to make something from scratch that many people can use.
-The easiest python introductory class Fumitaka Osawa [Author]
-[Python] GUI app "Chasing game" made with 70 lines by Tkinter
Recommended Posts