Python Forum
changing animation speed using buttons in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing animation speed using buttons in python
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Virtual Env changing mysql connection string in python Fredesetes 0 324 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,539 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,290 Jan-21-2022, 06:24 PM
Last Post: mcva
  changing Python files to .exe alok 2 2,185 Jul-20-2021, 02:49 PM
Last Post: alok
  Changing Index of 2 List in python giddyhead 0 1,640 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  Automating to run python script 100 times by changing parameters pmt 1 2,562 Dec-29-2020, 10:31 AM
Last Post: andydoc
  Changing to new Python formatting example leodavinci1990 3 1,988 Sep-22-2020, 07:36 PM
Last Post: yaythomas
  Python module speed or python speed in general Enrique6 1 1,792 May-04-2020, 06:21 PM
Last Post: micseydel
  Problem after changing Python directory Souila7 1 2,109 Jan-25-2020, 05:06 PM
Last Post: ThiefOfTime
  Changing axis graduation matplotlib 3D animation axerousso 0 2,108 Jan-09-2020, 08:25 PM
Last Post: axerousso

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020