Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Color the Dot
- #there is one moving dot and you need to click it as many times as possible within 10 seconds.
- import pygame, sys, time, math
- from pygame.locals import *
- # User-defined classes
- class Dot:
- bgColor=pygame.Color('black')
- fgColor=pygame.Color('white')
- notSelected=0
- selected=1
- fontSize= 60
- maxTime= 9
- def __init__(self,color,radius,center,speed):
- self.color=color
- self.center=center
- self.radius=radius
- self.speed=speed
- self.selectCount=0
- self.state=Dot.notSelected
- def draw(self,surface):
- selectedColor=pygame.Color('red')
- if self.state == Dot.notSelected:
- pygame.draw.circle(surface,self.color,self.center,self.radius)
- else:
- pygame.draw.circle(surface,selectedColor,self.center,self.radius)
- def eraseDot(self,Surface):
- pygame.draw.circle(surface,Dot.bgColor,self.center,self.radius)
- def moveDot(self,surface):
- boxSize=surface.get_size()
- for coord in range(0,2):
- self.center[coord]=self.center[coord]+self.speed[coord]
- if self.center[coord]<self.radius:
- self.speed[coord]=-self.speed[coord]
- self.center[coord]=self.center[coord]+2*(self.radius-self.center[coord])
- if self.center[coord]+self.radius>boxSize[coord]:
- self.speed[coord]=-self.speed[coord]
- self.center[coord]=self.center[coord]-2*(self.center[coord]+self.radius-boxSize[coord])
- def containsPoint(self,position):
- distanceX=position[0]-self.center[0]
- distanceY=position[1]-self.center[1]
- distance=math.sqrt(distanceX**2+distanceY**2)
- return distance < self.radius
- def handleMouseUp(self,position,surface):
- if self.containsPoint(position):
- self.state=Dot.selected
- self.selectCount=self.selectCount+1
- else:
- self.state=Dot.notSelected
- def drawScore(self,surface):
- displayText=('Score: '+ str(self.selectCount))
- font=pygame.font.SysFont(None,Dot.fontSize,True)
- textSurface=font.render(displayText,True,Dot.fgColor,Dot.bgColor)
- surface.blit(textSurface,(0,0))
- def calculateTime(self):
- elapsedTime=Dot.maxTime-pygame.time.get_ticks()//1000
- return elapsedTime
- def drawTime(self,surface):
- displayString1= 'Time: '+str(self.calculateTime())
- displayString2= 'Time: 0'
- font=pygame.font.SysFont(None,Dot.fontSize,True)
- if self.calculateTime()<0:
- textSurface=font.render(displayString2,True,Dot.fgColor,Dot.bgColor)
- else:
- textSurface=font.render(displayString1,True,Dot.fgColor,Dot.bgColor)
- surface.blit(textSurface,(0,(surface.get_height()-textSurface.get_height())))
- def update(aDot, surface):
- if (aDot.calculateTime()<0): # check if the game is over
- return True
- else: # continue the game
- aDot.eraseDot(surface)
- aDot.moveDot(surface)
- aDot.draw(surface)
- aDot.drawScore(surface)
- aDot.drawTime(surface)
- return False
- # Main program
- # Initialize pygame
- pygame.init()
- # Set window size and title, and frame delay
- surfaceSize = (500, 400) # example window size
- windowTitle = 'color the dot' # example title
- frameDelay = 0.02 # smaller is faster game
- # Create the window
- surface = pygame.display.set_mode(surfaceSize, 0, 0)
- pygame.display.set_caption(windowTitle)
- # create and initialize objects
- gameOver = False
- center = [200, 200] # example - replace
- color= pygame.Color('blue')
- radius= 40
- speed= [1,2]
- aDot=Dot(color,radius,center,speed)
- # Draw objects
- aDot.draw(surface)
- # Refresh the display
- pygame.display.update()
- # Loop forever
- while True:
- # Handle events
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- # Handle additional events
- if event.type== MOUSEBUTTONUP and not gameOver:
- aDot.handleMouseUp(event.pos,surface)
- # Update and draw objects for the next frame
- gameOver = update(aDot, surface)
- # Refresh the display
- pygame.display.update()
- # Set the frame speed by pausing between frames
- time.sleep(frameDelay)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement