pong.py

Created by valmontechno

Created on September 18, 2023

2.33 KB


from math import *
from kandinsky import *
from ion import *
from time import sleep
from random import choice

print('Pong')

width = 320
height = 222

bgColor = (60,60,60)
titleColor = (255,183,52)
ballColor = (255,183,52)
padColor = (255,255,255)
textColor = (255,255,255)

ballSize = 5
padG = 5
padW = 5
padH = 30
padSpeed = 4
ballSpeed = 1
ballAccel = 0.2

bestScore = 0

while True:

    game = True
    score = 0
    ballX = floor(width/2)
    ballY = floor(height/2)
    ballA = choice((-ballSpeed, ballSpeed))
    ballB = choice((-ballSpeed, ballSpeed))
    pad1Y = floor(height/2 - padH/2)

    fill_rect(0, 0, width, height, bgColor)
    scoreText = 'Best: ' + str(bestScore)
    draw_string('Press OK', 125,height-50,textColor,bgColor)
    draw_string(scoreText,int(width/2)-len(scoreText)*5,padG,textColor,bgColor)
    fill_rect(padG, pad1Y, padW, padH, padColor)
    fill_rect(width-padG-padW, pad1Y, padW, padH, padColor)
    draw_string(' P.O.N.G ',120,100,bgColor,titleColor)
    
    while not (keydown(KEY_OK) or keydown(KEY_EXE)): pass
    while keydown(KEY_OK) or keydown(KEY_EXE): pass

    while game:

        ballX += ballA
        if ballX <= padG+padW or ballX+ballSize >= width-padG-padW:
            if ballY+ballSize >= pad1Y and ballY <= pad1Y+padH:
                ballA *= -1
                ballA += copysign(ballAccel, ballA)
                ballB += copysign(ballAccel, ballB)
                score += 1
            else:
                game = False

        elif ballX+ballSize >= width:
           ballA *= -1
            
        ballY += ballB
        if ballY <= 0 or ballY+ballSize >= height:
            ballB *= -1

        if keydown(KEY_UP):
            pad1Y -= padSpeed
            if pad1Y < 0:
                pad1Y = 0
                
        if keydown(KEY_DOWN):
            pad1Y += padSpeed
            if pad1Y+padH > height:
                pad1Y = height-padH
        
        fill_rect(0, 0, width, height, bgColor)
        scoreText = str(score)
        draw_string(scoreText,int(width/2)-len(scoreText)*5,padG,textColor,bgColor)
        fill_rect(padG, pad1Y, padW, padH, padColor)
        fill_rect(width-padG-padW, pad1Y, padW, padH, padColor)
        fill_rect(int(ballX), int(ballY), ballSize, ballSize, ballColor)
        sleep(0.02)

    if score > bestScore:
        bestScore = score

# by Valmontechno

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.