August 25, 2019, 12:21:51 AM

Author Topic: Learning Python  (Read 2752 times)

Offline Pfaber11

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

Offline Derron

  • Hero Member
  • *****
  • Posts: 2337
Re: Learning Python
« Reply #46 on: May 17, 2019, 09:55:43 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)

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:
Code: [Select]
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

Offline Derron

  • Hero Member
  • *****
  • Posts: 2337
Re: Learning Python
« Reply #47 on: May 17, 2019, 10:00:50 AM »
Your collision code does not make sense to me too ...

Code: [Select]
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #48 on: May 17, 2019, 12:16:55 PM »
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.

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #49 on: May 17, 2019, 02:19:55 PM »
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 .

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #50 on: May 17, 2019, 05:10:31 PM »
Code: [Select]
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 .

Code: [Select]
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2337
Re: Learning Python
« Reply #51 on: May 17, 2019, 06:41:34 PM »
Code: [Select]
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #52 on: May 17, 2019, 07:42:40 PM »
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2337
Re: Learning Python
« Reply #53 on: May 17, 2019, 08:02:38 PM »
Quote
Code: [Select]
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):
Code: [Select]
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #54 on: May 18, 2019, 01:25:16 PM »
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 .
 
Code: [Select]
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 .
Code: [Select]
screen.blit(image1_rect,(x,y))
This is the code I use to draw it onto the screen . nothing wrong there I think .
Code: [Select]
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 .

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #55 on: May 18, 2019, 01:37:30 PM »
If I just use
Code: [Select]
screen.blit(image1.image(x,y))It puts it on the screen no problem its when I use
Code: [Select]
/screen.blit(image1_rect,(x,y))[code]
A few lines of code please . come on it can't be that hard.
Thanks

Offline Derron

  • Hero Member
  • *****
  • Posts: 2337
Re: Learning Python
« Reply #56 on: May 18, 2019, 01:47:39 PM »
LEARN TO READ

I already answered your question regarding the rect_collide.





Code: [Select]
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #57 on: May 18, 2019, 02:01:05 PM »
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2337
Re: Learning Python
« Reply #58 on: May 18, 2019, 02:34:42 PM »
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

Offline Pfaber11

  • Full Member
  • ***
  • Posts: 210
Re: Learning Python
« Reply #59 on: May 20, 2019, 11:40:48 AM »
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.
Code: [Select]
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.