| | |
Getting a tuple to return values.
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
0
#12 32 Days Ago
def getPos(self):
self.RobotA = x,y = 100,200
self.RobotB = x,y = 700,800
return self.RobotA, self.RobotBthat 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.
0
#13 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.
May 'the Google' be with you!
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
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)
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?
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?
0
#15 31 Days Ago
•
•
•
•
python Syntax (Toggle Plain Text)
r.getPos()[:2] = ((100, 200), (700, 800))pygame.draw.circletakes 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?
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:
python Syntax (Toggle Plain Text)
>>> r = ((5,2),(7,3)) >>> r[0] (5, 2) >>> r[1] (7, 3) >>> r[2] Traceback (most recent call last): File "<input>", line 1, in <module> IndexError: tuple index out of range >>> r[:2] ((5, 2), (7, 3)) >>>
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:
python Syntax (Toggle Plain Text)
>>> r[:] ((5, 2), (7, 3)) >>> len(r) 2
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: Python Syntax (Toggle Plain Text)
List: [ A B ] Indices: 0 1 2
[: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.
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
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.
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.
0
#17 31 Days Ago
Here is a little pygame circle expriment that should give you a hint ...
python Syntax (Toggle Plain Text)
# exploring module pygame # draw a white circle on a black screen # vegaseat import pygame as pg # window/screen width and height w = 500 h = 500 # default background is black screen = pg.display.set_mode((w, h)) # set up the circle specs color = (255, 255, 255) # (r, g, b) tuple --> white position = (200, 200) # (x,y) coordinates of center radius = 150 # optional width of the circle's border # if width = 0 (or not given) the circle is filled with color width = 2 pg.draw.circle(screen, color, position, radius, width) # update display pg.display.flip() # event loop ... running = True while running: for event in pg.event.get(): # quit when window corner x is clicked if event.type == pg.QUIT: running = False
May 'the Google' be with you!
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
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:
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.
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.
![]() |
Similar Threads
- Return values from functions (C)
- Function to return two values (C++)
- comparing return values of subroutines (Assembly)
- return values (C++)
Other Threads in the Python Forum
- Previous Thread: python books or websites
- Next Thread: Clear the terminal window.
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






