• 0

[Python/PyGame] Recognising when the mouse has drawn a line


Question

I have to create a game for one of my assignments in python, using pygame. I'm considering developing an InkBall type game (InkBall can be found in Windows Vista), however, I cannot seem to find any information on how to get the user interface to recognise when the mouse has drawn a line. I just can't seem to figure it out.

I would like to be able to hold down the left button on the mouse and draw a line anywhere in the game area, just like InkBall, but I can't figure this out anywhere.

I'm not experienced in this as we haven't been doing python long and I haven't really created any games yet.

If anybody could help me at all, I would really appreciate it :)

Thanks in advance.

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I've done it once in XNA, but I've no experience with pyGame and precious little experience with Python at all. But I'll tell you what I did in XNA :

Any time the mouse button is newly pressed (wasn't pressed last update but is pressed this update), delete previous line

Any time the mouse is pressed (currently pressed this update), add its coordinates (and a bunch of coordinates around it, you want the line to have a certain width) to your "line" object

Any time the ball collides with the line, detect angle between center of ball and colliding pixel, rebound around that angle

Any time the ball collides with the line, delete line

You don't have much choice but to do per-pixel collision detection because the line has a totally arbitrary shape. The ball hasn't, however, it's a circle, and of course you only need to check for pixels near the ball.

Also remember to draw the line, of course, but that's easy (for each pixel in line, draw a black pixel, for instance).

One issue you might have is that if your game's frame rate is too slow, the line will not be continuous when the user is moving the mouse too fast. Then you might want to correct that by tracing straight lines between recorded points. I heard pyGame is very slow so this could be an issue.

I hope that helps.

Link to comment
Share on other sites

  • 0

Thank you for the quick reply Dr_Asik :) I read it on 29 March, I have just been busy with other assignments and thinking about this some more.

Your way sounds the most logical and I will look into this in the next few days, when I come to develop this further.

Link to comment
Share on other sites

  • 0
You're welcome. I'd like to see how your project turns out.

Sure :) I'll show you how it went soon after I've handed it all in. I only have 'till 20th to get this done so I hope I haven't left it too late :p

Thanks, again, for the quick help!

Link to comment
Share on other sites

  • 0
You're welcome. I'd like to see how your project turns out.

Just to let you know, I ended up scrapping the InkBall idea :p I ran out of time and other assignments took up too much of my time. Instead, I made a very quick snake-type game. Nothing spectacular and not many features :D

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.