user3064057
user3064057

Reputation: 3

How do you make an image move through the screen and then make it disappear in python?

I am in an Intro to programming class, and we have to make our owm game using pygame. I want my game to be a man trying to avoid falling objects. I have gotten the man to move but i cant figure out a scoring system and how to get the image to fall and then disappear. this is what i have:

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

green = (0,255,0)

white = (255,255,255)

red = (255,0,0)

black = (0,0,0)


def draw_stick_figure(screen,x,y):

    # Head
    pygame.draw.ellipse(screen,black,[1+x,y,10,10],0)

    # Legs
    pygame.draw.line(screen,black,[5+x,17+y],[10+x,27+y],2)
    pygame.draw.line(screen,black,[5+x,17+y],[x,27+y],2)

    # Body
    pygame.draw.line(screen,green,[5+x,17+y],[5+x,7+y],2)

    # Arms
    pygame.draw.line(screen,green,[5+x,7+y],[9+x,17+y],2)
    pygame.draw.line(screen,green,[5+x,7+y],[1+x,17+y],2)

pygame.init()

size = [700,500]

screen = pygame.display.set_mode(size)


x_coord=350

y_coord=250


x_speed=0

y_speed=0

done = False

clock=pygame.time.Clock()

while done == False:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            done = True

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:

                x_speed = -3

            elif event.key == pygame.K_RIGHT:

                x_speed = 3

            elif event.key == pygame.K_UP:

                y_speed = -3

            elif event.key == pygame.K_DOWN:

                y_speed = 3

        elif event.type == pygame.KEYUP:

            if event.key == pygame.K_LEFT:

                x_speed = 0

            elif event.key == pygame.K_RIGHT:

                x_speed = 0

            elif event.key == pygame.K_UP:

                y_speed = 0

            elif event.key == pygame.K_DOWN:

                y_speed = 0



    x_coord = x_coord + x_speed

    y_coord = y_coord + y_speed


    screen.fill(red)


    draw_stick_figure(screen,x_coord,y_coord)


    pygame.display.flip()

    clock.tick(20)
pygame.quit()

Upvotes: 0

Views: 2548

Answers (2)

Serial
Serial

Reputation: 8043

What you can do is use the sprite class

make a class for a block like so:

from pygame.locals import *
import pygame
import os
#added spaces show up as part of the code

class Block(pygame.sprite.Sprite):
    def __init__(self, pos):

        #make it a sprite
        pygame.sprite.Sprite.__init__(self)

        #create a rect at position (pos) that is 25 by 25 pixels
        self.image = pygame.Rect(pos[0], pos[1] 25, 25)

        #make the rect a class variable that can be moved
        self.rect = self.image.get_rect()

   def update(self):
       #move rect 20 pixels each update (can be adjusted)
       self.rect.y += 20
       #if it goes to to the bottom of the screen delete it
       if self.rect.y > screen_height:
           self.kill()

then create a block like this:

block = Block([20, 20])

then draw it and update it in the main loop like so:

block.update()
block.draw()

you can create multiple blocks that will fall by using a sprite group:

block_list = pygame.sprite.Group()
for i in xrange(10):
    block = Block([i,i])
    block_list.add(block)

now you can update the whole group the same way:

block_list.draw()
block_list.update()

you should do the same thing with the player and use an image instead of drawing him with lines

the Sprite class

the Rect class

Im sorry if i overwhelmed you it is very hard at first but very useful to learn

Good luck :)

Upvotes: 2

furas
furas

Reputation: 142651

Load image. Set image x, y, y_speed. Change image position in main loop. If image y is bigger than screen height then set image y to 0 to use it again. Use pygame.Rect() for image and player and you will can to use pygame.rect.collision_rect() to detect collision betwin player and image and change score.

And etc.

Upvotes: 0

Related Questions