July 19, 2019, 01:34:14 AM

Author Topic: Learning Python  (Read 2141 times)

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 588
  • Cerberus-X Dev Team
    • Cerberus X
Re: Learning Python
« Reply #30 on: May 13, 2019, 05:47:22 AM »

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.

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #31 on: May 13, 2019, 12:58:08 PM »
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 .

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #32 on: May 13, 2019, 01:13:11 PM »
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.

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #33 on: May 16, 2019, 07:18:07 AM »
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2208
Re: Learning Python
« Reply #34 on: May 16, 2019, 08:10:37 AM »
https://www.google.com/search?q=pygame%20sprite%20collision

First hits will help you ... documentation and samples

Code:  (Unknown Language)
  1. def checkCollision(self, sprite1, sprite2):
  2.     col = pygame.sprite.collide_rect(sprite1, sprite2)
  3.     if col == True:
  4.         sys.exit()
  5.  
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #35 on: May 16, 2019, 08:13:41 AM »
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.

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #36 on: May 16, 2019, 08:26:31 AM »
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 .

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #37 on: May 16, 2019, 10:03:05 AM »
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2208
Re: Learning Python
« Reply #38 on: May 16, 2019, 11:04:43 AM »
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)




A click later I land there:



Clicking on one of the listed collide functions leads to eg.:

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:



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

Offline c0d3r9

  • Full Member
  • ***
  • Posts: 121
Re: Learning Python
« Reply #39 on: May 16, 2019, 11:16:34 AM »
Thanks a lot, Derron, I did not know how I could have said it nice.
You made pfaberĀ“s day ;)
Laptop: Win10@64bit - i3 2x2Ghz - 8GB Ram - 1TB HDD
Desktop: Win10@64bit - AMD Ryzen 5 2400G - MSI B450 Tomahawk - 8GB Ram - 240GB SSD

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #40 on: May 16, 2019, 11:42:27 AM »
Why thanks guys , much appreciated. I will try harder. Thanks once again .

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #41 on: May 17, 2019, 08:14:45 AM »
Code: [Select]
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 .

Offline Derron

  • Hero Member
  • *****
  • Posts: 2208
Re: Learning Python
« Reply #42 on: May 17, 2019, 08:37:29 AM »
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #43 on: May 17, 2019, 09:05:09 AM »
Thanks Ron I'll Give it a go.

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 191
Re: Learning Python
« Reply #44 on: May 17, 2019, 09:32:30 AM »
Code: [Select]
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.