Getting a tuple to return values.

Thread Solved

Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #11
32 Days Ago
sorry, i didnt spot you jlm699
i didnt expect people to respond so fast, let alone to.
Forgive me, i'll check it out now.

Cheers for the help
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #12
32 Days Ago
def getPos(self):
        self.RobotA = x,y = 100,200
        self.RobotB = x,y = 700,800
        return self.RobotA, self.RobotB

that worked. I feel quite bad for constantly asking when i run up against errors, but they just keep coming up.

pygame.draw.circle(screen,WHITE,r.getPos()[:2],10)
TypeError: an integer is required

It's not clear where i have to put/get this integer from.

My apologese guys.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #13
32 Days Ago
Originally Posted by thehivetyrant View Post
def getPos(self):
        self.RobotA = x,y = 100,200
        self.RobotB = x,y = 700,800
        return self.RobotA, self.RobotB

that worked. I feel quite bad for constantly asking when i run up against errors, but they just keep coming up.

pygame.draw.circle(screen,WHITE,r.getPos()[:2],10)
TypeError: an integer is required

It's not clear where i have to put/get this integer from.

My apologese guys.
My suggestion is to test print WHITE and r.getPos()[:2], they should both be integer tuples.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #14
31 Days Ago
Okay i test printed WHITE and r.getPos()[:2]
WHITE = (255, 255, 255)
r.getPos()[:2] = ((100, 200), (700, 800))


i was doing a bit of looking around and the pygame.draw.circle takes 5 values.

(screen, color, (x,y), radius, thickness)

pygame.draw.circle(screen,WHITE,r.getPos()[:2],10)
we have screen,
and the color,
r.getPos()[:2] are 2 different (x,y) tuples,
and we have 10 as the last radius or thickness,

I have tried putting another 10 after that 10 but i still get the same error thats an integer is required.

Strange?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 263
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #15
31 Days Ago
Originally Posted by thehivetyrant View Post
  1. r.getPos()[:2] = ((100, 200), (700, 800))
pygame.draw.circle takes 5 values:
(screen, color, (x,y), radius, thickness)
...
pygame.draw.circle(screen,WHITE,r.getPos()[:2],10) ...
we have screen,
and the color,
r.getPos()[:2] are 2 different (x,y) tuples,
and we have 10 as the last radius or thickness,
...
Strange?
Okay, you've solved your own problem you just don't even realize it yet. The circle function is looking for the values you suggested above. Everything you've provided is correct except the r.getPos() part, which you even identified as being incorrect (r.getPos()[:2] are 2 different (x,y) tuples). Now if you take another peek at the circle parameters it's looking for ONE (x,y) tuple. But you're giving it two, as you yourself noted.

Do you see the problem yet? You need to decide which tuple you want to go with, the first or the second.

I have a feeling the confusion comes from the way you're trying to do your indexing. Here's a little example:
  1. >>> r = ((5,2),(7,3))
  2. >>> r[0]
  3. (5, 2)
  4. >>> r[1]
  5. (7, 3)
  6. >>> r[2]
  7. Traceback (most recent call last):
  8. File "<input>", line 1, in <module>
  9. IndexError: tuple index out of range
  10. >>> r[:2]
  11. ((5, 2), (7, 3))
  12. >>>
In most computer languages, indexing starts from position 0 (zero). This index is the first element. The second element is thus at index 1. The index after the second element is 2; however in our case there is no third element, so when we try to explicitly call it, we get an error.

Using a colon in indexing means you want a range of indices. When there is no number present, it represents either the starting or ending index. If I use only a colon with no numbers, it is literally giving me a copy of the entire object, from beginning to end:
  1. >>> r[:]
  2. ((5, 2), (7, 3))
  3. >>> len(r)
  4. 2
As you can see, the length of r is 2. So even though there is no element at index 2, it is still pointing to the "end" of the list. I'll try to draw a picture of how the indexing is represented in a list:
  1. List:
  2. [ A B ]
  3. Indices: 0 1 2
So when I provide the range of [:2] that's the same as saying [0:2] , which is our starting and ending indices. So in this example we get all elements that are between the 0th and 2nd index (A and B, or first and second elements).

If we wanted just the first element we could either use the index [:1] (a.k.a. the element between index 0 and index 1), or explicitly call it using [0] , which is essentially the same thing.

I hope that explanation was clear, it'd be easier to explain if you were in front of me with a chalk board, but hopefully that makes a little bit more sense for you.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #16
31 Days Ago
I appreciate the explianation along with that, it was very helpful,
i used [0]
and wrote the draw.circle line again and wrote[1] instead.

This now works and 2 circles are now shown properly on screen when run.

They don't move like they're supposed to (or at all) but thats a different problem.

I think this problem is solved then?

Thanks for both your help Vega and jlm, it was very much appreciated and helpful!

you too slate.
Last edited by thehivetyrant; 31 Days Ago at 9:53 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #17
31 Days Ago
Here is a little pygame circle expriment that should give you a hint ...
  1. # exploring module pygame
  2. # draw a white circle on a black screen
  3. # vegaseat
  4.  
  5. import pygame as pg
  6.  
  7. # window/screen width and height
  8. w = 500
  9. h = 500
  10. # default background is black
  11. screen = pg.display.set_mode((w, h))
  12.  
  13. # set up the circle specs
  14. color = (255, 255, 255) # (r, g, b) tuple --> white
  15. position = (200, 200) # (x,y) coordinates of center
  16. radius = 150
  17. # optional width of the circle's border
  18. # if width = 0 (or not given) the circle is filled with color
  19. width = 2
  20. pg.draw.circle(screen, color, position, radius, width)
  21.  
  22. # update display
  23. pg.display.flip()
  24.  
  25. # event loop ...
  26. running = True
  27. while running:
  28. for event in pg.event.get():
  29. # quit when window corner x is clicked
  30. if event.type == pg.QUIT:
  31. running = False
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #18
31 Days Ago
while we're on the subject, after you draw that circle.
How would you go about moving it to the right say 5 pixels.

I tried doing something like: self.move_to(self.x-5,self.y) but it didnt do anything. It might be because my definitions aren't done right.

Is there a simple way of doing this that i might look at and adapt and implement into my program?

Thanks for the circle code, theres quite abit of comments on it thats very helpful in the understanding.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC