Guest User

Untitled

a guest
Mar 31st, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. import random
  4. import math
  5. import threading
  6. import sys
  7. import time
  8. from time import localtime, strftime
  9.  
  10. #options
  11. number_of_blocks=15
  12. window_open=True
  13. speed=250
  14.  
  15. if len(sys.argv)!=3:
  16.     print "Enter valid arguments, like 'python file.py 12:56 30', where 12:56 - time, when you want run alarm, 30 - seconds of play"
  17.     sys.exit()
  18.    
  19. time_alarm=sys.argv[1]
  20. time_alarm=time_alarm.split(':')
  21. hours=time_alarm[0]
  22. minutes=time_alarm[1]
  23. time_of_play=int(sys.argv[2])*1000
  24.  
  25. #init
  26. pygame.init()
  27. screen = pygame.display.set_mode((640,480))
  28. pygame.display.set_caption('Wake up, mazafaka')
  29.  
  30. def wait():
  31.     """wait for time"""
  32.     while window_open:
  33.         if strftime("%H", localtime())>=hours and strftime("%M", localtime()) >= minutes:
  34.             thrd()
  35.             break
  36.         else: time.sleep(30)
  37.  
  38. def music():
  39.     try:
  40.         pygame.mixer.music.load('music.mp3')
  41.     except:
  42.         print "no music file"
  43.         sys.exit()
  44.    
  45.     pygame.mixer.music.set_volume(1)
  46.     pygame.mixer.music.play()
  47.    
  48. def main():
  49.     clock=pygame.time.Clock()
  50.     clock2=pygame.time.Clock()
  51.     alltime=0
  52.    
  53.     data=[ random.randint(0,360) for i in range(number_of_blocks)]
  54.     colors=[(random.randint(0,255),random.randint(0,255),random.randint(0,255)) for i in range(number_of_blocks)]
  55.     rects=[Rect(320,240,random.randint(20,70),random.randint(20,70)) for i in range(number_of_blocks)]
  56.    
  57.     #main cicle    
  58.     while window_open:
  59.         #x,y coordinats of our rectangle
  60.         for event in pygame.event.get():
  61.             pygame.mouse.set_visible(0)
  62.             x,y=pygame.mouse.get_pos()
  63.         try:
  64.             me=Rect(x,y,30,30) #our rectangle and his position
  65.         except:
  66.             x,y=-100,-100 #the crutch
  67.             me=Rect(x,y,30,30)
  68.  
  69.         #make main window and fill him
  70.         screen.fill(pygame.Color(0, 0, 0, 0))
  71.         milli = clock.tick(40)
  72.         seconds = milli / 1000.0
  73.         dr= int(speed*seconds)
  74.  
  75.         for i in range(number_of_blocks):
  76.             rects[i].x+=math.cos(data[i])*dr
  77.             rects[i].y+=math.sin(data[i])*dr
  78.             if rects[i].x>640:
  79.                 data[i]=random.randint(140,230)
  80.             if rects[i].y>480:
  81.                 data[i]=random.randint(50,150)
  82.             if rects[i].x<0:
  83.                 data[i]=random.randint(-30,60)
  84.             if rects[i].y<0:
  85.                 data[i]=random.randint(-100,-30)
  86.             #draw other blocks    
  87.             pygame.draw.rect(screen,colors[i],rects[i])
  88.         #draw our block
  89.         pygame.draw.rect(screen,(255,0,0),me)
  90.         pygame.display.update()
  91.        
  92.         if me.collidelistall(rects):
  93.             pygame.time.wait(100)
  94.             main()
  95.  
  96.         if pygame.mixer.music.get_busy() == False:
  97.             pygame.mixer.music.rewind()
  98.             pygame.mixer.music.play()
  99.            
  100.         alltime+=clock2.tick()
  101.         if alltime>time_of_play:sys.exit()
  102. def thrd():
  103.     thread1=threading.Thread(target=music)
  104.     thread2=threading.Thread(target=main)
  105.     thread1.start()
  106.     thread2.start()
  107.    
  108. def wait():
  109.     while window_open:
  110.         if strftime("%H", localtime())>=hours and strftime("%M", localtime()) >= minutes:
  111.             thrd()
  112.             break
  113.  
  114.         else: time.sleep(30)
  115. if __name__=='__main__':
  116.     wait()
Advertisement
Add Comment
Please, Sign In to add comment