Created
August 26, 2020 10:21
-
-
Save alvintian/d4c68201be7874c7ef6c69264eb93f8e to your computer and use it in GitHub Desktop.
A Pong game made with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
pygame.init() | |
#Variables | |
sc_width = 800 | |
sc_height = 600 | |
ballx = 2 | |
bally = 2 | |
clock = pygame.time.Clock() | |
player1score =0 | |
player2score =0 | |
ballspeed=125 | |
start =False | |
screen = pygame.display.set_mode((sc_width, sc_height)) | |
font = pygame.font.SysFont('Arial',30) | |
run = False | |
colors = [(255,0,0),(0.255,0),(0,0,255),(0,0,0),(128,0,128)] | |
colorindex1 = 0 | |
colorindex2 = 0 | |
#Game Rectangles | |
player1 = pygame.Rect(15, sc_height/2 - 70, 10, 200) | |
player2 = pygame.Rect(780, sc_height/2 - 70, 10, 200) | |
#ball movement | |
ball =pygame.Rect(screen.get_rect().centerx-10, screen.get_rect().centery-10, 20,20) | |
def draw_screen(): | |
screen.fill((255,0,0)) | |
pygame.draw.rect(screen, colors[colorindex1], player1) | |
pygame.draw.rect(screen, colors[colorindex2], player2) | |
pygame.draw.rect(screen, (0,0,0), player2) | |
pygame.draw.ellipse(screen, (0, 0, 255), ball) | |
pygame.draw.aaline(screen,(0,0,0),(sc_width/2,10),(sc_width/2,sc_height-10)) | |
pygame.draw.aaline(screen,(0,0,0),(10,10),(10,sc_height-10)) | |
pygame.draw.aaline(screen,(0,0,0),(sc_width-10,10),(sc_width-10,sc_height-10)) | |
pygame.draw.aaline(screen,(0,0,0),(10,10),(sc_width-10,10)) | |
pygame.draw.aaline(screen,(0,0,0),(10,sc_height-10),(sc_width-10,sc_height-10)) | |
points = font.render("P1:"+str(player1score)+" Ballspeed: "+str(ballspeed)+" P2:"+str(player2score),True,(0,0,0)) | |
screen.blit(points,(100,70)) | |
def ball_move(): | |
global ballx | |
global bally | |
global player2score | |
global player1score | |
global ballspeed | |
ball.x=ball.x+ ballx | |
ball.y=ball.y+ bally | |
if ball.top <= 10 or ball.bottom >= sc_height -10: | |
bally *=-1 | |
if ball.left <= 10 or ball.right >= sc_width -10: | |
ballx *=-1 | |
if ball.colliderect(player1) or ball.colliderect(player2): | |
ballx *=-1 | |
if ball.left <= 10 or ball.right >= sc_width -10: | |
if ball.left <=10: | |
player2score = player2score +1 | |
ballspeed+=50 | |
if ball.right >= sc_width -10: | |
player1score = player1score +1 | |
ballspeed+=50 | |
ball.x = sc_width/2-15 | |
ball.y = sc_height/2-15 | |
pygame.time.delay(ballspeed) | |
while run == False: | |
screen.fill((255,255,255)) | |
text = font.render("welcome to pong! hit down + enter to begin",True,(0,0,0)) | |
screen.blit(text,(100,sc_height/2)) | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
#make an if statment that checks for | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_RETURN: | |
run = True | |
pygame.display.update() | |
while True: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
# if event.type == pygame.KEYDOWN: | |
# if event.key == pygame.K_RETURN: | |
# start = True | |
keys = pygame.key.get_pressed() | |
if keys[pygame.K_w]: | |
if player1.top > 15: | |
player1.y -= 2 | |
if keys[pygame.K_s]: | |
if player1.bottom < sc_height - 15: | |
player1.y += 2 | |
if keys[pygame.K_UP]: | |
if player2.top > 15: | |
player2.y -= 2 | |
if keys[pygame.K_DOWN]: | |
if player2.bottom < sc_height - 15: | |
player2.y += 2 | |
ball_move() | |
draw_screen() | |
pygame.display.update() | |
#pygame.display.flip() | |
clock.tick(ballspeed) | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment