Jump to content

lines not apprearing in pgzero python

AmateurBuildr

If I run this code the lines and numbers that should be appearing are not appearing, can somebody help me?

 

import pgzrun
from random import randint
from time import time

WIDTH=600
HEIGHT=338

num_of_pc=8
computers=[]
lines=[]
next_computer=0
s_time=0
t_time=0
e_time=0

def  create_computers():
    global s_time
    for count in range(0,num_of_pc):
        pc=Actor("download")
        pc.pos=randint(40,WIDTH-40), randint(40,HEIGHT-40)
        computers.append(pc)
    s_time=time()

def draw():
    screen.blit("maps",(0,0))
    number=1
    for pc in computers:
        screen.draw.text(str(number),(pc.pos[0]+8,pc.pos[1]+13))
        pc.draw()
        number=number+1
    for line in lines:
        screen.draw.line(line[0],line[1],(255,0,0))
    if next_computer<num_of_pc:
        t_time=time()-s_time
        screen.draw.text(str(round(t_time,1)),(10,10),fontsize=30)
    else:
        screen.draw.text(str(round(t_time,1)),(10,10),fontsize=30)
create_computers()
def on_mouse_down(pos):
    global next_computer,lines
    if next_computer<num_of_pc:
        if computers[next_computer].collidepoint(pos):
            if next_computer:
                lines.append((computers[next_computer-1].pos,computers[next_computer].pos))
            next_computer+=1
        else:
            lines=[]
            next_computer=0
pgzrun.go()

 

Link to comment
Share on other sites

Link to post
Share on other sites

I've run your code on my machine with random placeholder images for "download" and "maps". I can see red lines appearing as I click and there is a number visible at the top left of the window. If they're not appearing for you can you please show what is appearing?

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/18/2023 at 12:07 AM, Ominous said:

I've run your code on my machine with random placeholder images for "download" and "maps". I can see red lines appearing as I click and there is a number visible at the top left of the window. If they're not appearing for you can you please show what is appearing?

its supposed to show numbers 1-8 at the computers and it neede to show lines

Link to comment
Share on other sites

Link to post
Share on other sites

Replace your "def draw()" with this:

 

def draw():
    global t_time
    screen.blit("maps",(0,0))
    number=1
    for pc in computers:
        pc.draw()
        screen.draw.text(str(number),(pc.pos[0]+8,pc.pos[1]+13))
        number=number+1
    for line in lines:
        screen.draw.line(line[0],line[1],(255,0,0))
    if next_computer<num_of_pc:
        t_time=time()-s_time
        screen.draw.text(str(round(t_time,1)),(10,10),fontsize=30)
    else:
        screen.draw.text(str(round(t_time,1)),(10,10),fontsize=30)

 

You need to add global t_time to the top of it, as your if statement overwrites the variable defined out of the scope of the method and turns it into a local variable, meaning by the time you reach the else, t_time is not able to be referenced.

 

Secondly, just swap lines 29 and 30, so:

 

screen.draw.text(str(number),(pc.pos[0]+8,pc.pos[1]+13))

pc.draw()

 

Became:

 

pc.draw()

screen.draw.text(str(number),(pc.pos[0]+8,pc.pos[1]+13))

 

Your code was drawing the number first, then the PC was drawn on top of the number. Now, the PC is drawn first, then the number is drawn on top of the PC.

 

By the way, the text is white by default. If you want to change the colour of the number text, then do this instead of the above:

 

screen.draw.text(str(number),(pc.pos[0]+8,pc.pos[1]+13), color=(0, 0, 0))

 

This will make the text black, these are RGB values so do whatever you think looks best.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×