[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] circular movement in pygame



Looking at the code on this page lines 47 & 48

http://programarcadegames.com/python_examples/show_file.php?file=sprite_circle_movement.py

is there a way to doÂ
self.rect.x += some value
self.rect.y += some value

rather than

self.rect.x = self.radius * math.sin(self.angle) + self.center_x
self.rect.y = self.radius * math.cos(self.angle) + self.center_y

?

I tired it in the code attached and strangely it works ok but only OUTSIDE THE class. When I implement the exact code in a class it does something weird. I can't find where I have gone wrong.

Please help.

Many thanks in advance.

Diliup Gabadamudalige


**********************************************************************************************
This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions.
**********************************************************************************************

import sys, os, pygame, itertools
from math import sin,cos,pi, radians
from pygame.locals import *

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (50,50) #Set window position

pygame.init()
clock = pygame.time.Clock()
FPS = 1000

SCREENW = 800   #screen width
SCREENH = 740   #screen height

BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
ORANGE = (128, 100, 30)
FONT1= "Cookie-Regular.ttf"

SCREEN = pygame.display.set_mode((SCREENW, SCREENH), 0, 32) #display screen
clock = pygame.time.Clock()

#-------------------------------------------------------------------------------
def maketext(msg,fontsize, colour = ORANGE, font = FONT1):
    mafont = pygame.font.Font(font, fontsize)
    matext = mafont.render(msg, True, colour)
    matext = matext.convert_alpha()
    return matext

#-------------------------------------------------------------------------------
def print_info():
    """"""
    textcos = maketext(str(round(obj.rect.x, 2)) + "   " + str(round(obj.rect.y, 2)), 30)
    SCREEN.blit(textcos, (obj.rect.x, obj.rect.y + 30))

#-------------------------------------------------------------------------------
class object_factory(pygame.sprite.Sprite):

    def __init__(self, imagelist, xpos = 0, ypos = 0, speedx = 0, speedy = 0, value = 0):
        """Constructor"""
        pygame.sprite.Sprite.__init__(self)
        self.name = ""
        self.frame = 0
        self.imagelist = imagelist
        self.image = imagelist[self.frame]
        self.mask = pygame.mask.from_surface(self.image) # pixelmask
        self.rect = self.image.get_rect()
        self.rect.x = xpos
        self.rect.y = ypos
        self.speedx = speedx
        self.speedy = speedy
        self.timer = 0
        self.timerlimit = 10

        #----------------------------------------------------------------------
    def move(self):  # wallsprites, Herosprite, looptime
        self.rect.x += self.speedx
        self.rect.y += self.speedy

    #----------------------------------------------------------------------
    def update(self):
        """"""
        self.image = self.imagelist[self.frame]
        if self.timer >= self.timerlimit:
            self.frame += 1
            if self.frame >= len(self.imagelist):
                self.frame = 0
            self.timer = 0
        self.timer += 1

plat = pygame.image.load("plt0.png").convert_alpha()
star = pygame.image.load("gemp0.png").convert_alpha()
box = pygame.image.load("crateB.png").convert_alpha()

platforms = pygame.sprite.Group()
boxes = pygame.sprite.Group()