Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/api/Sprite2.py
diff options
context:
space:
mode:
authorAlan Aguiar <alanjas@hotmail.com>2013-08-22 03:50:45 (GMT)
committer Alan Aguiar <alanjas@hotmail.com>2013-08-22 03:50:45 (GMT)
commit0c2096a9339a7c35ccea2aec3ce4f1876240b6b8 (patch)
treed57d86a5535a1c73a7c64fc2a050669d7ceb985b /src/api/Sprite2.py
parentcd58e868d23203bea1562813a3ec208982100eaa (diff)
simplify CSprite in new CSPrite2
Diffstat (limited to 'src/api/Sprite2.py')
-rwxr-xr-xsrc/api/Sprite2.py203
1 files changed, 203 insertions, 0 deletions
diff --git a/src/api/Sprite2.py b/src/api/Sprite2.py
new file mode 100755
index 0000000..edc02a3
--- /dev/null
+++ b/src/api/Sprite2.py
@@ -0,0 +1,203 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import pygame
+import math
+import Image
+import api.Math as CMath
+import api.Vector
+from api.Vector import CVector
+
+from Mouse import *
+
+class CSprite(pygame.sprite.Sprite):
+ """ An enhanced Sprite class
+ expects a gameEngine.Scene class as its one parameter
+ Use methods to change image, direction, speed
+ Will automatically travel in direction and speed indicated
+ """
+
+ mPos = None
+
+ mOffsetX = 0
+ mOffsetY = 0
+
+ def __init__(self):
+ pygame.sprite.Sprite.__init__(self)
+
+ self.mPos = CVector(0.0, 0.0)
+ self.mVel = CVector(0.0, 0.0)
+
+ # Registration point offset.
+ self.mOffsetX = 0
+ self.mOffsetY = 0
+
+ # Speed of the sprite.
+ self.mSpeed = 0
+
+ self.maxSpeed = 50
+ self.minSpeed = -3
+
+ self.mactive = False
+ self.mclicked = False
+
+ self.font = pygame.font.Font('assets/fonts/DejaVuSans.ttf', 30)
+ self.mImageMaster = self.font.render(">sprite>", True, (0,0,0), (0xFF, 0xFF, 0xFF))
+
+ # Set the image of the sprite.
+ self.image = self.mImageMaster.copy()
+
+ self.update()
+
+ def setRegistrationPointOffset(self, aOffsetX, aOffsetY):
+ self.mOffsetX = aOffsetX
+ self.mOffsetY = aOffsetY
+
+ def setXY(self, aX, aY):
+ self.mPos.setXY(aX, aY)
+ self.calculatePositionWithOffset()
+
+ def setVelXY(self, aVelX, aVelY):
+ self.mVel.setXY(aVelX, aVelY)
+ self.mVel.truncate(self.maxSpeed)
+
+ def setVelVec(self, aVec):
+ self.mVel.setXY(aVec.getX(), aVec.getY())
+ self.mVel.truncate(self.maxSpeed)
+
+ def setMaxSpeed(self, aMaxSpeed):
+ self.maxSpeed = aMaxSpeed
+
+ def update(self):
+
+ self.mPos.add(self.mVel)
+
+ self.calculatePositionWithOffset()
+
+ self.mclicked = False
+
+ if CMouse().firstPress():
+ if self.rect.collidepoint(CMouse().getPos()):
+ #print("first press in button")
+ self.mactive = True
+
+ #check for mouse release
+ if self.mactive == True:
+ if CMouse().release():
+ self.mactive = False
+ if self.rect.collidepoint(CMouse().getPos()):
+ #print("release in button")
+ self.mclicked = True
+
+ def setSpeed(self, speed):
+ """ immediately sets the objects speed to the
+ given value.
+ """
+ self.mSpeed = speed
+
+ def speedUp(self, amount):
+ """ changes speed by the given amount
+ Use a negative value to slow down
+ """
+ self.mSpeed += amount
+ if self.mSpeed < self.minSpeed:
+ self.mSpeed = self.minSpeed
+ if self.mSpeed > self.maxSpeed:
+ self.mSpeed = self.maxSpeed
+
+ def loadImage(self, aImageFilename, aIsTransparent=True):
+ self.mImageMaster = Image.loadImage(aImageFilename, aIsTransparent)
+ self.setImage(self.mImageMaster)
+
+ def setImage(self, aImage):
+ self.mImageMaster = aImage
+ self.image = aImage
+ self.rect = self.image.get_rect()
+ self.calculatePositionWithOffset()
+
+ def setPosition (self, position):
+ """ place the sprite directly at the given position
+ expects an (x, y) tuple
+ """
+ self.mPos.setX(position.getX())
+ self.mPos.setY(position.getY())
+
+ def setSpeedLimits(self, max, min):
+ """ determines maximum and minimum
+ speeds you will allow through
+ speedUp() method. You can still
+ directly set any speed you want
+ with setSpeed() Default values:
+ max: 10
+ min: -3
+ """
+ self.maxSpeed = max
+ self.minSpeed = min
+
+ def dataTrace(self):
+ """ utility method for debugging
+ print major properties
+ extend to add your own properties
+ """
+ print "x: %d, y: %d, speed: %.2f, dx: %.2f, dy: %.2f" % \
+ (self.mPos.x, self.mPos.y, self.mSpeed, self.mAngle, self.dx, self.dy)
+
+ def mouseDown(self):
+ """ boolean function. Returns True if the mouse is
+ clicked over the sprite, False otherwise
+ """
+ self.pressed = False
+ if CMouse().pressed():
+ if self.rect.collidepoint(CMouse().getPos()):
+ self.pressed = True
+ return self.pressed
+
+ def mouseOver(self):
+ if not CMouse().pressed():
+ if self.rect.collidepoint(CMouse().getPos()):
+ return True
+ else:
+ return False
+ else:
+ return False
+
+ def clicked(self):
+ """ Boolean function. Returns True only if mouse
+ is pressed and released over sprite
+ """
+ return self.mclicked
+
+ def distanceTo(self, point):
+ """ returns distance to any point in pixels
+ can be used in circular collision detection
+ """
+ (pointx, pointy) = point
+ dx = self.mPos.x - pointx
+ dy = self.mPos.y - pointy
+
+ dist = math.sqrt((dx * dx) + (dy * dy))
+ return dist
+
+ def destroy(self):
+ self.font = None
+ self.mImageMaster = None
+ self.image = None
+
+ def calculatePositionWithOffset(self):
+ self.rect = self.image.get_rect()
+ self.rect.x = self.mPos.getX() - self.mOffsetX
+ self.rect.y = self.mPos.getY() - self.mOffsetY
+
+ def getX(self):
+ return self.mPos.getX()
+
+ def getY(self):
+ return self.mPos.getY()
+
+ def getSize(self):
+ if self.image:
+ rect = self.image.get_rect()
+ return (rect[2], rect[3])
+ else:
+ return (0,0)
+