Anagha G
Anagha G

Reputation: 53

Egg and basket game in python

I am a beginner in python programming. I got a course project to make a game using python. I tried making the egg and basket game using pygame. The game is working to some extent. I am able to make the egg fall and move the basket on key press, but I am not able to make egg fall continuously i.e. just one egg falls and after that it stops falling. I am expecting eggs to fall one after other like in the actual game.

And I have no idea how to know when the egg falls in the basket and how to increase the score when it falls in the basket.

Can you please help me out??

Screenshot of my game

#The egg and basket game

import pygame
from pygame.locals import *
import time
import random
clock = pygame.time.Clock()
x=260
y=500
#Screen initialize
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode((600,600))
pygame.display.set_caption("egg")

#Background
cloud=pygame.image.load("clouds.jpg")
cloud=pygame.transform.scale(cloud,(600,600))
screen.blit(cloud,(0,0))

#Basket
basket=pygame.image.load("basket.jpg")
basket=pygame.transform.scale(basket,(80,80))
screen.blit(basket,(x,y))
pygame.display.update()

#egg
egg=pygame.image.load("egg.jpg")
egg=pygame.transform.scale(egg,(20,20))

#screen.blit(egg,(290,20))
pygame.display.update()

#Movement of basket
ychange=0
xchange=0
exiting=False
for yegg in range(20,550):

#for i in range(0,100):
xegg=random.randrange(50,550)
    while not exiting:    
   #xegg=random.randrange(50,550)
    #for yegg in range(20,550):
        if yegg<550:
            ychange+=1
            pygame.display.update()
            clock.tick(60)
            screen.blit(egg,(xegg,ychange))

        else:
            yegg=20
            yegg=yegg+ychange    
            pygame.display.update()
            clock.tick(60)
            screen.blit(egg,(xegg,yegg))
        #yegg=20                    
        pygame.display.update()
        clock.tick(60)
    #yegg=20

        for event in pygame.event.get():
            print(event)
            if(event.type==pygame.QUIT):
                exiting=True
                pygame.quit()
                quit()
            if(event.type==pygame.KEYDOWN):
                if(event.key==pygame.K_LEFT):
                    xchange=-5
                if(event.key==pygame.K_RIGHT):
                    xchange=5
            screen.blit(basket,(x,y))
            if(event.type==pygame.KEYUP):
                if(event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT):
                    xchange=0

            x=x+xchange
            print(x)
            screen.blit(cloud,(0,0))

            screen.blit(basket,(x,y))

            pygame.display.update()
            clock.tick(60)

    i=i+1
    ychange=0
    #random position of eggs

    #MOVEMENT OF egg

Upvotes: 1

Views: 2786

Answers (1)

emorphus
emorphus

Reputation: 560

First of all use .png images to make the white square around the images to disappear. Use pygame.image.load("myimage.png").convert_alpha()

xegg=random.randrange(50,550)

the above line should be inside the while loop so that you get random x values each iteration. I've done some changes to you code and now the eggs fall from random positions. To catch them you must check for collisions between the basket and the eggs.

#The egg and basket game

import pygame
from pygame.locals import *
import time
import random
clock = pygame.time.Clock()
x=260
y=500
#Screen initialize
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode((600,600))
pygame.display.set_caption("egg")

#Background
cloud=pygame.image.load("clouds.png").convert_alpha()
cloud=pygame.transform.scale(cloud,(600,600))
screen.blit(cloud,(0,0))

#Basket
basket=pygame.image.load("basket.png").convert_alpha()
basket=pygame.transform.scale(basket,(80,80))
screen.blit(basket,(x,y))
pygame.display.update()

#egg
egg=pygame.image.load("eggs.png").convert_alpha()
egg=pygame.transform.scale(egg,(20,20))

#screen.blit(egg,(290,20))
pygame.display.update()

#Movement of basket
ychange=0
xchange=0
exiting=False


xegg = random.randrange(50,550)
yegg = 20
while not exiting:
#xegg=random.randrange(50,550)
#for yegg in range(20,550):
    print yegg
    if yegg<550:
        yegg += 5
        pygame.display.update()
        clock.tick(60)
        screen.blit(egg,(xegg,yegg))

    else:
        yegg=20
        xegg = random.randrange(50,550)
        yegg=yegg+ychange
        pygame.display.update()
        clock.tick(60)
        screen.blit(egg,(xegg,yegg))
    #yegg=20
    pygame.display.update()
    clock.tick(60)
    #yegg=20

    for event in pygame.event.get():
        print(event)
        if(event.type==pygame.QUIT):
            exiting=True
            pygame.quit()
            quit()
        if(event.type==pygame.KEYDOWN):
            if(event.key==pygame.K_LEFT):
                xchange=-5
            if(event.key==pygame.K_RIGHT):
                xchange=5
        screen.blit(basket,(x,y))
        if(event.type==pygame.KEYUP):
            if(event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT):
                xchange=0

        x=x+xchange
        print(x)
        screen.blit(cloud,(0,0))

        screen.blit(basket,(x,y))

        pygame.display.update()
        clock.tick(60)

i=i+1
ychange=0
    #random position of eggs

    #MOVEMENT OF egg

Go to pygame collisions and learn collisons. Go here for some very good examples. Also here for an excellent Pygame guide.

Upvotes: 1

Related Questions