School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,122 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,962 people online right now. Registration is fast and FREE... Join Now!




[Pygame] moving sprite with arrow keys

 

[Pygame] moving sprite with arrow keys

Animech

9 Oct, 2009 - 03:22 PM
Post #1

New D.I.C Head
*

Joined: 16 Sep, 2008
Posts: 12

I'm working on a 2d top-scroller game and the main problem I'm having right now is moving my ship with the arrow keys. I know I have the right idea, but I'm probably just making a small mistake that I'm not noticing (as usual) here's what I have so far:

CODE

import pygame, random
pygame.init()

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

class Ship(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.screen = screen
        self.image = pygame.image.load("ship.jpg")
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        self.rect.center = (320, 400)
        self.dx = 0
        self.dy = 0
        self.x = self.rect.centerx
        self.y = self.rect.centery

    def update(self):
        self.checkBounds()
    
    def checkBounds(self):
        screen = self.screen
        if self.x > screen.get_width():
            self.x = screen.get_width()
        if self.x < 0:
            self.x = 0

class Missile(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("missile.jpg")
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        self.reset()

    def update(self):
        self.rect.centery += self.dy
        if self.rect.top > screen.get_height():
            self.reset()
    
    def reset(self):
        self.rect.bottom = 0
        self.rect.centerx = random.randrange(0, screen.get_width())
        self.dy = random.randrange(5, 10)

def main():
    background = pygame.Surface(screen.get_size())
    background.fill((0, 0, 0))
    screen.blit(background, (0, 0))
    ship = Ship()
    missile = Missile()
    star = Star()


    allSprites = pygame.sprite.Group(star, ship)
    clock = pygame.time.Clock()
    keepGoing = True
    while keepGoing:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    ship.dx = -10
                elif event.key == pygame.K_RIGHT:
                    ship.dx = 10
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    ship.dx = 0
                elif event.key == pygame.K_RIGHT:
                    ship.dx = 0

        allSprites.clear(screen, background)
        allSprites.update()
        allSprites.draw(screen)
        
        pygame.display.flip()
    pygame.quit()
        
if __name__ == "__main__":
    main()


I also know a way to do it using checkKeys(), but it still doesn't work. I'd really appreciate some help on this and I apologizing if this is ends up being a simple mistake.

User is offlineProfile CardPM
+Quote Post


Animech

RE: [Pygame] Moving Sprite With Arrow Keys

10 Oct, 2009 - 12:25 PM
Post #2

New D.I.C Head
*

Joined: 16 Sep, 2008
Posts: 12

nevermind. i finally got it right
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 01:54PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month