### Thread: Python, pygame making a segment

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. 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.
3. 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
4. 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.
5. 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!!
6. 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.

if width == None:
t = random.random()
r = random.random()
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)]
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.