![]() |
changing animation speed using buttons in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: changing animation speed using buttons in python (/thread-27855.html) |
changing animation speed using buttons in python - microwave - Jun-24-2020 I need help with animation speed in my simple game similar to pong. I am currently trying to have two buttons which will change the speed of "an egg". My plan for the game is that if you click on the "fast game", "the egg" will move downwards in 10 miliseconds and if you click on the "slow game", "the egg" will move downwards in 100 miliseconds. And this is the code I am currently trying to change: import tkinter from random import * canvas = tkinter.Canvas(width=600, heigh=300, bg="lightgreen") canvas.pack() import winsound def draw_egg(x, y): canvas.create_oval(x-5, y-7,x+5, y+7, fill='wheat') def draw_bowl(x, y): canvas.create_polygon (x, y-10, x+50,y-10, x+40,y+10, x+10, y+10, fill='brown',) def count_points(number_of_points): canvas.create_text(100, 10, text='Number of points:') canvas.create_text(200, 10, text=number_of_points) def lives (number_of_lives): canvas.create_text(400, 10, text='Number of lives:', fill="red") canvas.create_text(500, 10, text=number_of_lives, fill="red") def fast_game(): miliseconds=10 egg_falling() def slow_game(): miliseconds=100 egg_falling() def egg_falling(): canvas.delete('all') global egg_x, egg_y, number_of_points, number_of_lives egg_y = egg_y + 5 draw_egg(egg_x, egg_y) draw_bowl(bowl_x, bowl_y) count_points(number_of_points) lives(number_of_lives) if egg_y>300: egg_x = randrange(300) egg_y = 20 number_of_points = number_of_points number_of_lives = number_of_lives -1 winsound.PlaySound("SystemHand", winsound.SND_ALIAS) if bowl_x<egg_x<bowl_x+50 and bowl_y-10<egg_y<bowl_y: number_of_points= number_of_points + 10 egg_x = randrange(600) egg_y = 20 winsound.PlaySound("SystemExit", winsound.SND_ALIAS) if number_of_lives < 1: game_over() canvas.after(milisekundy, vajco_pada) def game_over(): canvas.delete('all') vajco_y = 0 canvas.create_text (300, 50, text= "GAME OVER", font= "arial 60") canvas.create_text (200, 200, text= "SCORE:",) canvas.create_text (400, 200, text= number_of_lives,) def mouse(coordinates): global number_of_points, bowl_x, number_of_lives, bowl_y bowl_x= coordinates.x bowl_y canvas.delete('all') draw_egg(egg_x, egg_y) draw_bowl(bowl_x, bowl_y) count_points(number_of_points) lives (number_of_lives) if number_of_points < 1: game_over() number_of_points = 0 number_of_lives = 3 egg_x = randrange(300) egg_y = 20 bowl_x = 150 bowl_y = 250 miliseconds=50 button2 = tkinter.Button(text='fast game', command = fast_game) button2.pack() button1 = tkinter.Button(text='slow game', command = slow_game) button1.pack() canvas.bind('<Motion>', mouse) RE: changing animation speed using buttons in python - GOTO10 - Jun-24-2020 What issue are you encountering? There are a few obvious problems with your code that may have come from translating to English. You have "milisekundy" in some lines and "miliseconds" in others. You also have "vajco" in place of "egg" in a few spots. Your mouse() function calls game_over() based on number_of_points instead of number_of_lives. Your code looks like it would probably work with a few corrections. If you are getting an error message or an unexpected result, you need to tell us what's happening before we can help you figure it out. |