thinkpad20

screen saver kind of thing with pygame

Jun 15th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import pygame, sys, random, time, math
  2. from pygame.locals import *
  3.  
  4. pygame.init()
  5.  
  6. width1 = 500
  7. height1 = 340
  8.  
  9. scr1 = pygame.display.set_mode((width1,height1), 0, 32)
  10.  
  11. bimage = "gradient.jpg"
  12. mimage = "ball.png"
  13.  
  14. background = pygame.image.load(bimage).convert()
  15. mouse_c = pygame.image.load(mimage).convert_alpha()
  16.  
  17. ball = [0,0]
  18. def generate_inc():
  19.     theta = random.random() * 2 * math.pi
  20.     x_inc = math.cos(theta)
  21.     y_inc = math.sin(theta)
  22.     print "Generated new increment:", (x_inc, y_inc)
  23.     return x_inc, y_inc
  24.  
  25. x_inc, y_inc = generate_inc()
  26.  
  27. count = 0
  28.  
  29. while True:
  30.     for event in pygame.event.get():
  31.         if event.type == QUIT:
  32.             pygame.quit()
  33.             sys.exit()
  34.         if event.type == KEYDOWN:
  35.             x_inc, y_inc = generate_inc()
  36.  
  37.     if count == 2000:
  38.         count = 0
  39.         x_inc, y_inc = generate_inc()
  40.  
  41.  
  42.     scr1.blit(background, (0,0))
  43.     scr1.blit(mouse_c, tuple(ball))
  44.  
  45.     ball[0] = (ball[0] + x_inc)# % width1
  46.     if ball[0] >= width1 or ball[0] <= 0:
  47.         x_inc = -1 * x_inc
  48.     ball[1] = (ball[1] + y_inc)# % height1
  49.     if ball[1] >= height1 or ball[1] <= 0:
  50.         y_inc = -1 * y_inc
  51.  
  52.     pygame.display.update()
  53.     count += 1
  54.    
  55.  
  56. ##from Tkinter import *
  57. ##
  58. ##master = Tk()
  59. ##
  60. ##w = Canvas(master, width = 200, height = 100)
  61. ##w.pack()
  62. ##
  63. ##w.create_line(0, 0, 200, 100)
  64. ##w.create_line(0, 100, 200, 0, fill="red", dash = (4, 4))
  65. ##
  66. ##
  67. ##
  68. ##w.create_rectangle(50,25,150,75, fill="blue")
  69. ##
  70. ##mainloop()
Advertisement
Add Comment
Please, Sign In to add comment