SyntaxBomb - Indie Coders

Languages & Coding => Others => Topic started by: Pfaber11 on April 30, 2019, 14:13:49

Title: Learning Python
Post by: Pfaber11 on April 30, 2019, 14:13:49
As I posted elsewhere on this forum I have taken it on myself to try and learn Python . Well I'm 2 days in and am struggling a bit . wish they had some decent documentation like AGK2 . What I need is a list of all the key words and commands with examples of how to use them . I'm only on my second day and was hoping to at least by able to open a graphics window or any window for that matter . I'll post back here when I make some progress. installed another module for it  called pygame . It's a module for game programming  that's 3 I've got so far . If anybody has any advice on where to start it'd be most welcome . I've read a few tutorials and am watching videos on you tube.
Title: Re: Learning Python
Post by: Derron on April 30, 2019, 14:48:09
Python itself does not have that many "commands" you have to learn.

If you use libraries/modules then of course you need to learn their commands - and it depends on the module on how well they are documented.


bye
Ron
Title: Re: Learning Python
Post by: MikeHart on April 30, 2019, 14:53:00
Regarding pygame.... it took me not even a minute to google for it, find its website. Top menu.. getting started. And there tutorials.
Title: Re: Learning Python
Post by: MikeHart on April 30, 2019, 14:54:16
Btw. google for w3schools and python. The damn internet is full of tutorials.
Title: Re: Learning Python
Post by: Pfaber11 on April 30, 2019, 15:04:01
yes just gotta get used to the coding style . found some good stuff about pygame to get me started . This oop stuff is taking some getting used to. I will not give up . what I could really do with is some printed out example code to get the ball rolling.
Title: Re: Learning Python
Post by: MikeHart on May 01, 2019, 10:04:43
Hey Pfaber11, did you see this?
https://www.humblebundle.com/books/python-oreilly-books?hmb_source=navbar&hmb_medium=product_tile&hmb_campaign=tile_index_2 (https://www.humblebundle.com/books/python-oreilly-books?hmb_source=navbar&hmb_medium=product_tile&hmb_campaign=tile_index_2)
Title: Re: Learning Python
Post by: Pfaber11 on May 02, 2019, 09:41:03
Thanks MikeHart just downloaded the 4 python books in the bundle paid a dollar. not bad considering The AGK tutorials are 20 to 30 quid each. so far my python setup has only cost me a dollar. not bad really in fact it's awesome. gonna get some serious reading in today. the way I see it is if I don't get on with it by the 10th June I'll be downloading AGK studio so got over a month to try and get to grips with it. When I started with AGK2 it took a few months to find my feet so I'm going to be patient. Thanks and thumbs up on the books.
Title: Re: Learning Python
Post by: Rick Nasher on May 03, 2019, 23:48:28
Perhaps this helps too:

https://projects.raspberrypi.org/en/codeclub (https://projects.raspberrypi.org/en/codeclub)

Then just follow the 3 python links and more if you have a Pi.

And as Mike suggested:
https://www.w3schools.com/python/default.asp (https://www.w3schools.com/python/default.asp)

They are usually pretty good at showing all that's possible.

For keywords etc: https://www.w3schools.com/python/python_reference.asp (https://www.w3schools.com/python/python_reference.asp)
Title: Re: Learning Python
Post by: Pfaber11 on May 04, 2019, 07:56:12
Don't have a pie but the link to the school seems pretty good. thanks.
Title: Re: Learning Python
Post by: Krischan on May 04, 2019, 17:04:26
Thanks for the books hint - I've just bought it too. I've never coded anything in Python until last week - I had to rewrite a Python script and so I got into it. It's a nice language but I've only scratched the surface yet.
Title: Re: Learning Python
Post by: Pfaber11 on May 09, 2019, 12:47:58
Hey that's cool i've got a few lines down so far but that's about it . Installed pygame so will be taking a look at that soon .
I'm coming from AGK2 what is your main language before python ? I think it's gonna take months before I get  to grips with it but what the hell I've got the time  at the moment . I almost stopped after a few days but have got back on the wagon . The hardest thing is getting those first few lines down to start the ball rolling . Which I have now. We're off.
Title: Re: Learning Python
Post by: Pfaber11 on May 09, 2019, 22:22:07
well I've been playing with python most of the evening and now know how to open a window using pygame. I am progressing slowly . trying to find a command the equivalent of sleep in AGK but am finding it hard at the moment . The documentation is very slow to use but I am getting there in small steps . according to what I have read using panda 3d is as fast as c++ . we'll see I suppose . sticking to pygame for now . Don't want to try and run before I can walk . Gonna try and write a simple ish game and see what I come up with . probably gonna take me a couple of months but should learn a bit from it . That's what I did with AGK and seems to be the way to do it . A way anyway. Bonjour
Title: Re: Learning Python
Post by: Pfaber11 on May 10, 2019, 07:12:00
Well my task today will be to put some graphics on the screen , and maybe get them moving . onwards and upwards . I'll let you know how I get on once I've done it (if).
Title: Re: Learning Python
Post by: Pfaber11 on May 10, 2019, 19:22:20
well I'm getting there got graphics working . Bought myself a notebook and a few pens and am making notes . Feeling much more confident I can do this now , The pieces of the puzzle are coming together and faster than I thought . The 16 months I spent learning AGK2 are really helping me now . I think I'm gonna like python after all. using pygame  at the moment but I think It only does 2d but also got panda3d for the 3d stuff but I'll concentrate on what I'm doing now to get me going then in a few weeks I should be ready to move onto the 3d side of it . I think I've put in about a week so far and am happy with my progress . So far I've spent 1 dollar on some e books and a quid on a notebook and some pens . would I recommend  python to anybody else  the truth is I don't know but I will in a couple of months . I'm hoping that the games I produce will be as good as the ones I've made with AGK2 and from the demo I've seen of panda3d I believe this is attainable. Have a nice evening I'm gonna take a quick look around on here then off to read a book on yes you guessed it python.
Title: Re: Learning Python
Post by: Pfaber11 on May 10, 2019, 19:32:14
By the way guys thanks for all the links you guys have posted . This is the first time I've tried anything different to basic and I think it's sinking in now . Did have a problem converting my .py files to exe files . It does it then at the end says I don't have permissions at the end . Anybody know how I can resolve this . they run fine as .py files.
Title: Re: Learning Python
Post by: plenatus on May 10, 2019, 21:14:21
you know http://www.py2exe.org/ (http://www.py2exe.org/)   https://pypi.org/project/auto-py-to-exe/ (https://pypi.org/project/auto-py-to-exe/)
Title: Re: Learning Python
Post by: Pfaber11 on May 10, 2019, 22:14:30
Thanks for the reply got it working now . Did it using pyinstaller in the end . Got it to compile to a single exe file but still have to include my media with the exe file in the same folder . As I do with AGK2 . Did try py2exe but pyinstaller seems easier and it compresses it all into a single exe without all the dll  files showing . It's pretty tidy. Only written a couple of short pieces of code so far but know enough to think this is for me . I haven't used or declared any classes yet . so far my programs are running ok without it but I guess there will come a time when it becomes necessary . I think it acts as a container for some procedures or something like that according to what I've read. It'll probably make more sense a little way down the track. We'll see . At least I've got some compiled code and it's working as it should . I think the next thing to do is dive in and write a game and work it out as I go along .
Title: Re: Learning Python
Post by: Pfaber11 on May 10, 2019, 22:24:14
By the way I'm making notes of everything I do so kinda writing my own user manual as I go along . At least by doing that With a quick flick through the pages I can jog my memory so don't have to worry about forgetting anything. I think in a few months I won't be an expert but I will be able to build games the way I like them .
Title: Re: Learning Python
Post by: MikeHart on May 11, 2019, 06:57:44
I am using pyinstaller too. Was super easy to integrate it into the ide. As an ide i use PyCharm. Nothing beats that.


Btw, you mentioned somewhere that you hope that your code runs faster when it is compiled. Python is always interpreted. For making code sections faster i am interfacing with Cypthon. Which method
do you use?
Title: Re: Learning Python
Post by: MikeHart on May 11, 2019, 07:04:18
What the py to exe tools do is to create a bundle that will be extracted at start. Depending on how big and how many files you have, it can take time. PyInstaller can also pack everything into one exe. I choose not to as for my app it is more important to start fast.





Title: Re: Learning Python
Post by: Pfaber11 on May 11, 2019, 07:27:42
Hi mike I'm using wing personal 7 . Nice and simple . Didn't realise they take longer to start if packaged into one file . Seems to work fine though. Really starting to get into it now. I no longer find it intimidating . I'm using pygame with python 3.7.3   at the moment . I read last night that you can produce APK files to run on Android but will have to look more closely into that to see how it's done . If I get stuck on anything I'll give you a shout .
Title: Re: Learning Python
Post by: Pfaber11 on May 11, 2019, 07:35:53
I did try pycharm first but found it a tad too complicated . Wing personal 7 is simpler . Did try wing 101 as well but for some reason I didn't like it it was very simple . I intend to build myself a nice portfolio of all my python apps on the internet and hopefully I'll get some good programs created . There's so much free stuff on the internet for python it's unbelievable . Have a nice morning.
Title: Re: Learning Python
Post by: Pfaber11 on May 11, 2019, 09:23:02
Hi all been looking at running python apps cross platform and saw something called Briefcase on the Beeware site . It claims to transfer a .py app into a  apk file which runs natively on Android .  Think it does the same for ios too. Not sure how it handles the touch screen etc but will find out in due coarse . Just had a look at the python forum and posted and got told off by a mod . never mind .
A bit of snobbery going on I believe. I prefer this forum to any of the others anyway. I'm off Have a nice morning .
Title: Re: Learning Python
Post by: MikeHart on May 11, 2019, 14:07:41

Chekc it out today!

https://www.packtpub.com/packt/offers/free-learning?from=block (https://www.packtpub.com/packt/offers/free-learning?from=block)
Title: Re: Learning Python
Post by: Pfaber11 on May 11, 2019, 15:00:36
Had a look unfortunately there was no sound . Done a lot of research today. Will be starting my first project very shortly . Everything is in place . Think it's gonna be a 2d space shooter or something to that effect to get the ball rolling and cut my teeth on using pygame and after I've done that I'll start on Panda3d  and see how that goes .  I've been taking notes on things like putting graphics on the screen, drawing , sound and collisions . just stuff to jog my memory and steer me in the right direction. Did the same thing with AGK2 in the beginning and it helped enormously.  A bit like my own quick start guide with everything I need in it. Quite a bit anyway.  I'm running python 3.7.3 which I got from python.org  I think it's cpython but don't really know.
I'm glad now I didn't invest in AGK studio when I got all the python stuff for free . Apart from the 77p I spent on 4 books . Which I probably will read at some point .
Title: Re: Learning Python
Post by: LineOf7s on May 12, 2019, 00:29:14
There is absolutely sound.  Try harder.
Title: Re: Learning Python
Post by: Pfaber11 on May 12, 2019, 13:13:07
I just tried again but was too late . I've just been getting the sound up and running in my little program . Took me all morning neglected to initialize the mixer. Oh well won't forget that again in a hurry.
Title: Re: Learning Python
Post by: Pfaber11 on May 12, 2019, 13:15:12
Hi line of 7s what's your language . Are you learning or learnt python?
I'm taking a break from it for a couple of hours . Then it's time to get my graphics moving  . After that I should be in the position to try a simple game . emphasis on simple . Haven't worked out how to put text on the screen either so maybe I'll tackle that first. Coffee time . Ding Ding
Title: Re: Learning Python
Post by: Pfaber11 on May 12, 2019, 20:29:40
Well I've kinda ground to a holt just deleted my days work . What I was wondering is there a special way of organising a python program . Finding it a bit difficult with no goto or gosub or return . I do understand procedures and how to call them sort of . Don't really get classes and if someone could explain this to me and where to create them I would be most grateful.
Title: Re: Learning Python
Post by: plenatus on May 12, 2019, 21:24:54
As your native language is english i only can say read books and google.
Python is not an hard to learn language and it seems that you have many problems with simple language things.
Read ebooks, tutorials, and source codes....

but in short:  https://www.w3schools.com/python/python_classes.asp (https://www.w3schools.com/python/python_classes.asp) and https://docs.python.org/3/tutorial/classes.html (https://docs.python.org/3/tutorial/classes.html) and very important http://www.giyf.com/ (http://www.giyf.com/)  :))
Title: Re: Learning Python
Post by: MikeHart on May 13, 2019, 05:47:22

Besides what coder, i and others suggested before...


You don't need classes in Python. You can work fine with just functions/procedures. And a procedure is similar to a gosub and return. Goto is only available in a few BASIC languages.

But Pfaber11, here is a good advice. I said it before. Stick with AGK. You seem to have somehow mysterial managed to create some apps in it, so stick with that.
Somewhere i read that you program for a long time but you seem to have difficulties to understand the most simple things. Stick with AGK.
Title: Re: Learning Python
Post by: Pfaber11 on May 13, 2019, 12:58:08
Well I have rewritten my code and am finally getting it . I have sound and sprites working . Gonna keep going at this . You know it's true I'm not the sharpest pencil in the pack but I am persistent . I get the class thing now and really don't think I need to worry about it for now . Read up on it . now getting into the whole structure of python. Define procedures first and main program at the bottom. In Basic I've always done it the other way around or put my main program in the middle . Can't do that with python and that's where I was falling down . Anyway got it sorted now and everything is working . Getting used to no goto, gosub and return . I've always shied away from anything not basic
and I've now made the move away from it . As for speed really doesn't seem to be any difference between AGK2 and python but that maybe down to my slow laptop. Thanks for all your input on this forum. Don't think much of the python forum got very little response on there .
Title: Re: Learning Python
Post by: Pfaber11 on May 13, 2019, 13:13:11
You Know mike I've only been at this a week and I feel I am making good progress . When I started AGK it took me 6 weeks to complete my first game and it was crap . In one week I have nearly as much knowledge of python as I had of AGK in six weeks so I'm very pleased . There's still a few things I need to figure out but I would say another couple of weeks and I'll be ready to start producing some 2d games . I hope anyway . Got my loops working as they should do now and that was the problem I solved this morning . Sometimes it's best to just tear it up and start again. I think AGK was a very important stepping stone for me and am really glad I learnt as much about it as I did . All that knowledge is coming through in what I'm doing now . Now this time yesterday I would of agreed with you to stick with AGK but today it's all coming together . Have an excellent day.
Title: Re: Learning Python
Post by: Pfaber11 on May 16, 2019, 07:18:07
Well all's going well and I learnt a few things yesterday . One thing I've decided to do today is work out how to detect a collision between two sprites. Had a quick look last night and that is my task today , created another sprite on the screen for this process. There's a fair bit of documentation available on the internet about python to sift through. If anybody knows of a simple collision method please post it here . Thanks.
Title: Re: Learning Python
Post by: Derron on May 16, 2019, 08:10:37
https://www.google.com/search?q=pygame%20sprite%20collision

First hits will help you ... documentation and samples

Code (Python) Select

def checkCollision(self, sprite1, sprite2):
    col = pygame.sprite.collide_rect(sprite1, sprite2)
    if col == True:
        sys.exit()

Source: https://stackoverflow.com/questions/16227616/how-to-use-sprite-collide-in-pygame


pygame offers multiple collision approaches:
https://www.pygame.org/docs/ref/sprite.html


bye
Ron
Title: Re: Learning Python
Post by: Pfaber11 on May 16, 2019, 08:13:41
Well I've looked at a few websites and am not finding what I'm looking for . would like to use the mask method preferably is there a simple solution to this . The stuff I found on the internet seemed to be complicated and incomplete. I'll keep looking but if you know how this is done please let me know.
Title: Re: Learning Python
Post by: Pfaber11 on May 16, 2019, 08:26:31
Thanks Ron just taking my lad to school and when I get back I'll try out that code . Thanks again for the speedy reply .
Title: Re: Learning Python
Post by: Pfaber11 on May 16, 2019, 10:03:05
Hi not having much luck with the above code . Think I need to give my sprites an attribute (rectangle) . I'm using python 3 if that makes any difference . This is more or less the last piece of the puzzle then I'm going to attempt a simple game and see how I get on . At the moment I'm just moving some graphics around the screen (sprites) and playing some sounds . Did a background as well and worked out how to go full screen . My notebook is full of useful information for when I start my first project .

I will be a python programmer yet.
Title: Re: Learning Python
Post by: Derron on May 16, 2019, 11:04:43
Quote from: Pfaber11 on May 16, 2019, 10:03:05
Hi not having much luck with the above code . Think I need to give my sprites an attribute (rectangle) . I'm using python 3 if that makes any difference .
[...]
I will be a python programmer yet.

Before you can become a programmer you need to:
- learn to read posts
- learn to use google (or whatever search engine you prefer)


(https://www.syntaxbomb.com/proxy.php?request=http%3A%2F%2Fi.imgur.com%2FYz5GfAp.png&hash=f49663d2b1a224f320e48201d3f8bc2c801b806c)

A click later I land there:
(https://www.syntaxbomb.com/proxy.php?request=http%3A%2F%2Fi.imgur.com%2Fha3w1ky.png&hash=b0503ac9b38c8298acceb411bc674fff7385fa37)


Clicking on one of the listed collide functions leads to eg.:
(https://www.syntaxbomb.com/proxy.php?request=http%3A%2F%2Fi.imgur.com%2F9BFbtay.png&hash=5ad2a1dc6d67503a2a21b6f0cbf4842c869587e5)
Choose your preferred example from the result list.

If you clicked on the mask function you even would get the code right in that page:
(https://www.syntaxbomb.com/proxy.php?request=http%3A%2F%2Fi.imgur.com%2FAlqeDfe.png&hash=bbb58c1b534f143d4c0705c473ef69e83b56e342)


As said it is only a matter of learning to _read_ and to surf around in the web (not talking about procrastinating ;-)).
Asking stuff here which is rather simple to find in the internet and answers are in the first hits on google does not shed the best light on your motivation and basic skills.

I understand that language barriers can create issues finding specific things as you just do not know the "right word to look for" but here you already knew the terms:
python sprite collision example

And I also understand that this posting sounds rather arrogant - so do not feel attacked, feel "not too gently" pushed into the right direction ;-)


bye
Ron
Title: Re: Learning Python
Post by: plenatus on May 16, 2019, 11:16:34
Thanks a lot, Derron, I did not know how I could have said it nice.
You made pfaber´s day ;)
Title: Re: Learning Python
Post by: Pfaber11 on May 16, 2019, 11:42:27
Why thanks guys , much appreciated. I will try harder. Thanks once again .
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 08:14:45
sprite.mask = pygame.mask.from_surface(sprite.image)

Questions
At the start where it says sprite.mask came I change this to whatever I want
and at the end where it says (sprite.image) what do I put in here.
Tried many permutations but my computer throws a wobbly every time I try and run this code .
Your help is much appreciated.
I know I'm gonna get ridiculed for asking this question but I am stumped as to why I can't get this piece of code to run . Tried changing the attributes to rect as well and having the same problem. Must be doing something wrong .
Title: Re: Learning Python
Post by: Derron on May 17, 2019, 08:37:29
Post the piece of code (so some lines earlier plus the line with the "mask") in which you execute these commands?

myspritevariable.mask = pygame.mask.from_surface(myspritevariable.image)

"mask" and "image" are not changeable - they are properties of the object "myspritevariable".
"myspritevariable" is wherever you loaded your image to (at least in earlier posts you were creating the sprite out of an image loading command thingy).


@ asking
you might ask. But you need to learn to give enough hints for "us" helpers to be able to see where the culprit might be. Else it is pure guessing - which eats your time reading our posts and our time for posting them.


bye
Ron
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 09:05:09
Thanks Ron I'll Give it a go.
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 09:32:30
b=pygame.sprite.Sprite()
b.image1 = pygame.image.load("picy.png").convert_alpha()
b.image2 = pygame.image.load("picy1.png").convert_alpha()
title = pygame.image.load("aba2 title.png")
b.image1.mask = pygame.mask.from_surface(b.image1.image)
b.image2.mask = pygame.mask.from_surface(b.image2.image)


here is my code every time it gets to the 5 th line down it throws up an error .
I have tried everything I can think of .
This is only a practice run but I need to get this sorted I've been going round in circles the last couple of days. I'm really sorry for annoying you so if you don't want to answer I perfectly understand.
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 09:45:15
def collisioncheck():
    collisions = pygame.sprite.spritecollide(b.image1,b.image2,False)
    for objects in collisions:
         objects.collideWith(b.image2)
         exit

This is what I'm going to use to check for a collision . . I think I'm actually not doing that bad . This piece of code I'll be looking at next I know it's got little chance of running at the moment but I'll be doing this after I've sorted the other bit .
Title: Re: Learning Python
Post by: Derron on May 17, 2019, 09:55:43
Quote from: Pfaber11 on May 17, 2019, 09:32:30
b=pygame.sprite.Sprite()
b.image1 = pygame.image.load("picy.png").convert_alpha()
b.image2 = pygame.image.load("picy1.png").convert_alpha()
title = pygame.image.load("aba2 title.png")
b.image1.mask = pygame.mask.from_surface(b.image1.image)
b.image2.mask = pygame.mask.from_surface(b.image2.image)


I am not using pygame nor do I have access to Python on this computer now:
line #1: you create a sprite
line #2: you load an image into a property "image1" of your sprite (dunno if that exists)
line #3: you load an image into a property "image2" of your sprite (dunno if that exists)

line #5: you try to create a mask of the property "image" of "image1" which is already an image ... and you try to assign it to "mask" of that image while it should be "mask" of the sprite
line #6: you try to create a mask of the property "image" of "image2" which is already an image ...

Python might allow dynamic adding of properties (so "image1" and so on exist afterwards) but the functionality to draw the image of a sprite isn't aware of "image1" or "image2" then - you would need to override all that stuff to.
To keep it simple you behave as expected and only load a single image per sprite object ...

Appending ".image" to something is not calling a method/function but here trying to retrieve the value behind the property "image". So in your case you tried to pass the property "image" of the property "image1" of your sprite "b". You should pass property "image" of sprite "b". But "image" is empty as you loaded your stuff into "image1" (and "image2").


So without testing:

b1 = pygame.sprite.Sprite()
b1.image = pygame.image.load("picy.png").convert_alpha()

b2 = pygame.sprite.Sprite()
b2.image = pygame.image.load("picy1.png").convert_alpha()

title = pygame.image.load("aba2 title.png")

b1.mask = pygame.mask.from_surface(b1.image)
b2.mask = pygame.mask.from_surface(b2.image)



bye
Ron
Title: Re: Learning Python
Post by: Derron on May 17, 2019, 10:00:50
Your collision code does not make sense to me too ...


def collisioncheck():
    collisions = pygame.sprite.spritecollide(b.image1,b.image2,False)


... did you READ the documentation?


pygame.sprite.spritecollide()
Find sprites in a group that intersect another sprite.
spritecollide(sprite, group, dokill, collided = None) -> Sprite_list


How does that even _loosely_ connects to what you want to do by passing two images into it? IMAGES!
You planned to set masks ... so why not do a mask collision check?

pygame.sprite.collide_mask()
Collision detection between two sprites, using masks.
collide_mask(SpriteLeft, SpriteRight) -> point

it wants you to pass two SPRITEs (not IMAGES!). So following my code above you will have something like
pygame.sprite.collide_mask(b1, b2)


Also the documentation states you need to define the "rect" property of your sprite - so "yoursprite.rect". Will leave that up to you to google for it.


bye
Ron
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 12:16:55
Hi Ron I'm back yes I borrowed the collision code off an example and it doesn't make sense to me either . I'll check out some more examples on this shortly . It's amazing you can do this without a computer with python on it in front of you . That code you did worked though . Thanks for the input . I'm getting there slowly . I think I've been at this 11 days now . when I've got this all working I'll try for a simple game next then probably another 2d one then I'm going to take a look at panda3d. Thanks for all your help. I'm off to take a look at collision stuff. I'll be back.
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 14:19:55
Hi . Well I didn't realize you had to do the rect code when using the mask method but I've done it without the mask thing now. just the rect thing . sorry for the bad English but my brain is fried . Anyway It's all up and running but it's not detecting a hit yet . I'll take a break and  see what's going on .
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 17:10:31
image1=pygame.sprite.Sprite()
image1.image = pygame.image.load("picy.png").convert_alpha()
image2 = pygame.sprite.Sprite()
image2.image = pygame.image.load("picy1.png").convert_alpha()
title = pygame.image.load("aba2 title.png")
image1_rect = image1.image.get_rect(topleft=(x,y))
image2_rect = image2.image.get_rect(topleft=(x1,y1))
image1.mask = pygame.mask.from_surface(image1.image)
image2.mask = pygame.mask.from_surface(image2.image)
screen.blit(title,(0,0))
screen.blit(image2.image,(x1,y1))
screen.blit(image1.image,(x,y))
pygame.display.update()




what I'm trying to do is create a sprite with the rect attribute attached to it .just looking at the image1 code have I done this correctly . to detect a collision I've used this but so far although the code runs it does not detect a collision. Decided to put the mask code back in but the image1.mask and image1_rect stuff isn't working .

def collisioncheck():
#   if pygame.sprite.collide_rect(image1.image,image2.image):
#       exit()
    if image1.rect.colliderect(image2):
        exit()


what do you think I'm doing wrong . Any advice would be appreciated.
Title: Re: Learning Python
Post by: Derron on May 17, 2019, 18:41:34

image1_rect = image1.image.get_rect(topleft=(x,y))
image2_rect = image2.image.get_rect(topleft=(x1,y1))


image1_rect is not the same as
image1.rect


Bye
Ron
Title: Re: Learning Python
Post by: Pfaber11 on May 17, 2019, 19:42:40
Thanks for pointing that out. Still doesn't work unfortunately . Probably be one of those things it's gonna take a while to figure out . Had a look at a panda3D demo and it was really very good . I think It'll give AGK2 a run for it's money . I do think AGK is an excellent package though . I'm just trying to learn a professional language.
Title: Re: Learning Python
Post by: Derron on May 17, 2019, 20:02:38
Quote

def collisioncheck():
#   if pygame.sprite.collide_rect(image1.image,image2.image):
#       exit()


I will repeat myself: did you read the documentation?
Quote
Collision detection between two sprites, using rects.
collide_rect(left, right) -> bool
Tests for collision between two sprites. Uses the pygame rect colliderect function to calculate the collision. Intended to be passed as a collided callback function to the *collide functions. Sprites must have a "rect" attributes.
https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.collide_rect

NOWHERE do they say: please use IMAGES as param.

(untested):

def collisioncheck():
   if pygame.sprite.collide_rect(b1, b2):
       exit()

b1 and b2 are your SPRITES not your image.
Just think about it: you define a mask for the sprite by the image assigned to the sprite. You define a rectangle to the sprite.
So the image does know what about the sprite? NOTHING

So why do you pass an image to a SPRITE(!) collide_rect() function?


Think it is better to use a language for now which is a bit more strict concerning types.
Java and libGDX is mighty.
C# and Godot or Unity
...


bye
Ron
Title: Re: Learning Python
Post by: Pfaber11 on May 18, 2019, 13:25:16
Good afternoon . My problem is not detecting a hit or collision its assigning the rect attribute to a sprite . It just won't have it .
image1_rect = image1.image.get_rect(topleft(x,y))
It's this piece of code that will not work . I guess you've had enough by now Ron
basically it will not work. What I want to know is how you do it .
A few lines of code would be good .
screen.blit(image1_rect,(x,y))

This is the code I use to draw it onto the screen . nothing wrong there I think .
pygame.display.update()

Anyway could somebody please show me how it's done . I have done a lot of reading and none of it is getting through .
Title: Re: Learning Python
Post by: Pfaber11 on May 18, 2019, 13:37:30
If I just use
screen.blit(image1.image(x,y))
It puts it on the screen no problem its when I use
/screen.blit(image1_rect,(x,y))[code]
A few lines of code please . come on it can't be that hard.
Thanks
Title: Re: Learning Python
Post by: Derron on May 18, 2019, 13:47:39
LEARN TO READ

I already answered your question regarding the rect_collide.

(https://www.syntaxbomb.com/proxy.php?request=http%3A%2F%2Fi.imgur.com%2FSlCFJ6u.png&hash=c3860dc84bb8b89cc1a954ddf6375c35b6001b1c)




screen.blit(image1_rect,(x,y))


I do no longer know what to say. Move on to AGK and start posting your questions to the AGK users here, they might enjoy it.

According to your capability to read, understand and learn I assume you are in elementary school or maybe 1-2 classes higher (if this offends you, read it as "very young"), so I understand that this stuff here is a above what you are used to. Maybe begin with something which helps you to not mix up underscores, points, commands ... something which only accepts the right params and is strict on types.
Use Java, AGK, ... or whatever you like, but Python, JS, GDScript, ... they are all nothing for you until you grasped some more bits of programming.


You now edited your post, so the quoted code seems to be "corrected". Still you have a LOT to learn regarding READING and checking what you then WRITE. I mean ... just think about the command:

screen.blit(someRectVariable, (x,y))
screen: the object to blit something to
blit: the command to do so
someRectVariable: a rectangle object consisting of x, y, width and height
(x,y): a somehow grouped element consiting of coordinates x and y

... now think about this.
... think again!
... ask yourself: WHAT do I blit do the "screen"
... think again!

Isn't there the object missing which I want to see blit on the "screen" object?
Yes it is - and this is where the "docs" come reaching you a helping hand. LEARN TO READ.
I assume some IDEs for Python would even allow to integrate documentation into their workflow (F1 when cursor is over a command - or rightclick and context menu or so).


Again, I understand that this might be over your head - so maybe choose another language to start learning programming - BEFORE you even think of moving on to 3D.


And no - this time no excuse for using so harsh words and sounding arrogant.


bye
Ron
Title: Re: Learning Python
Post by: Pfaber11 on May 18, 2019, 14:01:05
That's ok Ron sorry I've annoyed you yet again . The reason my code is in such a mess is I've been trying out everything I can think of and yes I do read . And no I'm not in school . I am not ready to throw in the towel just yet and so far I think I've done ok only started my journey 12 days ago with python . Just ground to a halt on this bit . Anyway Ron Have a nice day.
Title: Re: Learning Python
Post by: Derron on May 18, 2019, 14:34:42
Thanks.

If something does not work:
- check if the thing you pass as param is of the right type (rect <> image <> sprite <> string ...)
- check if what you pass exists (b1_image does not exist - b1.image might exist)
- if you do not know how to reach a functionality of your library: use their documentation
- if you do not know how to tackle a certain problem logic wise: ask in forums (like here)


bye
Ron
Title: Re: Learning Python
Post by: Pfaber11 on May 20, 2019, 11:40:48
Well Ron I did it in the end and it was real simple . All my fault . Anyway I now have collision detection working fine . This has taken me 4 days . Don't suppose I'll forget it in a hurry. Thanks for all your help. In the end I was just putting the code in the wrong place. Thanks again.
def collisioncheck():
    image1_rect = image1.image.get_rect(topleft=(x,y))   
   
    if image1_rect.colliderect(image2_rect):
        pygame.mixer.Sound.play(bells)
 
        image1_rect = image1.image.get_rect(topright=(x,y))   
       
        if image1_rect.colliderect(image2_rect):
            pygame.mixer.Sound.play(bells)
           
            image1_rect = image1.image.get_rect(bottomleft=(x,y))   
           
            if image1_rect.colliderect(image2_rect):
                pygame.mixer.Sound.play(bells)
               
                image1_rect = image1.image.get_rect(bottomright=(x,y))   
               
                if image1_rect.colliderect(image2_rect):
                    pygame.mixer.Sound.play(bells)       


Thought after all that I would show you what I did . Basically I put the image_rect part of code where I put all my variables at the start and not next to the image1_rect.colliderect code . Glad I stuck with it after 4 days and loads of attempts to get this working . Anyway thanks for all your efforts . wish I had of posted all the code now as you would of spotted more or less straight away where I had gone wrong . Have a nice day .
I'm 14 days into the python now and after that major pain I am finally getting somewhere . When I was learning AGK I was getting stuck on things for upto a week in the first couple of months so nothing new . Just glad I didn't give up. I still think this is no harder to learn than AGK . But it did take me a year before I made a 3d game in AGK2 . Hoping it won't take this long this time as I know the process already albeit in a different language.  Took me 2 weeks to get a height map up and running in AGK2 but now I've done it a few times I'm looking at about half an hour . Made loads for my maze game . So at least I'm getting python drilled into me now . I downloaded a free book called games with python and  pygame . About 300 pages of useful information.
Title: Re: Learning Python
Post by: Pfaber11 on May 25, 2019, 09:18:23
Well I've been at this 3 weeks or so now and am going to try a simple game . Hopefully when complete it'll be good enough to put on my website . We'll see I suppose . Just gotta come up with the right idea I suppose .  Was on my phone last night and typed  python into the play store . It came up with loads of stuff books and all sorts . Phone IDE s too . Downloaded about ten books so far and I feel I should take the time to read then . There's also a ton of stuff on you tube and I've watched a few videos on there . Also enrolled in a udemy coarse for free . Hopefully in twelve months I'll be answering questions on here instead of asking them . Have a nice day.
Title: Re: Learning Python
Post by: Pfaber11 on May 26, 2019, 15:11:09
Picked up another free pdf book today called Python Pocket Reference 5th Edition. Definitely worth having . I've just discovered that windows ten will read your pdf files to you . It's not perfect but is pretty good and I read along with it . Watched a few videos on you tube for beginners and now going to move onto the intermediate  ones . I had a go at Java last year for a couple of weeks and got nowhere with it Python is a lot easier than that in my opinion. According to what I have read Python is a procedural language as well as an oop  language  so I guess it can be used either way . Bonjour
Title: Re: Learning Python
Post by: Pfaber11 on May 28, 2019, 11:20:01
I was reading somewhere that C++ is around 400 times faster than Python and have also read that Panda3d is nearly as fast as C++ . I've  done an example program of panda3d and it does look pretty impressive . Was a bit put off by all this talk of nodes but have found out it's just another name for objects . Still practising with pygame and to be honest it is a bit slow for my liking . Tried all the ways I can think of to smooth out the graphics and speed things up but I now Know panda3d does 2d stuff as well as 3d so I might just head off in that direction .   
Title: Re: Learning Python
Post by: Pfaber11 on May 30, 2019, 12:25:01
Just completed a very small coarse on Udemy for free . Learnt a couple of things with it  Got the certificate . Although I watched all of it and paid attention I could of slept through it and still got the certificate . But what would of been the point of that. Thinking of doing another one this afternoon. I think using video tutorials seems to be a good way to learn . Have a nice day.
Title: Re: Learning Python
Post by: Pfaber11 on June 01, 2019, 13:57:43
Decided to try pypy3.6 again . I've got it installed ok as when  I click on pypy3.exe it takes me into the shell . However when I try to use it with wing personal7 which is the python IDE I use it just doesn't seem to work . I've taken a screen shot which I'll place here and maybe somebody can tell me where I'm going wrong . If not I'll try another IDE and see if that helps . Here's the screen shot.
(//)
Title: Re: Learning Python
Post by: Pfaber11 on June 01, 2019, 20:50:00
Well I got pypy3.6 v.7.1.1 up and running on pycharm community addition along with pyglet . Can't get pygame running as of yet but hoping pyglet will be ok for now. Still got python 3.7.3 installed and running in the wing personal 7 IDE. Anyway if anybody wants to try this pypy out and want's any tips just ask away.   It's still python but on steroids running at 5 or 7 times as fast. Can't wait to get into both of these and do some comparisons. Good evening.
Title: Re: Learning Python
Post by: Pfaber11 on June 11, 2019, 21:50:04
Had some problems using pyglet  in that the program kept getting stuck anyway decided for now just to use standard python with pygame and ratcave . Apparently ratcave can expand pygame for 3d graphics . Found some documentation on it and will try it out shortly . All going well with python although I should imagine I'm  just scratching the surface . Now I'm getting used to the syntax and the style of it I'm finding it ok . At first I couldn't get ratcave to install and what cured it was installing ms c++ build tools . Works fine now . It's just over a gig to download but did the trick. Feels good to be using pygame again after trying out pyglet and really not liking it . Anyway that's it from me . Happy coding .
Title: Re: Learning Python
Post by: Pfaber11 on August 13, 2019, 17:57:02
Well guys after learning python for a couple of months I have decided it's not the language for me . Much prefer AGK classic for a few reasons . If I would of wanted to do the same stuff as I'm used to doing I would of had to learn Panda 3d  as well as python and I don't think I want to spend the next year learning it especially when I can do some cool stuff in AGK classic. So that's it I'm back to AGK classic which I know and love .  By the way was on steam earlier and for the next couple of days you can pick up AGK classic for £16.50 on steam. It's a bargain in my opinion. Bonjour.
Just thought I would add to this and that is Python and pygame are not a patch on AGK . Just my opinion. AGK is a lot smoother too.  8) 8)
Just to add I did enjoy python and it is very flexible with all the modules  just not my cup of tea.
Title: Re: Learning Python
Post by: Pfaber11 on October 11, 2019, 20:51:00
This is to be my last post in this thread . I went from blitz3D to AGK2 to Python back to AGK2 and now on to Pure Basic . All in a two year period .I hope this is gonna be it for at least a year but who knows . Going from Python back to AGK2 was great
and I felt at home with it . Why am I changing  to Pure Basic after 21 months basically I want to produce machine code exe's instead of byte code . I know it's not that much faster but it is faster and speed is everything to me when it comes to programming . I also like the syntax. But if I find PB is not for me which I very much doubt then I will be going back to AGK classic. Bonjour
Title: Re: Learning Python
Post by: RemiD on October 12, 2019, 10:56:30
Quote
I want to produce machine code exe's instead of byte code . I know it's not that much faster but it is faster and speed is everything to me when it comes to programming
seeing the complexity of your games, maybe consider that what you really need is to learn to optimize your procedures (don't check all entities each loop), your systems (don't update what does not need to be updated), your rendering (don't render what is not visible or has not changed)...
Title: Re: Learning Python
Post by: Pfaber11 on October 12, 2019, 23:16:09
yes you are right how did you know I check all entities per cycle . but yes only check what needs to be checked . My coding is improving and I'm getting smarter with my code . Enjoy your evening .
Title: Re: Learning Python
Post by: KonohaHokage on January 27, 2021, 13:00:32
Try this python tutorial (https://intellipaat.com/blog/tutorial/python-tutorial/) too for learning it from the best and to have a better edge over it.

p.s. admin don't delete this. lol!

thanks in advance
konoha hokage
Title: Re: Learning Python
Post by: Aurel [banned] on January 30, 2021, 09:42:50
I must react on this...

Who ,or in which interest is to push Python everywhere ?
Most of us know that python is not good doe many things because is toooo slow...
And also is so limited because depend on 3th party libs...
so why all this propeganda ....data sciance ....machine learning(what ever that junk mean).
Title: Re: Learning Python
Post by: Scaremonger on January 31, 2021, 13:00:34
Python is mostly an interpreted language so yes; its slower than anything compiled, but it can be very powerful and elegant. I started using it simply to serve a basic webpage that I could use to test another application. I did that in 5 lines of code and I still use it occasionally. Try building the same in Blitzmax and I'd be there all day!

Part of the reason it's become so popular is that the syntax can be simple for people who are not professional programmers. Having a library that can do Statistical Analysis or Neural Networks etc. means users can concentrate on the data instead of the code. I have turned to it to analyse data myself and it's often a lot easier to write in Python than in BlitzMax or other high level languages.

I am primarily a hobby programmer, but have produced commercial and private code for clients over the years. I have used a lot of programming languages since I started in 1981 and include amongst them Assembler and Scripting languages. I have written Demos, Prototypes, Web apps, Desktop apps, Libraries, Firmware, Microcode and Printer drivers and each one has different requirements.

I wouldn't write a GUI app in Assembler or firmware in Python, but I would look at the requirements of the software and select the best languages to write it and test it. Python is one of those tools I would consider and it I think it should have it's place in everyone's toolkit.

Si...
Title: Re: Learning Python
Post by: Aurel [banned] on January 31, 2021, 20:41:16
First i don't use BlitzMax
ouh ..just in 5 lines of code were behind this five lines hangin C or C++ or whatever else in some sort of
library , i am sure that python is not that powerfull that can build web site in 5 lines using core interpreter.
But i really dont like all that glorifiaction of python over  anything else >:(

QuotePython is one of those tools I would consider and it I think it should have it's place in everyone's toolkit.

I have it on my computer just for testing programs from github, performance of GUI apps are very poor and sor some
others too, then there is no real good connection between winapi and pyInterpreter..etc..etc
Title: Re: Learning Python
Post by: Steve Elliott on February 01, 2021, 09:33:19
The 3 problems I have with Python are:

1) It's just another generic language, so it relies on extra libraries to do anything but basic text programs.  Which means you have to 'bolt-on' libraries written by other people that might have a very different design philosophy to Python.  Plus it's something else not native and so must be learnt for each library added, is the documentation any good?

2) Blocks of code and scope are based on what column the text is in!  While looking neat when correct, this is a terrible idea imo!  Bugs will be harder to spot, and if your code file ever gets inputted to another text editor the code might get out of column alignment, so the program will not work immediately and easily.

3) It's interpreted and therefore slow compared to compiled code.  Computers these days are so fast that a lot of 2d games are possible and it runs immediately, but when you need extra speed it's not there.
Title: Re: Learning Python
Post by: Aurel [banned] on February 01, 2021, 10:50:39
Yes Steve i agree with you in all 3 points

let say this program should be web server on local machine in 3 lines of code

import os
from http.server import HTTPServer, CGIHTTPRequestHandler
# Make sure the server is created at current directory
os.chdir('.')
# Create server object listening the port 80
server_object = HTTPServer(server_address=('', 80), RequestHandlerClass=CGIHTTPRequestHandler)
# Start the web server
server_object.serve_forever()


ok i save code ,do exectly what is stated on that site and then i try to run
it looks that interpreter run code but then i figure that is not
then as thay say user need to run it from CMD...yeah
windows says ..it is not recognized...i am not surprized at all...and so on
ok maybe i do something wrong ...but what a heck...

Yes as you say ...user must learn all stuff if you using specific lib...so you dont learn python then
user must learn how to use this lib and how is designed ..all that is such a mess.
Title: Re: Learning Python
Post by: Aurel [banned] on February 01, 2021, 10:58:32
Then i become pissed off and try another one ...

# Python 3 server example
from http.server import BaseHTTPRequestHandler, HTTPServer
import time

hostName = "localhost"
serverPort = 8080

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
        self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
        self.wfile.write(bytes("<body>", "utf-8"))
        self.wfile.write(bytes("<p>This is an example web server.</p>", "utf-8"))
        self.wfile.write(bytes("</body></html>", "utf-8"))

if __name__ == "__main__":
    webServer = HTTPServer((hostName, serverPort), MyServer)
    print("Server started http://%s:%s" % (hostName, serverPort))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass

    webServer.server_close()
    print("Server stopped.")


..OK ...this one work because i see in console (IDE output message )

*** Remote Interpreter Reinitialized  ***
Server started http://localhost:8080


OK ...BUT this is not in just 3 LINES OF CODE !!!

..OMG ...i become a python programmer ... ::)
Title: Re: Learning Python
Post by: TomToad on February 01, 2021, 13:05:57
Just like any other language, Python has its positives and negatives. 

Python is very good at processing large amounts of data.  With things like list comprehensions, and statistical functions, all fine tuned and handled in the runtime, makes it good for things like machine learning and scientific research.  It's dynamic nature makes it highly scalable which makes it good for large web services.  There is a reason why sites like Facebook and Twitter use Python on their servers.

As for games and GUI applications, Python is not the best choice.  Python does have TKInter, but it is very basic and outdated, mainly used to make a quick interface for your program.  Python also has PyGame, but it is slow and jerky.  There are better 3rd party modules you can use such as WxPython or AGK for Python, but if you are developing a full featured GUI app or game, I think there are better languages.

Basically it comes down to using the right tool for the right job.  You should learn several languages, not just one.  To criticize Python as a language because you cannot make a good game with it, or criticize C++ because it takes 10x more lines to do the same code than Python, is like criticizing the pliers in your toolbox because it is poor at hammering in screws.
Title: Re: Learning Python
Post by: Aurel [banned] on February 01, 2021, 16:59:25
Look
I do not critic python as it is then in general ...what many around promoted..
alsmost looks like we dont need any other programming language than python..
do you get it now..?
also i am not new in programming and i know very well what is what !
Title: Re: Learning Python
Post by: Steve Elliott on February 01, 2021, 18:19:41
Quote
almost looks like we dont need any other programming language than python..

They said that about Java  :))
Title: Re: Learning Python
Post by: Scaremonger on February 01, 2021, 18:34:33
You can do a lot without any external libraries apart from those shipped with Python. I mentioned a 5 line bit of code, but it can be even easier:

1. Put an index.html and style.css file into a folder (Samples below).
2. Open a command prompt and navigate to that folder.
3. Type the following

python -m SimpleHTTPServer

4. Point your browser at 127.0.0.1:8000 and check it is working.

index.html

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>


style.css

body { background-color: powderblue; }
h1 { color: blue; }
p { color: red; }


Title: Re: Learning Python
Post by: LineOf7s on February 02, 2021, 02:05:06
Show us on the doll where the bad programming language hurt you.

It's okay.  Python can't hurt you anymore.
Title: Re: Learning Python
Post by: Aurel [banned] on February 16, 2021, 08:13:58
QuoteShow us on the doll where the bad programming language hurt you

wait a moment ..are you one of this which constantly spreading such
patronized and insulting ...babeling ..

I can only say to you ...PUT THE PIPE DOWN !
Title: Re: Learning Python
Post by: Steve Elliott on February 16, 2021, 10:04:44
Quote
Show us on the doll where the bad programming language hurt you

Quote
wait a moment ..are you one of this which constantly spreading such
patronized and insulting ...babeling ..

I can only say to you ...PUT THE PIPE DOWN !

:))