Untitled
raw download clone
TEXT
views 50
,
size 2135 b
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
import random
import math

def gui():
    # Окно и холст
    
    WIDTH = 400
    HEIGHT = 400
    
    global WINDOW
    WINDOW=Tk()
    
    global c
    c = Canvas(WINDOW, width=WIDTH, height=HEIGHT, bg="white")
    c.pack()
    
    # Оформление
    c.create_rectangle(0,0,400,400,fill="#404040")
    c.create_rectangle(5,5,395,395, fill="#777777")
    # Дисплей
    c.create_rectangle(305,10,390,55, fill="#404040")
    c.create_rectangle(307,12,388,53, fill="#365834");
    
def init_game():
    # Шарик
    global ball
    DEF_BALL_POS=[200,350]
    RADIUS=20
    
    ball = c.create_oval(DEF_BALL_POS[0]-RADIUS, DEF_BALL_POS[1]-RADIUS,
                  DEF_BALL_POS[0]+RADIUS, DEF_BALL_POS[1]+RADIUS,
                  fill="#547650", outline="black")
    
    # Платформа
    global platform
    platform = c.create_rectangle(140,380,260,395, fill="#391D19")
 
    
def keyboard():
    
    # Движение платформы влево при нажатии клвиши <left>
    def move_left(event):
        
        if ((c.coords(platform))[0]) >= 5:
            c.move(platform,-30,0)
         
        print("Current coords:\n", c.coords(platform))
            
    # Движение платформы вправо при нажатии клавиши <right>    
    def move_right(event):
        
        if ((c.coords(platform))[2]) <= 400:
            c.move(platform,30,0)
       
        print("Current coords:\n", c.coords(platform))
        
    WINDOW.bind('<Left>',  move_left)
    WINDOW.bind('<Right>', move_right)  
    #WINDOW.bind('<Escape>', restart_game)    

def game():
    
    DEGR=random.randint(0,135)
    
    def move_ball():
        
        
        
        c.move(ball, 1*math.cos(math.radians(DEGR)), -1*math.sin(math.radians(DEGR)))
        
        c.after(10, move_ball)
        pass   
            
    move_ball()
        
        
gui()
init_game()
keyboard()
game()
mainloop()
close fullscreen
Login or Register to edit or fork this paste. It's free.