samedi 21 avril 2018

python pygame game - My apple go trough the cart

I was watching SentDex, youtuber , about pygame tutorial makinga a "simple" game... So I decided to renovate it, to make my own , but with same flat as his. when you play apples are falling from trees and you have to catch them with the cart, however, when i want to catch it apple go trough , but it doesn't go trough when apple hits the edge. Here is code:

import pygame
import time
import random

pygame.init()
sirina = 800
visina = 600
gameDisplay = pygame.display.set_mode((sirina,visina))
pygame.display.set_caption("Hvatanje Jabuka")
clock = pygame.time.Clock()
jabuka = pygame.image.load("jabuka.png")

gajbaImg = pygame.image.load("gajba.png")
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
def jabuke(x,y):
    gameDisplay.blit(jabuka,(x,y))
def text_objects(text,font):
    textSurface = font.render(text,True,black)
    return textSurface,textSurface.get_rect()




def message_dispaly(text):
    largeText = pygame.font.Font("freesansbold.ttf",90)
    TextSurf, TextRect = text_objects(text,largeText)
    TextRect.center = ((sirina/2),(visina/2))
    gameDisplay.blit(TextSurf,TextRect)
    pygame.display.update()

    time.sleep(2)

    game_loop()


def n_uhvatili():
    message_dispaly("Niste uhvaili jabuku")

def gajba(x,y):
    gameDisplay.blit(gajbaImg,(x,y))

def game_loop():
    #Podatci o jabuki
    sirina_jabuke = 50
    visina_jabuka = 82
    jabuka_startx = random.randrange(0,sirina-sirina_jabuke)
    jabuka_starty = -600
    jabuka_brzina = 8
    x_gajbe = (sirina * 0.45)
    y_gajbe = (visina * 0.8)
    x_change = 0
    gajba_sirina = 193
    gajba_visina = 105

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameEXit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -8
                elif event.key == pygame.K_RIGHT:
                    x_change = 8
                elif event.tyoe == pygame.K_q:
                    pygame.quit()
                    quit()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0



        x_gajbe += x_change

        gameDisplay.fill(white)
        jabuke(jabuka_startx,jabuka_starty)
        jabuka_starty += jabuka_brzina

        if jabuka_starty > visina:
            n_uhvatili()



        gajba(x_gajbe,y_gajbe)
        if x_gajbe > sirina - gajba_sirina:
            x_gajbe = sirina - gajba_sirina
        if x_gajbe < 0:
            x_gajbe = 0
#Note that problem must be in this statement.
        if y_gajbe < jabuka_starty + visina_jabuka:
            print("y crossover")
            if x_gajbe > jabuka_startx and x_gajbe < jabuka_starty + sirina_jabuke or x_gajbe +gajba_sirina > jabuka_startx and x_gajbe + gajba_sirina < jabuka_startx + visina_jabuka:

                print("Uhvatili ste jabuku")
                jabuka_startx = random.randrange(0, sirina - sirina_jabuke)
                jabuka_starty = -50


        pygame.display.update()
        clock.tick(59)
game_loop()
pygame.quit()
quit()

I marked where is the problem. I am not good at programming, I think that not everyone can do it, my logic is bad. Thanks for helping!




Aucun commentaire:

Enregistrer un commentaire