Python help |
Author: |
Message: |
Chancer
Senior Member
Posts: 648 Reputation: 7
35 / /
Joined: May 2005
Status: Away
|
O.P. RE: Python help
Cool
Actually, the "hard part" was
Python code: ((int(str(symbol)[-1]))-1)
You get a simbol (65361, 65362, etc);
convert it to a string;
get only the last character ([-1]);
convert into an integer;
subtract 1.
Is this right?
|
|
09-20-2009 10:05 PM |
|
|
Jarrod
Veteran Member
woot simpson
Posts: 1304 Reputation: 20
– / /
Joined: Sep 2006
|
RE: Python help
yep that's it, you could do it with dictionaries or more if statements, but indexes just seemed like a thinner way to code it, it also gave me less to think about,
python code: >>> symbol=65361
>>> str(symbol)[-1]
'1'
>>> int(str(symbol)[-1])
1
>>> int(str(symbol)[-1])-1
0
This post was edited on 09-21-2009 at 12:11 AM by Jarrod.
|
|
09-20-2009 10:08 PM |
|
|
Chancer
Senior Member
Posts: 648 Reputation: 7
35 / /
Joined: May 2005
Status: Away
|
O.P. RE: Python help
Or even easier:
Python code: symbol % 10 - 1
It worked.
|
|
09-21-2009 01:06 AM |
|
|
Jarrod
Veteran Member
woot simpson
Posts: 1304 Reputation: 20
– / /
Joined: Sep 2006
|
RE: Python help
that's probably more efficient too,
well if you need any help with getting pyglet to do anything else I'm happy to give you a hand
|
|
09-21-2009 01:23 AM |
|
|
Chancer
Senior Member
Posts: 648 Reputation: 7
35 / /
Joined: May 2005
Status: Away
|
O.P. RE: Python help
Here I am once again.
Our teacher told us to use pygame to create the interface, so I was wondering if it was possible to port that code you did to pygame libraries.
I made a big progress, but it's not good enough:
python code: import pygame
from pygame.locals import *
def main():
pygame.init()
screen = pygame.display.set_mode((75, 55))
screen = pygame.display.set_caption('Projeto 39')
screen = pygame.display.get_surface()
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 255, 255))
screen.blit(background, (0, 0))
while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
return # Close window when clicking X ou hitting Escape
elif event.type == KEYDOWN: # Verify pressed keys
key = pygame.key.get_pressed()
if key[K_UP] and key[K_RIGHT]:
print 'AD'
elif key[K_UP] and key[K_LEFT]:
print 'AE'
elif key[K_DOWN] and key[K_RIGHT]:
print 'RD'
elif key[K_DOWN] and key[K_LEFT]:
print 'RE'
elif key[K_UP]:
print 'Ax'
elif key[K_DOWN]:
print 'Rx'
elif key[K_RIGHT]:
print 'xD'
elif key[K_LEFT]:
print 'xE'
screen.blit(background, (0, 0))
pygame.display.flip()
main()
This does print the codes (AD, Ax, etc) when I press the keys, but it does it only once per keypress, and I need it to do it repeatedly.
What do you say? Is it possible?
|
|
09-26-2009 11:20 PM |
|
|
Jarrod
Veteran Member
woot simpson
Posts: 1304 Reputation: 20
– / /
Joined: Sep 2006
|
RE: Python help
well this is why I suggested pyglet over pygame, AFAIK in pygame you have an update method just like in pyglet but it needs to be inherited from a sprite object this is a sample I found on the net,
python code: class Fist(pygame.sprite.Sprite):
"""moves a clenched fist on the screen, following the mouse"""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image, self.rect = load_image('fist.bmp', -1)
self.punching = 0
def update(self):
"move the fist based on the mouse position"
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5, 10)
so the problem with the code you currently have is the while True loop, and while it seems natural that that should work, it doesn't seem to work in pygame or even pyglet for that matter.
To rectify this I would create a sprite object and put your key test in the update method similar to what we did in pyglet. Were there any advantages for pygame over pyglet? When I was informed about which one to use I was told pygame might be easier but pyglet is definitely more feature rich, unless it is expected that you use pygame, you might like to consider sticking to pyglet that is if you have the choice.
This post was edited on 09-27-2009 at 01:13 PM by Jarrod.
|
|
09-27-2009 09:35 AM |
|
|
Chancer
Senior Member
Posts: 648 Reputation: 7
35 / /
Joined: May 2005
Status: Away
|
O.P. RE: Python help
Well. I didn't say, but this will control an RC car, and we're planning to have a wheel which will turn right and left when we press right and left.
So I guess this will do the job.
I read this chimp example, but I couldn't get much info. Thanks for this.
I'm using pygame because our teacher told us to. I guess he has experience with pygame...
This post was edited on 09-27-2009 at 03:33 PM by Chancer.
|
|
09-27-2009 02:39 PM |
|
|
Jarrod
Veteran Member
woot simpson
Posts: 1304 Reputation: 20
– / /
Joined: Sep 2006
|
RE: Python help
well it's been a while since I did pygame but hopefully this is kinda right,
python code: import pygame
from pygame.locals import *
class Mover(pygame.sprite.Sprite):
"""moves a clenched fist on the screen, following the mouse"""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
def update(self):
"move the fist based on the mouse position"
pos = pygame.mouse.get_pos()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
return
elif event.type == KEYDOWN: # Verify pressed keys
key = pygame.key.get_pressed()
if key[K_UP] and key[K_RIGHT]:
print 'AD'
elif key[K_UP] and key[K_LEFT]:
print 'AE'
elif key[K_DOWN] and key[K_RIGHT]:
print 'RD'
elif key[K_DOWN] and key[K_LEFT]:
print 'RE'
elif key[K_UP]:
print 'Ax'
elif key[K_DOWN]:
print 'Rx'
elif key[K_RIGHT]:
print 'xD'
elif key[K_LEFT]:
print 'xE'
self.rect.move_ip(5, 10)
def main():
pygame.init()
screen = pygame.display.set_mode((75, 55))
screen = pygame.display.set_caption('Projeto 39')
screen = pygame.display.get_surface()
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 255, 255))
screen.blit(background, (0, 0))
m=Mover()
screen.blit(background, (0, 0))
pygame.display.flip()
main()
note: I haven't tested that but I think that's how pygame does it
|
|
09-27-2009 09:12 PM |
|
|
Chancer
Senior Member
Posts: 648 Reputation: 7
35 / /
Joined: May 2005
Status: Away
|
O.P. RE: Python help
Yeah! I did it
Just changes a little bit that while True loop.
python code: while True:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
return
>>> key = pygame.key.get_pressed() # Added only to check the condition below<<<
>>> while key[K_UP] or key[K_DOWN] or key[K_RIGHT] or key[K_LEFT]<<<
key = pygame.key.get_pressed()
if key[K_UP] and key[K_RIGHT]:
SendData('AD')
elif key[K_UP] and key[K_LEFT]:
SendData('AE')
elif key[K_DOWN] and key[K_RIGHT]:
SendData('RD')
elif key[K_DOWN] and key[K_LEFT]:
SendData('RE')
elif key[K_UP]:
SendData('Ax')
elif key[K_DOWN]:
SendData('Rx')
elif key[K_RIGHT]:
SendData('xD')
elif key[K_LEFT]:
SendData('xE')
>>> pygame.time.delay(10) # Added to have a better control - 10 ms between each loop<<<
>>> pygame.event.pump() # This makes the trick, but I don't know exactly how it works<<<
Also, already added the command to send data via serial.
|
|
09-27-2009 10:47 PM |
|
|
Chancer
Senior Member
Posts: 648 Reputation: 7
35 / /
Joined: May 2005
Status: Away
|
|
10-04-2009 04:05 AM |
|
|
Pages: (2):
« First
«
1
[ 2 ]
Last »
|
|