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

Re: [pygame] Why is there no movement in this code.



James is correct.  Here's a way to accomplish what he has pointed out, as well as the code needed for the sprite to move left manually instead of having it move to the left each frame.  Let me know if you have any questions:

#!/usr/bin/env python

import os
import sys
import pygame
from pygame.locals import *

width , height = 1680 , 1050

pygame.init()
fpsclock = pygame.time.Clock()
# fpsclock.tick(50)
FPS = 50
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("SteamPunk")


backfile = os.path.join("images","steampunk.jpg")
backfile_surface = pygame.image.load(backfile)
backfile_surface = pygame.transform.scale(backfile_surface ,(1680,1050))
screen.blit(backfile_surface,(0,0))
pygame.display.update()

def input(events):
    for event in events:
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key in (K_LEFT, ord('w')):
                fighter1.moveleft()
            elif event.key == K_ESCAPE:
                pyg.event.post(pyg.event.Event(QUIT))
        else:
            print event




def load_image(name, colorkey=None):
    fullname = os.path.join('images', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print("Cannot load image:", name)
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer:
        return NoneSound()
    fullname = os.path.join('images', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error, message:
        print("Cannot load sound:", wav)
        raise SystemExit, message
    return sound


class Fighter(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.currentx = 50
        self.currenty = (height - 389)
        self.image, self.rect = load_image("Fighter1.jpg", -1)
        self.rect.topleft = (self.currentx,self.currenty )

    def update(self):

        move = self.rect.move(self.currentx,self.currenty)
        self.rect = move

    def moveleft(self):
        "move the fighter to the left of current position"
        self.currentx += 100


game_over = False

fighter1 = Fighter()
allsprites = pygame.sprite.RenderUpdates()
allsprites.add(fighter1)
rectlist = allsprites.draw(screen)
pygame.display.update(rectlist)


while game_over == False:
    input(pygame.event)
    fighter1.update()
    rectlist = allsprites.draw(screen)
    pygame.display.update()
    fpsclock.tick(FPS)


On Thu, Dec 27, 2012 at 2:31 AM, James Heslin <program.ix.j@xxxxxxxxx> wrote:

My immediate guess is that the last eight or so lines need to be in a while or for loop. From what I can make out they are only called once, ever, by your code.

Hope that's correct/helpful - my first reply to a topic in this list.

James (PROGRAM_IX on Twitter and in #pygame chatroom)

On Dec 27, 2012 2:41 AM, "Discord" <sabatino.steven@xxxxxxxxx> wrote:
#!/usr/bin/env python

import os
import sys
import pygame
from pygame.locals import *

width , height = 1680 , 1050

pygame.init()
fpsclock = pygame.time.Clock()
fpsclock.tick(50)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("SteamPunk")


backfile = os.path.join("images","steampunk.jpg")
backfile_surface = pygame.image.load(backfile)
backfile_surface = pygame.transform.scale(backfile_surface ,(1680,1050))
screen.blit(backfile_surface,(0,0))
pygame.display.update()

def input(events):
   for event in events:
      if event.type == QUIT:
         sys.exit(0)
      else:
         print event




def load_image(name, colorkey=None):
    fullname = os.path.join('images', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print("Cannot load image:", name)
        raise SystemExit, message
    image = image.convert()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

def load_sound(name):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer:
        return NoneSound()
    fullname = os.path.join('images', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error, message:
        print("Cannot load sound:", wav)
        raise SystemExit, message
    return sound


class Fighter(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.currentx = 50
        self.currenty = (height - 389)
        self.image, self.rect = load_image("Fighter1.jpg", -1)
        self.rect.topleft = (self.currentx,self.currenty )

    def update(self):

        move = self.rect.move(self.currentx,self.currenty)
        self.rect = move

    def moveleft(self):
        "move the fighter to the left of current position"
        self.currentx += 100



fighter1 = Fighter()
allsprites = pygame.sprite.RenderUpdates()
allsprites.add(fighter1)
rectlist = allsprites.draw(screen)
pygame.display.update(rectlist)

fighter1.moveleft()
fighter1.update()
rectlist = allsprites.draw(screen)
pygame.display.update()



--
View this message in context: http://pygame-users.25799.n6.nabble.com/Why-is-there-no-movement-in-this-code-tp419.html
Sent from the pygame-users mailing list archive at Nabble.com.