When I run pygame code , I just get non responding window.
[code]import pygame
screen=pygame.display.set_mode((800,600))
klik=pygame.mouse.get_pressed
running=1
pozice=pygame.mouse.get_pos
while running==1:
if klik(0)==1:
x=pozice(0)
y=pozice(1)
draw=1
while draw ==1:
pygame.draw.line(screen, (0,255,0), (x,y),(pozice(0),pozice(1)), width=1)
x1=pozice(0)
y1=pozice(1)
pygame.display.flip
if x1!=pozice(0) or y1!= pozice (1):
pygame.draw.line(screen, (0,0,0), (x,y),(x1,y1), width=1)
pygame.display.flip
[/code]
Im only starting pythont and this is attemp at drawing a line using mouse.
When you clik left mouse button , it grabs mouse position and starts a loop.
In the loop it draws a line from the beggining mouse pos and current mouse pos.
Then it grabs the second coordinates of the line and updates the screen.
THen it checks if the mouse moved , and if itr did , it draws a black line over the first line erasing it.
THen the loop restarts.
THis should make the line follow the mouse.
[QUOTE=Levakama;19896326]Words[/QUOTE]
You forgot to add event management. A proper way to do things would be:
[code]
import pygame
screen=pygame.display.set_mode((400,400))
running=True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key==pygame.K_ESCAPE:
running=False
//whatever stuff you want
pygame.display.upate()
[/code]
You need to call some sort of pygame.event.get(). Theres other stuff you can use instead of event.get(). More on that [url=http://www.pygame.org/docs/ref/event.html]here[/url] and [url=http://www.google.de/search?q=pygame+event+management&hl=en]here[/url].
Okay now it works , but the code doesnt work...
I must think more
[editline]05:04PM[/editline]
SOmething is wrong with the klik var
[QUOTE=Levakama;19896516]Okay now it works , but the code doesnt work...
I must think more
[editline]05:04PM[/editline]
SOmething is wrong with the klik var[/QUOTE]
I'll post the solution in a second.
Here it is:
[code]
import pygame
screen = pygame.display.set_mode((800,600))
running = True
startpos = (0,0)
endpos = (0,0)
drawing = False
lines = []
justdrawn = False
while running:
for event in pygame.event.get():
screen.fill((255,255,255))
justdrawn = False
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 1:
if drawing == True:
endpos = pygame.mouse.get_pos()
drawing = False
justdrawn = True
lines.append((startpos,endpos))
if drawing == False:
startpos = pygame.mouse.get_pos()
endpos = pygame.mouse.get_pos()
if justdrawn == False:
drawing = True
if event.type == pygame.MOUSEBUTTONDOWN and event.dict["button"] == 3:
lines = []
drawing = False
if drawing == True:
endpos = pygame.mouse.get_pos()
pygame.draw.line(screen,(0,0,0),startpos,endpos)
for element in lines:
start,end=element
pygame.draw.line(screen,(0,0,0),start,end)
pygame.display.update()[/code]
Essentially you do everything within the event loop. It what keeps your program running.
The rest of what I did, you'll have to figure out yourself as I'm way too lazy to explain it all in detail.
I will be thank ful if you do.
I only learned python for approx . 5 hours total , so I dont know much yet.
Maybe what I posted is a bit of an overkill then...
You should consider doing just python for a while until you have understanding for programming practices in general. Then you should start thinking about how to do games with pygame.
Not really , I understand your code.
But can I modify my code just a bit to make it work or do I have to make it like you did?
Your code doesn't really work well. Take the code from my first post and go from there.
You need to do all the work inside the event loop to ensure that you're getting all the inputs.
Also you shouldn't do loops inside the event loop that "listen" to other events.
That way you'll end up with another unresponsive window. Hence why I introduced a drawing variable to know whether I'm currently drawing.
Hope that helps.
Sorry, you need to Log In to post a reply to this thread.