Currently, all the functions work as expected, but the moving left and right. I want to alter my code to allow me to hold down right and left and for it to continue moving. I know there is a statement with the right syntax, I just dont know how to apply it. As it is I need to tap the right and left keys continuously to move.

# Pygame Practice
import os, sys
import pygame
from pygame.locals import *
screen_mode = (400,480)
colour_green = 0,255,100
def load_image(name, colorkey=None):
    fullname = os.path.join('data', 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()


class scoreboard(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image("scoreboard.bmp", -1)
    def update(self):
        self.rect.topleft = (0,0)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image("PlaneNeutral.bmp", -1)
        self.leanL = load_image("PlaneLeanLeft.bmp", -1)
        self.leanR = load_image("PlaneLeanRight.bmp", -1)
        self.neutral = self.image, self.rect
        self.xpos = 200
        self.ypos = 420
        self.move_right = 0
        self.move_left = 0
    def update(self):
        self.rect.topleft = (self.xpos, self.ypos)
        if self.move_right == 1:
            self._right()
        if self.move_left == 1:
            self._left()
    def _left(self):
        self.xpos = self.xpos - 2
    def _right(self):
        self.xpos = self.xpos + 2

    
def main():
    pygame.init()
    screen = pygame.display.set_mode((screen_mode))
    pygame.display.set_caption('Shooter')

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((colour_green))
    screen.blit(background, (0,0))
    pygame.display.flip()

    clock = pygame.time.Clock()
    scoreBoard = scoreboard()
    player = Player()
    first_layer = pygame.sprite.RenderPlain(player)
    last_layer = pygame.sprite.RenderPlain(scoreBoard)

    while 1:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    player.image, player.rect = player.leanL
                    player._left()
                    print "move left"
                elif event.key == K_RIGHT:
                    player.image, player.rect = player.leanR
                    player._right()
                    print "move right"
                elif event.key == K_SPACE:
                    print "shoot"
            elif event.type == KEYUP:
                player.image, player.rect = player.neutral
                
        first_layer.update()
        last_layer.update()
        screen.blit(background, (0,0))
        first_layer.draw(screen)
        last_layer.draw(screen)
        pygame.display.flip()

if __name__ == '__main__': main()

Please can someone respond? I've fixed the initial problem, but I can't delay the action with the wait or delay command. I want to "barrel roll" my plane so that it rolls across the screen. Without wait commands it moves the right distance, but it is instant. Here's the code:

# Pygame Practice
import os, sys
import pygame


from pygame.locals import *
screen_mode = (400,480)
colour_green = 0,255,100
def load_image(name, colorkey=None):
    fullname = os.path.join('data', 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()


class scoreboard(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image("scoreboard.bmp", -1)
    def update(self):
        self.rect.topleft = (0,0)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image, self.rect = load_image("PlaneNeutral.bmp", -1)
        self.leanL = load_image("PlaneLeanLeft.bmp", -1)
        self.leanR = load_image("PlaneLeanRight.bmp", -1)
        self.RollLeanR = load_image("RollPlaneLeanRight.bmp", -1)
        self.RollLeanL = load_image("RollPlaneLeanLeft.bmp", -1)
        self.neutral = self.image, self.rect
        self.xpos = 200
        self.ypos = 420
        self.move_right = 0
        self.move_left = 0
    def update(self):
        self.rect.topleft = (self.xpos, self.ypos)
        if self.move_right == 1:
            self._right()
        if self.move_left == 1:
            self._left()
    def _left(self):
        self.xpos = self.xpos - 2
    def _right(self):
        self.xpos = self.xpos + 2

    
def main():
    pygame.init()
    screen = pygame.display.set_mode((screen_mode))
    pygame.display.set_caption('Shooter')

    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((colour_green))
    screen.blit(background, (0,0))
    pygame.display.flip()

    clock = pygame.time.Clock()
    scoreBoard = scoreboard()
    player = Player()
    first_layer = pygame.sprite.RenderPlain(player)
    last_layer = pygame.sprite.RenderPlain(scoreBoard)
    pygame.key.set_repeat(20, 2)
    while 1:
        clock.tick(60)
         
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN:
                if event.key == K_LEFT:
                    
                    player.image, player.rect = player.leanL
                    player._left()
                    print "move left"
                elif event.key == K_RIGHT:
                    player.image, player.rect = player.leanR
                    player._right()
                    print "move right"
                elif event.key == K_SPACE:
                    print "shoot"
                elif event.key == K_n:
                    player.image, player.rect = player.leanL
                    player.xpos = player.xpos - 3
                    clock.delay(2)
                    player.image, player.rect = player.RollLeanL
                    player.xpos = player.xpos - 3
                    clock.delay(2)
                    player.image, player.rect = player.RollLeanR
                    player.xpos = player.xpos - 3
                    clock.delay(2)
                    player.image, player.rect = player.leanR
                    player.xpos = player.xpos - 3
                    
            elif event.type == KEYUP:
                player.image, player.rect = player.neutral
                
        first_layer.update()
        last_layer.update()
        screen.blit(background, (0,0))
        first_layer.draw(screen)
        last_layer.draw(screen)
        pygame.display.flip()

if __name__ == '__main__': main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.