0
votes

I am new to pygame and I am making a side scrolling game that uses a bird as the character and it flies but I am trying to get it to move up and down on the screen but I can't figure out how.

import pygame
from pygame.locals import *
import os
import sys
import time
pygame.init()

class Fly:
    def __init__(self):
        self.last_update = time.clock()

        self.screen = pygame.display.set_mode((700, 400), 0, 32)
        #Load bird
        self.bird_state = 1
        self.bird_frames = []
        for r in xrange(1, 5):
            self.bird_frames.append(pygame.image.load('bird%s.png' % r))
        self.bg = pygame.image.load('bg.png').convert()

        self.Loop()

    def eventLoop(self):
        'Take and process input from perephirals'
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

    def Update(self):
        self.bird_state += 1
        if self.bird_state > 4:
            self.bird_state = 1

    def Draw(self):
        self.screen.blit(self.bg, [0, 0])
        self.screen.blit(self.bird_frames[self.bird_state - 1], (150, 150))

    def Loop(self):
        while 1:
            self.eventLoop()
            if time.clock() - self.last_update > 0.15:
                self.Update()
                self.last_update = time.clock()
            self.Draw()
            pygame.display.update()

Fly()
2
can you be more clear about your question, what did you tried ect ect - S.Visser

2 Answers

2
votes
import pygame
from pygame.locals import *
import os
import sys
import time
pygame.init()

class Fly:
    def __init__(self):
        self.last_update = time.clock()

        self.screen = pygame.display.set_mode((700, 400), 0, 32)
        #Load bird
        self.bird_state = 1
        self.bird_frames = []
        for r in xrange(1, 5):
            self.bird_frames.append(pygame.image.load('bird%s.png' % r))
        self.bg = pygame.image.load('bg.png').convert()

        self.Loop()

    def eventLoop(self):
        'Take and process input from perephirals'
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

    def Update(self):
        self.bird_state += 1
        if self.bird_state > 4:
            self.bird_state = 1

    def Draw(self):
        self.screen.blit(self.bg, [0, 0])
        self.screen.blit(self.bird_frames[self.bird_state - 1], (150, 150))

    def Loop(self):
        while 1:
            self.eventLoop()

            k = pygame.key.get_pressed()
            if k[K_DOWN]:
                # Do something with your bird
            if k[K_UP]:
                # Do something with your bird

            if time.clock() - self.last_update > 0.15:
                self.Update()
                self.last_update = time.clock()
            self.Draw()
            pygame.display.update()

Fly()

The pygame.key.get_pressed will check if you pressed a button. Check the reference for what kind of options you have there. http://www.pygame.org/docs/ref/key.html

Place this in your loop because then its "realtime".

 k = pygame.key.get_pressed()
 if k[K_DOWN]:
    # Do somthing with your bird
  if k[K_UP]:
    # Do somthing with your bird
0
votes

If you think of your Player as a rectangle (this could apply to all of your sprites in your game) then all you have to do is set up two parameters for your rectangle called self.speedx and self.speedy. Since you you're looking for only up and down movement, speedx would not change, but you would adjust speedy depending on what button is pressed. Here's a snippet of code from one of my games. It is part of the update def for my player class. Unfortunately, in this game I just wanted lateral (left and right) movement, but you'll get the idea. If you need anymore help, let me know. Here's the code:

    def update(self):
    # Ensures default motion is 0
    self.speedx = 0
    self.speedy = 0
    # This checks to see what key's are pressed
    keystate = pygame.key.get_pressed()

    # This gives control for movement (Can use WASD or arrow keys)
    if keystate[pygame.K_a] or keystate[pygame.K_LEFT]:
        self.speedx = -5
    if keystate[pygame.K_d] or keystate[pygame.K_RIGHT]:
        self.speedx = 5
    if keystate[pygame.K_SPACE]:
        self.shoot(bullet_img)

    # Gives movement based on keys that are pressed
    self.rect.x += self.speedx
    self.rect.y += self.speedy