Example of drawing and loading image animation effects implemented by Python3,

Source: Internet
Author: User

Example of drawing and loading image animation effects implemented by Python3,

This article describes how to draw and attach animated images in Python3. We will share this with you for your reference. The details are as follows:

#__ * _ Coding: UTF-8 _ * __# python3import timefrom tkinter import * def moveImage (event): if event is the function to be bound to the move of the logo.gif image. keysym = 'up': canvas. move (,-3) # move the object whose ID is 1, so that the X-axis is 0, and the X-axis is 3 elif event. keysym = 'low': canvas. move (1, 0, + 3) elif event. keysym = 'left': canvas. move (1,-3, 0) elif event. keysym = 'right': canvas. move (1, 3, 0) tk. update () time. sleep (0.05) def changeColor (event): if event. keysym = 'up': canvas. itemconfig (pg, fill = 'blue') # fill in things whose ID is pg and fill in bluetk = Tk () # window canvas = Canvas (tk, width = 400, height = 400) # canvas. pack () # myImage = PhotoImage (file = 'C: \ Users \ lai \ Desktop \ logo.gif ') # The image format must be gif im = canvas. create_image (0, 0, anchor = NW, image = myImage) # Load image pg = canvas. create_polygon (10, 10, 10, 60, 50, 35, fill = 'red') # create a triangle print (im); print (pg) # display the image and triangle IDcanvas. bind_all ('<KeyPress-Up>', moveImage) # bind the direction key upcanvas. bind_all ('<KeyPress-Down>', moveImage) canvas. bind_all ('<KeyPress-Left>', moveImage) canvas. bind_all ('<KeyPress-Right>', moveImage) # canvas. bind_all ('<KeyPress-Up>', changeColor)

Running result:

You can move an image after pressing the Left or Right key.

Baffle game example

#__* _ Coding: UTF-8 _ * __# python3from tkinter import * import randomimport timeclass Ball: # def _ init _ (self, canvas, paddle, color): self. canvas = canvas # pass the canvas value self. paddle = paddle # pass the baffle into self. id = canvas. create_oval (10, 10, 25, fill = color) # draw an ellipse and save its ID self. canvas. move (self. id, 245,100) start = [-3,-2,-, 2, 3] random. shuffle (start) # randomization list self. x = start [0] self. y =-3 self. canvas_heigh = self. canvas. winfo_height () # obtain the window height and save self. canvas_width = self. canvas. winfo_width () def draw (self): self. canvas. move (self. id, self. x, self. y) pos = self. canvas. coords (self. id) # Return the current coordinates of the image represented by the corresponding ID (the coordinates in the upper left and upper right corner) # so that the ball does not exceed the window pad = self. canvas. coords (self. paddle. id) # obtain the coordinates of the baffle if pos [1] <= 0: self. y = 3 if pos [3]> = self. canvas_heigh or (pos [3]> = pad [1] and pos [2]> = pad [0] and pos [2] <= pad [2]): self. y =-3 if pos [0] <= 0: self. x = 3 if pos [2]> = self. canvas_width: self. x =-3 class Paddle: # baffle class def _ init _ (self, canvas, color): self. canvas = canvas self. color = color self. id = canvas. create_rectangle (, fill = color) self. canvas. move (self. (id, 200,300) self. canvas_width = self. canvas. winfo_width () self. l = 0 self. r = 0 def draw (self): pos = self. canvas. coords (self. id) if pos [0] <= 0: self. l = 0 if pos [2]> = self. canvas_width: self. r = 0 def turn_left (self, event): self. canvas. move (self. id, self. l, 0) self. l =-20 def turn_right (self, event): self. canvas. move (self. id, self. r, 0) self. r = 20tk = Tk () tk. title ('game') tk. resizable () # The window size cannot be adjusted tk. wm_attributes ('-topmost', 1) # Place the window containing the canvas in front of another window Canvas = canvas (tk, width = 500, height = 400, bd = 0, highlightthickness = 0) # Remove the border canvas from the last two parameters. pack () tk. update () paddle = Paddle (canvas, 'blue') ball = Ball (canvas, paddle, 'red') canvas. bind_all ('<KeyPress-Left>', paddle. turn_left) # bind the arrow key canvas. bind_all ('<KeyPress-Right>', paddle. turn_right) while 1: ball. draw () paddle. draw () tk. update_idletasks () # redraw screen tk quickly. update () time. sleep (0.01)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.