Why is this not working?

17 views
Skip to first unread message

lsk464

unread,
Sep 25, 2022, 2:27:04 AM9/25/22
to pygame mirror on google groups
Hi I'm a bit new the these groups/forums but don't know where else to turn for some help.
I've been learning and writing some python/pygame programs and this one has me stumped. I have my code to upload if someone is interested to help me.
In this game there are 16 faces arranged around a circle with a shooter in the center.
All faces should move towards the center. There is an xvel/yvel for each face but the faces are not moving properly. Some only move on the x-axis some only on the y-axis and some don't move at all ???  I can ZIP up my code and pics for anyone interested to help. Thanks!

lsk464

unread,
Sep 25, 2022, 8:59:31 AM9/25/22
to pygame mirror on google groups
OK here is some simplified code, the issues is with line 33,34 see below:

# Pygame template - skeleton for a new pygame project
import pygame
import os
from math import sin,cos

WIDTH = 600
HEIGHT = 600
FPS = 10
PI=3.14159

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
pygame.mixer.set_num_channels(16)

win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gobblins")
clock = pygame.time.Clock()

# define game folder and assets folder
game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "bling")
gob_img=(pygame.image.load(os.path.join(img_folder,"face8.png")))
######################################################################
class Gob():
    def __init__(self,i,x,y):

        # velfactor of -80 then all faces move but velfactor of -300 and movement is not right at all
        velfactor=-300

        self.image=pygame.transform.scale(gob_img,(70,70))
        self.rect=self.image.get_rect()
        self.rect.center=(x+WIDTH/2,y+HEIGHT/2)
        self.xvel=round(x/velfactor*1.07,3)
        self.yvel=round(y/velfactor,3)
        print("%3d %8.3f %8.3f %8.3f %8.3f" % (i,x,y,self.xvel,self.yvel))

    def gupdate(self):
        (x,y)=self.rect.center
        x+=self.xvel
        y+=self.yvel
        self.rect.center=(x,y)
######################################################################
# Replace the stupid Python range() function with this much better one
def myrange(start=1,stop=10,step=1):    
    rng=[]
    x=start
    if step>0:
        while x<=stop:
            rng.append(x)
            x+=step
            x=round(x,5)
    else:
        while x>=stop:
            rng.append(x)
            x+=step
            x=round(x,5)
    return(rng)
#############################################################################
# this generates a list of "num_dots" coordiants (x,y) evenly distributed around a circle of radius "radius"
def dots(num_dots=12,radius=300):
    pts=[] ; num_dots/=2
    for a in myrange(PI/2,num_dots*5,PI/num_dots):
        Y=float(round(radius*cos(a)))
        X=float(round(radius*sin(a)))
        if [X,Y] in pts: break
        pts.append([X,Y])
    return(pts)  
######################################################################
# create goblins around a circle
gobs=[]
gpoints=dots(16,WIDTH/2-50)
for i,(x,y) in enumerate(gpoints):
    gob = Gob(i,x,y)
    gobs.append(gob)
######################################################################
# Game loop
run = True
while run:
    # keep loop running at the right speed - Frames Per Second
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            run = False

    # Update
    for gob in gobs:
        gob.gupdate()

    # Draw / render the screen
    win.fill('white')
    for gob in gobs:
        win.blit(gob.image,(gob.rect.x,gob.rect.y))
    pygame.draw.circle(win,'red',(WIDTH/2,HEIGHT/2),300,2)
    pygame.draw.circle(win,"red",(WIDTH/2,HEIGHT/2),5,2)
    pygame.display.flip()

pygame.quit()

Reply all
Reply to author
Forward
0 new messages