Advertisement
Guest User

Untitled

a guest
May 28th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. # Color the Dot
  2. #there is one moving dot and you need to click it as many times as possible within 10 seconds.
  3.  
  4. import pygame, sys, time, math
  5. from pygame.locals import *
  6.  
  7. # User-defined classes
  8. class Dot:
  9. bgColor=pygame.Color('black')
  10. fgColor=pygame.Color('white')
  11. notSelected=0
  12. selected=1
  13. fontSize= 60
  14. maxTime= 9
  15.  
  16. def __init__(self,color,radius,center,speed):
  17. self.color=color
  18. self.center=center
  19. self.radius=radius
  20. self.speed=speed
  21. self.selectCount=0
  22. self.state=Dot.notSelected
  23.  
  24. def draw(self,surface):
  25. selectedColor=pygame.Color('red')
  26. if self.state == Dot.notSelected:
  27. pygame.draw.circle(surface,self.color,self.center,self.radius)
  28. else:
  29. pygame.draw.circle(surface,selectedColor,self.center,self.radius)
  30.  
  31. def eraseDot(self,Surface):
  32. pygame.draw.circle(surface,Dot.bgColor,self.center,self.radius)
  33.  
  34. def moveDot(self,surface):
  35. boxSize=surface.get_size()
  36. for coord in range(0,2):
  37. self.center[coord]=self.center[coord]+self.speed[coord]
  38. if self.center[coord]<self.radius:
  39. self.speed[coord]=-self.speed[coord]
  40. self.center[coord]=self.center[coord]+2*(self.radius-self.center[coord])
  41. if self.center[coord]+self.radius>boxSize[coord]:
  42. self.speed[coord]=-self.speed[coord]
  43. self.center[coord]=self.center[coord]-2*(self.center[coord]+self.radius-boxSize[coord])
  44.  
  45. def containsPoint(self,position):
  46. distanceX=position[0]-self.center[0]
  47. distanceY=position[1]-self.center[1]
  48. distance=math.sqrt(distanceX**2+distanceY**2)
  49. return distance < self.radius
  50.  
  51. def handleMouseUp(self,position,surface):
  52. if self.containsPoint(position):
  53. self.state=Dot.selected
  54. self.selectCount=self.selectCount+1
  55. else:
  56. self.state=Dot.notSelected
  57.  
  58. def drawScore(self,surface):
  59. displayText=('Score: '+ str(self.selectCount))
  60. font=pygame.font.SysFont(None,Dot.fontSize,True)
  61. textSurface=font.render(displayText,True,Dot.fgColor,Dot.bgColor)
  62. surface.blit(textSurface,(0,0))
  63.  
  64. def calculateTime(self):
  65. elapsedTime=Dot.maxTime-pygame.time.get_ticks()//1000
  66. return elapsedTime
  67.  
  68. def drawTime(self,surface):
  69. displayString1= 'Time: '+str(self.calculateTime())
  70. displayString2= 'Time: 0'
  71. font=pygame.font.SysFont(None,Dot.fontSize,True)
  72. if self.calculateTime()<0:
  73. textSurface=font.render(displayString2,True,Dot.fgColor,Dot.bgColor)
  74. else:
  75. textSurface=font.render(displayString1,True,Dot.fgColor,Dot.bgColor)
  76.  
  77. surface.blit(textSurface,(0,(surface.get_height()-textSurface.get_height())))
  78.  
  79.  
  80. def update(aDot, surface):
  81.  
  82. if (aDot.calculateTime()<0): # check if the game is over
  83. return True
  84. else: # continue the game
  85. aDot.eraseDot(surface)
  86. aDot.moveDot(surface)
  87. aDot.draw(surface)
  88. aDot.drawScore(surface)
  89. aDot.drawTime(surface)
  90. return False
  91.  
  92. # Main program
  93.  
  94. # Initialize pygame
  95. pygame.init()
  96.  
  97. # Set window size and title, and frame delay
  98. surfaceSize = (500, 400) # example window size
  99. windowTitle = 'color the dot' # example title
  100. frameDelay = 0.02 # smaller is faster game
  101.  
  102. # Create the window
  103. surface = pygame.display.set_mode(surfaceSize, 0, 0)
  104. pygame.display.set_caption(windowTitle)
  105.  
  106. # create and initialize objects
  107. gameOver = False
  108.  
  109. center = [200, 200] # example - replace
  110. color= pygame.Color('blue')
  111. radius= 40
  112. speed= [1,2]
  113. aDot=Dot(color,radius,center,speed)
  114. # Draw objects
  115. aDot.draw(surface)
  116.  
  117.  
  118. # Refresh the display
  119. pygame.display.update()
  120.  
  121. # Loop forever
  122. while True:
  123. # Handle events
  124. for event in pygame.event.get():
  125. if event.type == QUIT:
  126. pygame.quit()
  127. sys.exit()
  128. # Handle additional events
  129. if event.type== MOUSEBUTTONUP and not gameOver:
  130. aDot.handleMouseUp(event.pos,surface)
  131.  
  132. # Update and draw objects for the next frame
  133. gameOver = update(aDot, surface)
  134.  
  135. # Refresh the display
  136. pygame.display.update()
  137.  
  138. # Set the frame speed by pausing between frames
  139. time.sleep(frameDelay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement