Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Aguiar <alanjas@hotmail.com>2012-02-26 23:14:27 (GMT)
committer Alan Aguiar <alanjas@hotmail.com>2012-02-26 23:14:27 (GMT)
commit8a4c1748efa00bb215a06a3618ff32bdf7ba9c6c (patch)
tree1b50d0f71d9ff9b20fcc29dc06ca66195de19650
parent4d4b82d4e1b117fcfe85d6a4db8c5d3aaf51461e (diff)
add new CSprite.py
-rw-r--r--[-rwxr-xr-x]src/api/CSprite.py103
1 files changed, 49 insertions, 54 deletions
diff --git a/src/api/CSprite.py b/src/api/CSprite.py
index 6a27e5a..aa746c6 100755..100644
--- a/src/api/CSprite.py
+++ b/src/api/CSprite.py
@@ -5,19 +5,16 @@ import pygame
import math
import CImage
import CMath
+import CVector
+from CVector import *
# TODO: Cambiar las conversiones de grados a radianes y viceversa usando las funciones.
# TODO: This class is a rotating sprite. update() in each frame rotates the sprite
# image, so this is inneficient for sprites that does not rotate continuoulsy.
class CSprite(pygame.sprite.Sprite):
-
- x = 0
- y = 0
- velX = 0
- velY = 0
- accelX = 0
- accelY = 0
+
+ # TODO: Como defino aca los vectores ?
""" An enhanced Sprite class
expects a gameEngine.Scene class as its one parameter
@@ -32,19 +29,10 @@ class CSprite(pygame.sprite.Sprite):
# self.mGroup = aGroup
- self.x = 0
- self.y = 0
- self.velX = 0
- self.velY = 0
- self.accelX = 0
- self.accelY = 0
-
-
+ self.mPos = CVector(0.0, 0.0)
+ self.mVel = CVector(0.0, 0.0)
+ self.mAccel = CVector(0.0, 0.0)
- # Components of the velocity vector.
- self.dx = 0
- self.dy = 0
-
# Speed of the sprite.
self.mSpeed = 0
@@ -77,8 +65,8 @@ class CSprite(pygame.sprite.Sprite):
# Set the position of the image.
# oldCenter is used to draw the trace in drawTrace().
# oldCenter is the position of the sprite in the previous frame.
- self.oldCenter = (self.x, self.y)
- self.rect.center = (self.x, self.y)
+ self.oldCenter = (self.mPos.x, self.mPos.y)
+ self.rect.center = (self.mPos.x, self.mPos.y)
# Angle of rotation in degrees used for movement calculations.
self.mAngle = 0
@@ -90,9 +78,18 @@ class CSprite(pygame.sprite.Sprite):
self.currentState = "default"
def setXY(self, aX, aY):
- self.x = aX
- self.y = aY
- self.rect.center = (self.x, self.y)
+ self.mPos.x = aX
+ self.mPos.y = aY
+ self.rect.center = (self.mPos.x, self.mPos.y)
+
+ def setVelXY(self, aVelX, aVelY):
+ self.mVel.setXY(aVelX, aVelY)
+
+ def setAccelXY(self, aAccelX, aAccelY):
+ self.mAccel.setXY(aAccelX, aAccelY)
+
+ def setMaxSpeed(self, aMaxSpeed):
+ self.maxSpeed = aMaxSpeed
def update(self):
self.oldCenter = self.rect.center
@@ -103,21 +100,19 @@ class CSprite(pygame.sprite.Sprite):
# Rotate the image according to the angle of the sprite.
self.__rotateSpriteImage()
self.checkBounds()
- self.rect.center = (self.x, self.y)
+ self.rect.center = (self.mPos.x, self.mPos.y)
# TODO: Test this. Need to pass a reference to background to draw.
# self.drawTrace((255,0,0))
- self.velX = self.velX + self.accelX
- self.velY = self.velY + self.accelY
+ self.mVel.add(self.mAccel)
- #TODO: Arreglar con vectores.
- if (self.velX > self.maxSpeed):
- self.velX = self.maxSpeed
- if (self.velY > self.maxSpeed):
- self.velY = self.maxSpeed
+ #self.mVel.truncate(self.maxSpeed)
+ if (self.mVel.x > self.maxSpeed):
+ self.mVel.x = self.maxSpeed
+ if (self.mVel.y > self.maxSpeed):
+ self.mVel.y = self.maxSpeed
- self.x = self.x + self.velX
- self.y = self.y + self.velY
+ self.mPos.add(self.mVel)
#---------------------------------------------------------------------------
@@ -148,8 +143,8 @@ class CSprite(pygame.sprite.Sprite):
self.dy = math.sin(theta) * self.mSpeed
self.dy *= -1
- self.x += self.dx
- self.y += self.dy
+ self.mPos.x += self.dx
+ self.mPos.y += self.dy
#---------------------------------------------------------------------------
# __rotateSpriteImage().
@@ -281,7 +276,7 @@ class CSprite(pygame.sprite.Sprite):
""" place the sprite directly at the given position
expects an (x, y) tuple
"""
- (self.x, self.y) = position
+ (self.mPos.x, self.mPos.y) = position
def moveBy (self, vector):
""" move the sprite by the (dx, dy) values in vector
@@ -289,8 +284,8 @@ class CSprite(pygame.sprite.Sprite):
speed or angle settings.
"""
(dx, dy) = vector
- self.x += dx
- self.y += dy
+ self.mPos.x += dx
+ self.mPos.y += dy
self.checkBounds()
def forward(self, amt):
@@ -303,8 +298,8 @@ class CSprite(pygame.sprite.Sprite):
dx = amt * math.cos(radians)
dy = amt * math.sin(radians) * -1
- self.x += dx
- self.y += dy
+ self.mPos.x += dx
+ self.mPos.y += dy
def addForce(self, amt, angle):
""" apply amt of thrust in angle.
@@ -354,7 +349,7 @@ class CSprite(pygame.sprite.Sprite):
extend to add your own properties
"""
print "x: %d, y: %d, speed: %.2f, dir: %.f, dx: %.2f, dy: %.2f" % \
- (self.x, self.y, self.mSpeed, self.mAngle, self.dx, self.dy)
+ (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
@@ -401,8 +396,8 @@ class CSprite(pygame.sprite.Sprite):
can be used in circular collision detection
"""
(pointx, pointy) = point
- dx = self.x - pointx
- dy = self.y - pointy
+ dx = self.mPos.x - pointx
+ dy = self.mPos.y - pointy
dist = math.sqrt((dx * dx) + (dy * dy))
return dist
@@ -412,8 +407,8 @@ class CSprite(pygame.sprite.Sprite):
a point """
(pointx, pointy) = point
- dx = self.x - pointx
- dy = self.y - pointy
+ dx = self.mPos.x - pointx
+ dy = self.mPos.y - pointy
dy *= -1
radians = math.atan2(dy, dx)
@@ -493,13 +488,13 @@ class CSprite(pygame.sprite.Sprite):
#create variables to simplify checking
offRight = offLeft = offTop = offBottom = offScreen = False
- if self.x > scrWidth:
+ if self.mPos.x > scrWidth:
offRight = True
- if self.x < 0:
+ if self.mPos.x < 0:
offLeft = True
- if self.y > scrHeight:
+ if self.mPos.y > scrHeight:
offBottom = True
- if self.y < 0:
+ if self.mPos.y < 0:
offTop = True
if offRight or offLeft or offTop or offBottom:
@@ -507,13 +502,13 @@ class CSprite(pygame.sprite.Sprite):
if self.mBoundAction == self.WRAP:
if offRight:
- self.x = 0
+ self.mPos.x = 0
if offLeft:
- self.x = scrWidth
+ self.mPos.x = scrWidth
if offBottom:
- self.y = 0
+ self.mPos.y = 0
if offTop:
- self.y = scrHeight
+ self.mPos.y = scrHeight
elif self.mBoundAction == self.BOUNCE:
if offLeft or offRight: