[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Making a clock rotate



Pete Shinners wrote:

btw, the get_rect() for a surface always returns a rectangle with the topleft corner at 0, 0 and width,height set to the dimensions of the image. so on our last line you can see we shortcut the get_rect()
I got it rotating, thx for your help Pete. But I have a problem more... I cannot manage to keep the rotation point in the center of the surface. My code now is this: (The problem is that the pointer progressively goes away of the surface till it goes completely out. Can you help me to correct the newrect() function which I suspect is the main responsible for this bad behave? Thx a lot)

import pygame
from pygame.locals import *
import time
import math

def load_image(name, colorkey=None):
try:
image = pygame.image.load(name)
except pygame.error, message:
print 'Cannot load image:', name
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
def newrect(oldrect, degree):
adegree = abs(degree)
if adegree in xrange(0,90):
width = 128+int(oldrect[2]*math.cos(degree) + oldrect[3]*math.sin(degree))
height = int(oldrect[2]*math.sin(degree) + oldrect[3]*math.cos(degree))
topx, topy = 0, 0
return pygame.Rect(topx, topy, width, height)
if adegree in xrange(91,180):
width = int(oldrect[2]*math.cos(degree) + oldrect[3]*math.sin(degree))
height = int(oldrect[2]*math.sin(degree) + oldrect[3]*math.sin(degree))
topx, topy = 0, 0
return pygame.Rect(topx, topy, 128+width,128+height)
if adegree in xrange(181,270):
width = int(oldrect[2]*math.cos(degree) + oldrect[3]*math.sin(degree))
height = int(oldrect[2]*math.sin(degree) + oldrect[3]*math.cos(degree))
topx, topy = 0, 0
return pygame.Rect(topx, topy, width, height)
if adegree in xrange(271, 360):
width = int(oldrect[2]*math.sin(degree) + oldrect[3]*math.cos(degree))
height = int(oldrect[2]*math.cos(degree) + oldrect[3]*math.sin(degree))
topx, topy = 0, 0
return pygame.Rect(topx, topy, width, height)

class orologio:
def __init__(self):
self.quadrante = pygame.display.set_mode((256,256))
self.image, self.rect = load_image('/home/dialtone/Documents/lancetta.jpg')
self.rect = (126, 0, self.rect[2], self.rect[3]) # Adjust rectangle
self.orig_rect = self.rect[:]
self.quadrante.blit(self.image, self.rect)
def update(self, degree):
self.degree = degree
#self.quadrante.blit(self.image, (120,0))
self.image = pygame.transform.rotozoom(self.image, self.degree, 1)
self.rect = newrect(self.orig_rect, self.degree)
#self.rect = self.image.get_rect()
self.quadrante.blit(self.image, self.rect)
pygame.display.update()
pygame.time.wait(1000)
if __name__=='__main__':
pygame.init()
clock = orologio()
degrees = 0
while 1:
if abs(degrees) >= 360: degrees = 0
clock.update(degrees)
degrees -= 6

--
try: troll.uses(Brain)
except TypeError, data: troll.plonk()
Co-Responsabile Hardware Information & Technology http://hit.edengames.net