[PYTHON] Final product

Challenged with the final product

・ Soccer PK game ・ Amidakuji etc ... However, ...


I gave up because I couldn't make it with my own ability. I reconsidered and came up with


** Break the block with three balls **



#coding:utf-8
import tkinter as tk
class Ball:
    def __init__(self, x, y, dx, dy, color):
        self.x = x
        self.y = y
        self.dx = dx
        self.dy = dy
        self.color = color

    def move(self, canvas):
        #Erase the current circle
        self.erase(canvas)
        #Move the X and y coordinates
        self.x = self.x + self.dx
        self.y = self.y + self.dy
        #Draw a circle at the next position
        self.draw(canvas)
        #If it is over the edge, turn it in the opposite direction
        if (self.x >= canvas.winfo_width()):
           self.dx = -1
        if (self.x <= 0):
           self.dx = 1
        if (self.y >= canvas.winfo_height()):
           self.dy = -1
        if (self.y <= 0):
           self.dy = 1

    def erase(self, canvas):
        canvas.create_oval(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill="white", width=0)

    def draw(self, canvas):
        canvas.create_oval(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill=self.color, width=0)

class Rectangle(Ball):
    def erase(self, canvas):
        canvas.create_rectangle(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill="white", width=0)

    def draw(self, canvas):
        canvas.create_rectangle(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill=self.color, width=0)

class Triangle(Ball):
    def erase(self, canvas):
        canvas.create_rectangle(self.x - 20, self.y - 20, self.x + 20,self.y + 20, fill="white", width=0)

    def draw(self, canvas):
        canvas.create_polygon (self.x, self.y - 20, self.x + 20,self.y + 20, self.x -20, self.y + 20, fill=self.color, width=0)

#Prepare three circles together
balls = [
    Ball(400, 300, 1, 1, "green"),
    Ball(200, 100, -1, 1, "green"),
    Ball(100, 200, 1, -1, "green")
]
def loop():
  #move
  for b in balls:
      b.move(canvas)

 #Block to break
 class Block:
        w_x = 100 #Block width(x coordinate)
        w_y = 30 #Block width(y coordinate)
        global dy, score #Since we want to change the movement amount and score of the ball class in the event of a collision, we make a global declaration.

        #Block switch. 1 is ON,0 is OFF
        block_list =[[1,1,1,1,1,1,1,1,1,1,1,1], # j = 0 , i = 0 ~ 11
                     [1,1,1,1,1,1,1,1,1,1,1,1], # j = 1 , i = 0 ~ 11
                     [1,1,1,1,1,1,1,1,1,1,1,1]] # j = 2 , i = 0 ~11 Row / column order
        def draw(self):
            for i in range(6):
                for j in range(3):
                        cv.create_rectangle(i*self.w_x, j*self.w_y, (i+1)*self.w_x, (j+1)*self.w_y, fill = "orange", tag = "block"+str(j)+str(i))

        def reflect(self):
            for i in range(12):
                for j in range(3):
                    #The ball reflects from above
                    if (ball.y-ball.w < (j+1)*self.w_y #The ball is below the block
                        and i*self.w_x < ball.x < (i+1)*self.w_x #The ball is sandwiched between the left and right sides of the block
                        and self.block_list[j][i] == 1): #Switch is 1
                            ball.dy *= -1 #Reflect
                            cv.delete("block"+str(j)+str(i)) #Erase the drawing of the block
                            self.block_list[j][i] = 0 #turn off the switch
                            score.score += 1 #Add points to the score
                            score.delete() #Update (delete) score-Generate)
                            score.draw()

  #again
  root.after(10,loop)

#Draw a window
root = tk.Tk()
root.geometry("800x600")

```pyhon

# Game over
    def gameover():
        global w, dx, dy
        if ball.y + ball.w > win_height :
            cv.delete("paddle")
            cv.delete("ball")
            cv.create_text(win_center_x, win_center_y, text = "GAME OVER(T_T)", font = ('FixedSys', 40))
            ball.w = 0
            ball.dx = 0
            ball.dy = 0

 #Game clear
    def gameclear():
        global w, dx, dy
        if score.score == 18 :
            cv.delete("paddle")
            cv.delete("ball")
            cv.create_text(win_center_x, win_center_y, text = "GAME CLEAR(^0^)", font = ('FixedSys', 40))
            ball.w = 0
            ball.dx = 0
            ball.dy = 0

# Put canvas
canvas =tk.Canvas(root, width =800, height =600, bg="#fff")
canvas.place(x = 0, y = 0)

# Set the timer
root.after(10, loop)

root.mainloop()

2020-07-09.png


#Summary By doing this, you can break the blocks by turning all the balls, triangles, and squares in the lesson into balls, and then programming the blocks to break. I will try again the soccer pk game and Amidakuji that I couldn't do.


#reference https://qiita.com/Yt330110713/items/ed6beb74c02515b35248

Recommended Posts

Final product
Final task