#! /usr/bin/env pythonimport pygameimport mathimport random#the class for starclass star(): def __init__(self,x,y): self.x = x self.y = y self.tempcolor = random.randint(0,255) self.color = (self.tempcolor,self.tempcolor,self.tempcolor) self.speed = 1 def draw(self,screen,blank): screen.set_at((self.x,self.y),self.color) def move(self): if self.tempcolor == 255 or self.tempcolor ==0: self.speed *= -1 self.tempcolor += self.speed self.color = (self.tempcolor,self.tempcolor,self.tempcolor)#the class for the ballclass ball(): def __init__(self,x,y,color): self.x = x self.y = y self.color = color self.xspeed = -1 self.yspeed = 1 def draw(self,screen,blank): if blank: pygame.draw.circle(screen,(0,0,0),(self.x,self.y),5) else: pygame.draw.circle(screen,self.color,(self.x,self.y),5) def move(self): self.x += self.xspeed self.y += self.yspeed if self.x <= 0 or self.x >=800: self.xspeed *= -1 if self.y <= 0 or self.y >=600: self.yspeed *= -1 def iscolliding(self,Xlistother, Ylistother): for x,y in zip(Xlistother,Ylistother): for my_x, my_y in zip(self.Xcollisionlist,self.Ycollisionlist): if my_x == x and my_y == y: return True return False def collision(self): #omg i am colliding print "mamah i hit something and i am a ball" self.xspeed *= -1 def get_collision_list(self): self.Xcollisionlist = [self.x] self.Ycollisionlist = [self.y]#THE LINE CLASSclass line(): def __init__(self,x,y,color,speed): self.x = x self.y = y self.x2 = x + 20 self.y2 = y + 20 self.color = color self.yspeed = speed self.xspeed = speed self.y2speed = speed self.x2speed = speed def draw(self,screen,blank): if blank: pygame.draw.line(screen,(0,0,0),(self.x,self.y),(self.x2 ,self.y2)) else: pygame.draw.line(screen,self.color,(self.x,self.y),(self.x2 ,self.y2)) def move(self): self.x += math.cos(self.xspeed) self.xspeed += 0.009 /2 self.y += math.cos(self.yspeed) self.yspeed += 0.008 / 1.5 self.x2 -= math.cos(self.x2speed) self.x2speed += 0.0072 self.y2 -= math.cos(self.y2speed) self.y2speed += 0.01 def get_collision_list(self): self.Xcollisionlist = [] self.Ycollisionlist = [] k = ( self.y2 - self.y ) / (self.x2 - self.x) screen.set_at((self.x,self.y),(255,0,0)) screen.set_at((self.x2,self.y2),(255,0,0)) m = self.y - k * self.x for x in range(int(self.x),int(self.x2)): y = k * x + m self.Xcollisionlist.append(x) self.Ycollisionlist.append(y) def iscolliding (self,objects,screen): for object in objects: if object == self: continue# print str(int(y))# screen.set_at((x,int(y)),(255,255,255)) #print "Comparing Y: " + str(int(y)) +", " + str(object.y) + " and X: "+str(x) + ","+str(object.x) if object.x == x and object.y == int(y): print "there was a collision" return True return False def collision(self): #omg i am colliding print "collision with line. aaaaaarg."class main: def __init__(self): self.winWidth = 800 self.winHeight = 600 self.screen = pygame.display.set_mode((self.winWidth,self.winHeight)) self.clock = pygame.time.Clock() self.stars = [] for i in range(0,100): self.stars.append(star(random.randint(0,800),random.randint(0,600))) self.objects = [] self.objects.append(line(500,350,(255,255,0),1)) for i in range(0,2): self.objects.append(ball(random.randint(0,800),random.randint(0,600),(random.randint(0,255),random.randint(0,255),random.randint(0,255)))) def draw(self, blank): for object in self.objects: object.draw(self.screen,blank) for star in self.stars: star.draw(self.screen,blank) def move(self): for object in self.objects: object.move() for star in self.stars: star.move() def loop(self): running = True while running: self.draw(True) #erase the old drawings self.move() #do the moves self.draw(False) #draw the new ones for event in pygame.event.get(): if event.type == pygame.QUIT: running = False pygame.display.flip() self.clock.tick(500)#start the whole processmain = main()main.loop()