[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] how hexcolour arrays works?
Hi
First of all, why do you write that many instruction on one line??
This looks much nicer (ok, it gets a bit longer, but clearer to read):
import pygame,random
pygame.init()
a=[]*8 #- same as 'dim a[8]' from sdlBasic?
a[0]=0x883746
a[1]=0xA88721
a[2]=0xBDE87A
a[3]=0xEE875F #- list assignment index out of range?
a[4]=0xFE98A1
a[5]=0x334836
a[6]=0x374838
a[7]=0xAAB398
screen=pygame.display.set_mode((320,240))
bkgd=pygame.Surface(screen.get_size())
bkgd=bkgd.convert()
bkgd.fill(a[random.random()*7])
pygame.display.set_caption("move a box")
box=pygame.Surface((25,25))
box=box.convert()
box.fill(a[random.random()*7])
box_x=0
box_y=100
clock=pygame.time.Clock()
kpg=True
while kpg:
clock.tick(30)
for event in pygame.event.get():
if event.type==pygame.QUIT:kpg=False
box_x+=5
if box_x>screen.get_width():box_x=0
screen.blit(bkgd,(0,0))
screen.blit(box,(box_x,box_y))
pygame.display.flip()
#----------
This 'a = []*8 ' does not what you expect. See documentation.
import pygame,random
pygame.init()
# nice thing about python, just init the list with the values
a = [0x883746, 0xA88721, 0xBDE87A, 0xEE875F, 0xFE98A1, 0x334836, 0x374838, 0xAAB398]
screen = pygame.display.set_mode((320,240))
bkgd = pygame.Surface(screen.get_size())
bkgd = bkgd.convert()
# might be better to use randint(lower, upper)
bkgd.fill(a[random.randint(0,7)])
pygame.display.set_caption("move a box")
box = pygame.Surface((25,25))
box = box.convert()
# same here, use randint
box.fill(a[random.randint(0,7)])
box_x = 0
box_y = 100
clock = pygame.time.Clock()
kpg = True
while kpg:
clock.tick(30)
for event in pygame.event.get():
if event.type==pygame.QUIT:
kpg = False
box_x += 5
if box_x>screen.get_width():
box_x = 0
screen.blit(bkgd,(0,0))
screen.blit(box,(box_x, box_y))
pygame.display.flip()
Maybe your interested in some more tutorials:
http://dr0id.homepage.bluewin.ch/pygame_tutorials.html
~DR0ID
Paulo Silva schrieb:
Hi, i'm having problems on defining a hexcolour array - weird is i
never have this kind of problem when defining a string array in the
same way...
This may look as a very naive mistake, but i got really stuck, since
even from google searches i can't get any help...
Thanks a lot in advance... :-)
import pygame,random
pygame.init()
a=[]*8 #- same as 'dim a[8]' from sdlBasic?
a[0]=0x883746;a[1]=0xA88721;a[2]=0xBDE87A;a[3]=0xEE875F #- list
assignment index out of range?
a[4]=0xFE98A1;a[5]=0x334836;a[6]=0x374838;a[7]=0xAAB398
screen=pygame.display.set_mode((320,240));bkgd=pygame.Surface(screen.get_size());bkgd=bkgd.convert();bkgd.fill(a[random.random()*7])
pygame.display.set_caption("move a box")
box=pygame.Surface((25,25));box=box.convert();box.fill(a[random.random()*7])
box_x=0;box_y=100;clock=pygame.time.Clock();kpg=True
while kpg:
clock.tick(30)
for event in pygame.event.get():
if event.type==pygame.QUIT:kpg=False
box_x+=5
if box_x>screen.get_width():box_x=0
screen.blit(bkgd,(0,0));screen.blit(box,(box_x,box_y));pygame.display.flip()