Skip to content

Instantly share code, notes, and snippets.

@bendtherules
Created May 10, 2013 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendtherules/5553778 to your computer and use it in GitHub Desktop.
Save bendtherules/5553778 to your computer and use it in GitHub Desktop.
import pygame
class ball(object):
global mouse_down
#global pygame
def __init__(self,x=320,y=240,speed=3):
#global pygame
self.x=x
self.y=y
self.speed=speed
self.dir=None
self.ball=pygame.image.load("gun.png")
self.ballrect=self.ball.get_rect()
#global mouse_down
#mouse_down=False;
if not mouse_down:
print 5
def mouse_handler(self):
global mouse_down
if pygame.event.get(pygame.MOUSEBUTTONDOWN) or mouse_down == True:
self.x=self.mouse_x
self.y=self.mouse_y
mouse_down=True
print 6
if pygame.event.get(pygame.MOUSEBUTTONUP):
mouse_down=False
def step(self):
self.mouse_x,self.mouse_y=pygame.mouse.get_pos()
self.diff_x=self.mouse_x-self.x
self.diff_y=self.mouse_y-self.y
self.diff_rad=math.atan2(self.diff_y,self.diff_x)
self.x+=self.speed*math.cos(self.diff_rad)
self.y+=self.speed*math.sin(self.diff_rad)
self.ballrect.center=(self.x,self.y)
def draw(self):
screen.blit(self.ball, self.ballrect)
def update(self):
self.step()
self.mouse_handler()
self.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment