#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Python, pygame making a segment


    my code looks like this
    def segment():
    t= ?? #THIS IS MY PROBLEM...IS THERE ANY OPTION THAT t COULD BE EVERY REAL NUMBER FROM 0 TO 1??I WAS THINKING ABOUT MAKING SOME GROUP OF FLOAT NUMBERS WITH WHILE LOOP BUT I HOPE THERE IS ANOTHER WAY
    x =100 + 100*t
    y = 500 - 400*t #parametric equation
    screen.set_at([x,y],red) #this make pixel colorfull on every x,y pixel possition

    Thank for any help
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Tewksbury, MA
    Posts
    36
    Rep Power
    2
    Originally Posted by kostik1992X
    my code looks like this
    def segment():
    t= ?? #THIS IS MY PROBLEM...IS THERE ANY OPTION THAT t COULD BE EVERY REAL NUMBER FROM 0 TO 1??I WAS THINKING ABOUT MAKING SOME GROUP OF FLOAT NUMBERS WITH WHILE LOOP BUT I HOPE THERE IS ANOTHER WAY
    x =100 + 100*t
    y = 500 - 400*t #parametric equation
    screen.set_at([x,y],red) #this make pixel colorfull on every x,y pixel possition

    Thank for any help
    EVERY real number between 0 and 1 is an infinite amount of numbers and I don't think even python can handle that. What you need is to make t a list of numbers (use the xrange function), then iterate over that list in a for loop.

    This will generate a list of 100 numbers between 0 and 1 for example:
    Code:
    for t in [n * 0.01 for n in xrange(100)]:
        print t
    Note that I used a list comprehension but you can break it down if you want it simpler to read.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    I do believe you want random.random() but I could be wrong. It generates a random floating point between 0 and 1.

    Code:
    >>> import random
    >>> help(random.random)
    Help on built-in function random:
    
    random(...)
        random() -> x in the interval [0, 1).
    
    >>> for x in range(5): print (random.random())
    ... 
    0.987384514941
    0.77259560662
    0.235799552316
    0.475659340322
    0.725902592632
    -Mek
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Are you trying to do something like this?

    python Code:
    import pygame as pg
    import os,sys,random
     
    if sys.version_info[0] == 2: range = xrange #version compatibility.
     
    RED = (255,0,0)
     
    pg.init()
    screen = pg.display.set_mode((300,600))
    Clock = pg.time.Clock()
     
    while 1:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit();sys.exit()
        for i in range(500):
            x = 100 + random.randint(0,100)
            y = 500 - random.randint(0,400)
            screen.set_at([x,y],RED)
        pg.display.update()
        Clock.tick(60)
    Note the above is just an example; please don't execute all your code in the global namespace.

    -Mek

    Edit: Actually I think you were trying to draw a line, not a box.
    python Code:
    import pygame as pg
    import os,sys,random
     
    if sys.version_info[0] == 2: range = xrange #version compatibility.
     
    RED = (255,0,0)
     
    pg.init()
    screen = pg.display.set_mode((300,600))
    Clock = pg.time.Clock()
     
    while 1:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit();sys.exit()
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_SPACE:
                    screen.fill(0)
        for i in range(20):
            t = random.random()
            x = 100 + int(100*t)
            y = 500 - int(400*t)
            screen.set_at([x,y],RED)
        pg.display.update()
        Clock.tick(60)
    Last edited by Mekire; May 24th, 2013 at 10:01 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    yes i wanted to draw a line...and make of t infinite numbers between 0 and 1 so my line would be full of colorfull pixels....thanks of advice!!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    You can do some pretty amusing stuff with this parametric approach. It can easily get too processing intensive though and start to slow down your program. Fun though.

    python Code:
    import pygame as pg
    import os,sys
    import random,math
     
    if sys.version_info[0] == 2: range = xrange #version compatibility.
     
    def ellipse_points(cent,rad,width=None):
        if width == None:
            width = rad
        t = random.random()
        r = random.random()
        x = cent[0]+int((rad[0]-width[0]*r)*math.cos(2*math.pi*t))
        y = cent[1]-int((rad[1]-width[1]*r)*math.sin(2*math.pi*t))
        return x,y
     
    def event_loop(screen):
        keys = pg.key.get_pressed()
        for event in pg.event.get():
            if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
                pg.quit();sys.exit()
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_SPACE:
                    screen.fill(0)
     
    def update(size,speed,screen):
        for i in range(speed):
            color = [random.randint(0,255) for i in range(3)]
            center,radius,width = (150,300),(100,250),(20,1)
            c,r,w = scale(size,center,radius,width)
            x,y = ellipse_points(c,r,w)
            screen.fill(color,((x*size[0],y*size[1]),size))
     
    def scale(size,*args):
        return [(arg[0]//size[0],arg[1]//size[1]) for arg in args]
     
    def main():
        pg.init()
        Clock = pg.time.Clock()
        pg.display.set_caption("FPS: {:.2f}".format(Clock.get_fps()))
        screen = pg.display.set_mode((300,600))
     
        cell_size = (5,5)
        cells_per_frame = 200
     
        while 1:
            event_loop(screen)
            update(cell_size,cells_per_frame,screen)
            pg.display.update()
            pg.display.set_caption("FPS: {:.2f}".format(Clock.get_fps()))
            Clock.tick(60)
     
    if __name__ == "__main__":
        main()

    -Mek
    Last edited by Mekire; May 24th, 2013 at 12:06 PM.

IMN logo majestic logo threadwatch logo seochat tools logo