v_attila

Python game

Jun 21st, 2014
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. from tkinter import *
  2. from random import randrange
  3.  
  4. ### DEFS
  5.  
  6. # directions
  7. def move_ball_right_down():
  8.     global x, y, dx, dy
  9.  
  10.     x, y = x + dx, y + dy
  11.  
  12.     can.coords(ball, x-r, y-r, x+r, y+r)
  13.  
  14. def move_ball_left_up():
  15.     global x, y, dx, dy
  16.  
  17.     x, y = x - dx, y - dy
  18.    
  19.     can.coords(ball, x-r, y-r, x+r, y+r)
  20.  
  21.  
  22. def move_ball():
  23.     global flag, x, y, moving
  24.  
  25.     if moving == True:
  26.         move_ball_right_down()
  27.     if moving == False:
  28.         move_ball_left_up()
  29.        
  30.     if x <= 20 or y <= 20:
  31.         moving = True
  32.     if x >= 480 or y >= 480:
  33.         moving = False
  34.  
  35.  
  36.     if flag > 0:
  37.         win.after(50, move_ball)
  38.  
  39.  
  40. def click_ball(event):
  41.     global x, y, points, ball, r, dx, flag
  42.  
  43.     if flag == 1 and\
  44.        event.x <= x + r and event.x >= x - r and\
  45.        event.y <= y + r and event.y >= y - r:
  46.        
  47.         points = points + 1
  48.         lab2.configure(text = "Points: " + str(points))
  49.        
  50.         r = r - 1
  51.         dx = dx + 1
  52.        
  53.        
  54.         can.coords(ball, x-r, y-r, x+r, y+r)
  55.  
  56.         pal = ["red", "green", "white", "blue", "black", "yellow", "magenta"]
  57.         m = randrange(7)
  58.         color = pal[m]
  59.  
  60.         can.itemconfig(ball, fill = color)
  61.  
  62.     if points == 15:
  63.         lab2.configure(text = "Points: " + str(points) + " - You won!", fg = "green")
  64.         flag = 0
  65.  
  66.        
  67. def start():    # start the game
  68.     global flag
  69.     if flag == 0 and points < 15:
  70.         flag = 1
  71.         move_ball()
  72.  
  73. def pause():    # pause the game
  74.     global flag
  75.     flag = 0
  76.  
  77. def restart():
  78.     global points, r, x, y, flag, dx
  79.    
  80.     x, y = 250, 250
  81.     r = 20
  82.     points = 0
  83.     dx, dy = 10, 10
  84.     moving = True
  85.  
  86.     can.coords(ball, x-r, y-r, x+r, y+r)
  87.     can.itemconfig(ball, fill = "red")
  88.     lab2.configure(text = "Points: " + str(points))
  89.  
  90.     if flag == 0:
  91.         flag = 1
  92.         move_ball()
  93.    
  94.  
  95. ### VARS
  96. x, y = 250, 250
  97. dx, dy = 10, 10
  98. r = 20
  99.  
  100. flag = 0
  101. moving = True
  102.  
  103. points = 0
  104.  
  105. ### GUI
  106.     # create a WINDOW
  107. win = Tk()
  108. win.title("Clicky game")
  109.  
  110.     # create a CANVAS
  111. can = Canvas(win, height = 500, width = 500, bg = "light grey")
  112. can.grid(row = 3, column = 1, columnspan = 3, sticky = N)
  113. can.bind("<Button-1>", click_ball)
  114.  
  115.     # create LABELS
  116. lab  = Label(win, text = "Click on the 'Start' button, than catch the ball!")
  117. lab.grid(row = 1, column = 2, sticky = N, pady = 2)
  118.  
  119. lab2 = Label(win, text = "Points: 0", fg = "red")
  120. lab2.grid(row = 2, column = 2, sticky = N, pady = 2)
  121.  
  122.     # add a BALL
  123. ball = can.create_oval(x-r, y-r, x+r, y+r, fill = "red")
  124.  
  125.     # create BUTTONS
  126. but1 = Button(win, text = "Quit", command = win.destroy)
  127. but1.grid(row = 4, column = 3, sticky = E, padx = 5, pady = 5)
  128.  
  129. but2 = Button(win, text = "Start", command = start)
  130. but2.grid(row = 4, column = 1, sticky = W, padx = 10, pady = 5)
  131.                                                              
  132. but3 = Button(win, text = "Pause", command = pause)
  133. but3.grid(row = 4, column = 1, sticky = E, padx = 10, pady = 5)
  134.  
  135. but4 = Button(win, text = "Restart", command = restart)
  136. but4.grid(row = 4, column = 2, sticky = W, padx = 10, pady = 5)
  137.  
  138. win.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment