├── .gitignore ├── Font.ttf ├── Game.py ├── LICENSE ├── README.md ├── hit.wav ├── ping.wav ├── pong.ogg └── score.ogg /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /Font.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7shantanu7/Retro-Pong/98f9f45d765782cd9310c9d46a101ffc1d8aab87/Font.ttf -------------------------------------------------------------------------------- /Game.py: -------------------------------------------------------------------------------- 1 | import pygame, sys, random, time 2 | pygame.mixer.pre_init(44100, -16, 1, 512) 3 | pygame.init() 4 | clock = pygame.time.Clock() 5 | 6 | #functions 7 | def ball_anim(): 8 | global ball_speed_x, ball_speed_y,player_score,opponent_score 9 | ball.x += ball_speed_x 10 | ball.y += ball_speed_y 11 | if ball.top <=0 or ball.bottom >= screen_height: 12 | pygame.mixer.Sound.play(pong_sound) 13 | ball_speed_y *= -1 14 | 15 | if ball.colliderect(player) or ball.colliderect(opponent): 16 | pygame.mixer.Sound.play(hit_sound) 17 | ball_speed_x *= -1 18 | 19 | if ball.left <=0: 20 | pygame.mixer.Sound.play(player_sound) 21 | ball_restart() 22 | player_score += 1 23 | 24 | if ball.right>=screen_width: 25 | pygame.mixer.Sound.play(score_sound) 26 | ball_restart() 27 | opponent_score += 1 28 | 29 | def ball_restart(): 30 | global ball_speed_y,ball_speed_x 31 | ball.center = (screen_width/2, screen_height/2) 32 | ball_speed_x *= random.choice((-1,1)) 33 | ball_speed_y *= random.choice((-1,1)) 34 | time.sleep(1) 35 | 36 | def player_anim(): 37 | player.y += player_speed 38 | if player.top<=0: 39 | player.top = 0 40 | if player.bottom>=screen_height: 41 | player.bottom = screen_height 42 | 43 | def opponent_ai(): 44 | #CPU player 45 | if opponent.top < ball.y: 46 | opponent.y += opponent_speed 47 | if opponent.bottom > ball.y: 48 | opponent.y -= opponent_speed 49 | 50 | if opponent.top <= 0: 51 | opponent.top = 0 52 | if opponent.bottom >= screen_height: 53 | opponent.bottom = screen_height 54 | 55 | #Layout 56 | screen_width = 1280 57 | screen_height = 600 58 | screen = pygame.display.set_mode((screen_width, screen_height)) 59 | pygame.display.set_caption('Retro Pong') 60 | 61 | #elements 62 | ball = pygame.Rect(screen_width/2-14, screen_height/2-14,28,28) 63 | player = pygame.Rect(screen_width-20, screen_height/2-75,10,150) 64 | opponent = pygame.Rect(10, screen_height/2-75,10,150) 65 | 66 | #colors 67 | bg_color = pygame.Color('#2F373F') 68 | light_grey = pygame.Color(255,0,0) 69 | bluish = (27,35,43) 70 | 71 | #variables 72 | ball_speed_x = 6*random.choice((-1,1)) 73 | ball_speed_y = 6*random.choice((-1,1)) 74 | player_speed = 0 75 | opponent_speed = 10 76 | player_score = 0 77 | opponent_score = 0 78 | game_font = pygame.font.Font("Font.ttf", 48) 79 | score_font = pygame.font.Font("Font.ttf", 48) 80 | pong_sound = pygame.mixer.Sound("pong.ogg") 81 | score_sound = pygame.mixer.Sound("score.ogg") 82 | player_sound = pygame.mixer.Sound("ping.wav") 83 | hit_sound = pygame.mixer.Sound("hit.wav") 84 | 85 | #main loop 86 | while True: 87 | for event in pygame.event.get(): 88 | if event.type == pygame.QUIT: 89 | pygame.quit() 90 | sys.exit() 91 | 92 | if event.type == pygame.KEYDOWN: 93 | if event.key == pygame.K_DOWN: 94 | player_speed +=7 95 | if event.key == pygame.K_UP: 96 | player_speed = -7 97 | 98 | if event.type == pygame.KEYUP: 99 | if event.key == pygame.K_DOWN: 100 | player_speed -=7 101 | if event.key == pygame.K_UP: 102 | player_speed +=7 103 | 104 | if player_score - opponent_score >= 10: 105 | ball_speed_x+=1 106 | opponent_speed+=5 107 | player_speed+=2 108 | 109 | #calling some functions 110 | ball_anim() 111 | player_anim() 112 | opponent_ai() 113 | 114 | # Visuals 115 | screen.fill(bg_color) 116 | pygame.draw.rect(screen, light_grey, player) 117 | pygame.draw.rect(screen, (255,0,0), opponent) 118 | pygame.draw.ellipse(screen, (255,255,255), ball) 119 | pygame.draw.line(screen, bluish, (screen_width/2,110), (screen_width/2,screen_height),5) 120 | pygame.draw.line(screen, bluish, (screen_width/2,0), (screen_width/2,64),5) 121 | 122 | #displayed text 123 | text = score_font.render("SCORE",False,(0,255,0)) 124 | screen.blit(text,(screen_width/2-82,70)) 125 | player_text = game_font.render(f"{player_score}",False,(255,255,255)) 126 | screen.blit(player_text,(660,120)) 127 | opponent_text = game_font.render(f"{opponent_score}",False,(255,255,255)) 128 | screen.blit(opponent_text,(600,120)) 129 | 130 | #cycles 131 | pygame.display.flip() 132 | clock.tick(60) 133 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Shantanu Z 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Retro-Pong 2 | A simple 1 player Retro style Pong game created using Pygame. 3 | 4 | Pygame module needs to be installed : 5 | ```bash 6 | pip install pygame 7 | ``` 8 | [Video](https://www.reddit.com/r/Python/comments/hg2m6i/my_first_pygame_project/?utm_source=share&utm_medium=web2x) 9 | 10 | ## Contributing 11 | Pull requests are welcome. 12 | -------------------------------------------------------------------------------- /hit.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7shantanu7/Retro-Pong/98f9f45d765782cd9310c9d46a101ffc1d8aab87/hit.wav -------------------------------------------------------------------------------- /ping.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7shantanu7/Retro-Pong/98f9f45d765782cd9310c9d46a101ffc1d8aab87/ping.wav -------------------------------------------------------------------------------- /pong.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7shantanu7/Retro-Pong/98f9f45d765782cd9310c9d46a101ffc1d8aab87/pong.ogg -------------------------------------------------------------------------------- /score.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/7shantanu7/Retro-Pong/98f9f45d765782cd9310c9d46a101ffc1d8aab87/score.ogg --------------------------------------------------------------------------------