Advertisement
here2share

# Tk_url_img_pong.py

Aug 9th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. # Tk_url_img_pong.py
  2.  
  3. ### img dl param >>>
  4. url = r'http://i.imgur.com/ZFkQoE1.png'
  5.  
  6. from Tkinter import *
  7. from PIL import Image, ImageTk
  8. import urllib
  9. import io
  10. root = Tk()
  11. root.geometry('800x600+0+0')
  12.  
  13. imagebytes = urllib.urlopen(url).read()
  14. imagedata = io.BytesIO(imagebytes)
  15. imagePIL = Image.open(imagedata)
  16. img = ImageTk.PhotoImage(imagePIL)
  17.  
  18.  
  19. import time
  20. import random
  21.  
  22. width = 800
  23. height = 600
  24. radius = 19
  25. root.title("Pong")
  26. vel = [0, 0]
  27. ballvel = [16, 6]
  28. maxscore = 0
  29. score = 0
  30. canvas = Canvas(root, width=800, height=600)
  31. canvas.pack()
  32. ball = canvas.create_image(400, 300, image=img)
  33. paddle1 = canvas.create_line(0, 0, 0, 100, width=20)
  34. mid_paddle = int((canvas.coords(paddle1)[3]-canvas.coords(paddle1)[1])/2)
  35. ms = canvas.create_text(530, 20, text='Highest score = %s' % maxscore)
  36. s = canvas.create_text(265, 20, text='Score = %s' % score)
  37.  
  38.  
  39. def keypress(event):
  40.     global vel
  41.     acc = 15
  42.     if event.keysym == "Up":
  43.         vel[1] = -acc
  44.     if event.keysym == "Down":
  45.         vel[1] = acc
  46.  
  47.  
  48. def keyrelease(event):
  49.     global vel
  50.     if event.keysym == "Up":
  51.         vel[1] = 0
  52.     if event.keysym == "Down":
  53.         vel[1] = 0
  54.     if event.char == "q":
  55.         vel = False ### to exit
  56.  
  57.  
  58. def new_game():
  59.     canvas.update()
  60.     time.sleep(2)
  61.     global ball, ballvel, score, maxscore
  62.     if maxscore < score:
  63.         maxscore = score
  64.     score = 0
  65.     canvas.delete(ball)
  66.     ball = canvas.create_image(400, 300, image=img)
  67.     ballvel = [16, 6]
  68.  
  69.  
  70. root.bind("<Key>", keypress)
  71. root.bind("<KeyRelease>", keyrelease)
  72.  
  73. while vel:
  74.     time.sleep(0.025)
  75.  
  76.     canvas.move(ball, ballvel[0], ballvel[1])
  77.     canvas.move(paddle1, vel[0], vel[1])
  78.  
  79.     if canvas.coords(paddle1)[1] <= 0:
  80.         canvas.coords(paddle1, 0, 0, 0, 100)
  81.     if canvas.coords(paddle1)[3] >= height:
  82.         canvas.coords(paddle1, 0, height - 100, 0, height)
  83.  
  84.     if canvas.coords(ball)[0] <= radius:
  85.         if canvas.coords(ball)[1] < canvas.coords(paddle1)[1] or canvas.coords(ball)[1] > canvas.coords(paddle1)[3]:
  86.             new_game()
  87.         else:
  88.             umph = random.randrange(2,9)
  89.             redirect = int(((canvas.coords(paddle1)[1]+mid_paddle)-canvas.coords(ball)[1])/umph)*-1
  90.             if redirect < -2 or redirect > 2:
  91.                 ballvel[1] = redirect
  92.             ballvel[0] = -ballvel[0]
  93.             if ballvel[0] < 60:
  94.                 if ballvel[0] == abs(ballvel[0]):
  95.                     ballvel[0] += 1
  96.                 else:
  97.                     ballvel[0] -= 1
  98.             score += 1
  99.     if canvas.coords(ball)[0] >= width - radius:
  100.         ballvel[0] = -ballvel[0]
  101.  
  102.     if canvas.coords(ball)[1] <= radius or canvas.coords(ball)[1] >= height - radius:
  103.         ballvel[1] = -ballvel[1]
  104.     canvas.itemconfig(ms, text='Highest score = %s' % maxscore)
  105.     canvas.itemconfig(s, text='Score = %s' % score)
  106.     canvas.update()
  107.  
  108. root.destroy()
  109.  
  110. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement