[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Object not moving!



I'm the 'point me in the right direction' type, so here's a vague description of where to go to fix this bug. If you want a more explicit answer, let me know!

Take a look at the line where you're blitting the image and look at your inputs to that function. There's a problem there. Take a look at the blit documentation and see what you can do to fix it.

Once you resolve that, you'll see a second bug, but the answer to that one should be pretty easy to figure out.

Happy pygaming!


On Thu, Dec 6, 2012 at 6:59 PM, Elias Benevedes <benevedeselias@xxxxxxxxx> wrote:
Hello everyone! I was having a little bug in my program. I HAVE NO IDEA WHY! I put print statements everywhere to check to make sure everything was being executed. Here is my code:

import pygame, sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((640,480))

class goodGuy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('goodGuy.png')
        self.rect = self.image.get_rect()
    def up(self):
        print 'up'
        self.rect[1] -= 10
    def down(self):
        print 'down'
        self.rect[1] += 10
    def right(self):
        print 'right'
        self.rect[0] += 10
    def left(self):
        print 'left'
        self.rect[0] -= 10
Guy = goodGuy()

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == K_UP:
                Guy.up()
                print 'Up'
            if event.key == K_DOWN:
                Guy.down()
                print 'Down'
            if event.key == K_RIGHT:
                Guy.right()
                print 'Right'
            if event.key == K_LEFT:
                Guy.left()
                print 'Left'
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
        screen.blit(Guy.image, Guy.rect, Guy.rect)
        pygame.display.update()

All that I want to happen is that for each respective arrow key, I want to move the image 10 pixels in that direction. Any help?


--
"The validity of internet quotes are getting sketchy nowadays"
-Abraham Lincoln