mercredi 22 juin 2016

Gravity in pong PYGAMES


hi I am doing a pong game in python and quisera That ball has gravity as a parable and not how to do it

import pygame, sys`enter code here`
from pygame.locals import *

# Number of frames per second
# Change this value to speed up or slow down your game
FPS = 200

#Global Variables to be used through our program
ancho = 400
alto = 300
linea= 10
paleta = 50
paletafuera = 20


negro = (0  ,0  ,0  )
blanco= (255,255,255)

#Draws the arena the game will be played in. 
def campo():
    pantalla.fill((0,0,0))
    #Draw outline of arena
    pygame.draw.rect(pantalla, blanco, ((0,0),(ancho,alto)), linea*2)
    #Draw centre line
    pygame.draw.line(pantalla, blanco, ((ancho//2),0),((ancho//2),alto), (linea//4))

#Draws the paddle
def paletas(paddle):
    #Stops paddle moving too low
    if paddle.bottom > alto - linea:
        paddle.bottom = alto - linea
    #Stops paddle moving too high
    elif paddle.top < linea:
        paddle.top = linea
    #Draws paddle
    pygame.draw.rect(pantalla, blanco, paddle)

def moverbola(ball,ballDirX,ballDirX):
        """ Calculamos el efecto de la gravedad. """
    if ball.x += ballDirX:
        ball.y += ballDirY
    else:
        ball.y += .35

    return ball


#draws the ball

def bola(ball):
    pygame.draw.rect(pantalla, blanco, ball)

#moves the ball returns new position


#Checks for a collision with a wall, and 'bounces' ball off it.
#Returns new direction
def colision(ball, ballDirX, ballDirY):
    if ball.top == (linea) or ball.bottom == (alto - linea):
        ballDirY = ballDirY * -1
    if ball.left == (linea) or ball.right == (ancho - linea):
        ballDirX = ballDirX * -1
    return ballDirX, ballDirY

#Checks is the ball has hit a paddle, and 'bounces' ball off it.     
def golpe(ball, paddle1, paddle2, ballDirX):
    if ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
        return -1
    elif ballDirX == 1 and paddle2.left == ball.right and paddle2.top < ball.top and paddle2.bottom > ball.bottom:
        return -1
    else: return 1

#Checks to see if a point has been scored returns new score
def puntaje(paddle1, ball, score, ballDirX):
    #reset points if left wall is hit
    if ball.left == linea: 
        return 0
    #1 point for hitting the ball
    elif ballDirX == -1 and paddle1.right == ball.left and paddle1.top < ball.top and paddle1.bottom > ball.bottom:
        score += 1
        return score
    #5 points for beating the other paddle
    elif ball.right == ancho - linea:
        score += 5
        return score
    #if no points scored, return score unchanged
    else: return score

#Artificial Intelligence of computer player 
def artificial(ball, ballDirX, paddle2):
    #If ball is moving away from paddle, center bat
    if ballDirX == -1:
        if paddle2.centery < (ancho/2):
            paddle2.y += 1
        elif paddle2.centery > (alto/2):
            paddle2.y -= 1
    #if ball moving towards bat, track its movement. 
    elif ballDirX == 1:
        if paddle2.centery < ball.centery:
            paddle2.y += 1
        else:
            paddle2.y -=1
    return paddle2

#Displays the current score on the screen
def puntajepantalla(score):
    resultSurf = fuente.render('Puntaje = %s' %(score), True, blanco)
    resultRect = resultSurf.get_rect()
    resultRect.topleft = (ancho - 150, 25)
    pantalla.blit(resultSurf, resultRect)


#Main function
def main():
    pygame.init()
    global pantalla
    ##Font information
    global fuente, tamano
    tamano = 20
    fuente = pygame.font.Font('freesansbold.ttf', tamano)

    FPSCLOCK = pygame.time.Clock()
    pantalla = pygame.display.set_mode((ancho,alto)) 
    pygame.display.set_caption('Pong')

    #Initiate variable and set starting positions
    #any future changes made within rectangles
    ballX = ancho//2 - linea//2
    ballY = alto//2 - linea//2
    posicion1 = (alto - paleta) //2
    posicion2 = (alto - paleta) //2
    score = 0

    #Keeps track of ball direction
    ballDirX = -1 ## -1 = left 1 = right
    ballDirY = -1 ## -1 = up 1 = down

    #Creates Rectangles for ball and paddles.
    paddle1 = pygame.Rect(paletafuera,posicion1, linea,paleta)
    paddle2 = pygame.Rect(ancho - paletafuera - linea, posicion2, linea,paleta)
    ball = pygame.Rect(ballX, ballY, linea, linea)

    #Draws the starting position of the Arena
    campo()
    paletas(paddle1)
    paletas(paddle2)
    bola(ball)

    while True: 
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEMOTION:
                mousex, mousey = event.pos
                paddle1.y = mousey

        campo()
        paletas(paddle1)
        paletas(paddle2)
        bola(ball)


        ball = moverbola(ball, ballDirX, ballDirY)
        ballDirX, ballDirY = colision(ball, ballDirX, ballDirY)
        score = puntaje(paddle1, ball, score, ballDirX)
        ballDirX = ballDirX * golpe(ball, paddle1, paddle2, ballDirX)
        paddle2 = artificial (ball, ballDirX, paddle2)

        puntajepantalla(score)

        pygame.display.update()
        FPSCLOCK.tick(FPS)

if __name__=='__main__':
    main()

please help i dont know how to do it HELP!

I'VE BEEN TRYING SOME IDEAS WITH U PAGES OF TERM BUT DO NOT UNDERSTAND , I find it hard we are doing BETWEEN THREE PEOPLE THANK YOU VERY MUCH FOR YOUR ANSWERS HAVE A NICE DAY


Aucun commentaire:

Enregistrer un commentaire