0

This question is related to

My code is below. You can use any small image for my images.

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, ypos, 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()

rotcenx = SCREENW/2
rotceny = SCREENH/2

radius = 200
angle = radians(90)  #pi/4 # starting angle 45 degrees
omega = radians(5) #Angular velocity

m = rotcenx + radius * cos(angle) #Starting position x
n = rotceny - radius * sin(angle) #Starting position y

for _ in itertools.repeat(None, 1):
    madyax = SCREENW/2
    madyay = SCREENH/2

    araya = 200
    konaya = radians(180)  #pi/4 # starting angle 45 degrees
    konika_pravegaya = radians(5) #Angular velocity

    a = madyax + (araya * cos(konaya)) #Starting position x
    b = madyay - (araya * sin(konaya)) #Startinh position y

    plat = object_factory([plat], a, b)
    plat.araya = araya
    plat.konaya = konaya
    plat.kp = konika_pravegaya


    platforms.add(plat)

while True:
    ms = clock.tick(FPS)  # milliseconds passed since last frame
    #looptime = milliseconds / 1000.0 # seconds passed since last frame

    SCREEN.fill((BLACK))

    pygame.draw.circle(SCREEN, BLUE, (SCREENW / 2, SCREENH / 2), 5)

    ##-----------------------------------------------------------
    SCREEN.blit(star, (m, n)) # Draw current x,y

    angle = angle + omega # New angle, we add angular velocity
    m = m + radius * omega * cos(angle + pi / 2) # New x
    n = n - radius * omega * sin(angle + pi / 2) # New y
    ##-----------------------------------------------------------
    # show object anchored to center of rotation
    pygame.draw.line(SCREEN, ORANGE, (rotcenx, rotceny), (m, n))

    text = maketext(str(radius), 30)
    SCREEN.blit(text, (m, n - 40))

    text = maketext((str(round(m, 2)) + "  " + str(round(n, 2))), 30)
    SCREEN.blit(text, (m, n + 40)) # Draw current x,y

    ##------------------------------------------------------------------
    for plat in platforms:

        plat.konaya = plat.konaya + plat.kp
        plat.rect.x = plat.rect.x + plat.araya * plat.kp * cos(plat.konaya + pi / 2)
        plat.rect.y = plat.rect.y - plat.araya * plat.kp * sin(plat.konaya + pi / 2)
    ##------------------------------------------------------------------------
    pygame.draw.line(SCREEN, ORANGE, (madyax, madyay), (plat.rect.x, plat.rect.y))

    platforms.update()
    platforms.draw(SCREEN)

    pygame.event.pump()
    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()

    pygame.display.update()
    pygame.time.wait(100)

Why does the code work when used outside a class and NOT when in? I simply can't find what I have done wrong. Please don't ask me to read any Google documents or search on the Internet as I am posting after doing so and NOT finding an answer to my question. I am NOT an expert in math and would only like to know a solution to this problem. Please help. Link to video is below http://youtu.be/0oRDX246aj8

wookie
  • 329
  • 1
  • 5
  • 13

0 Answers0