Skip to content

Commit

Permalink
Add initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
skamieniarz committed Oct 27, 2019
0 parents commit be35b05
Show file tree
Hide file tree
Showing 15 changed files with 632 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
*.pyc
.DS_store
.vscode/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 skamieniarz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
pyxel = "*"

[requires]
python_version = "3.7"
49 changes: 49 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# PyPong #

A slightly modified clone of [Pong](https://en.wikipedia.org/wiki/Pong)
classic game implemented with Python and [pyxel](https://github.com/kitao/pyxel).


![Demo!](https://github.com/skamieniarz/pypong/blob/master/assets/demo.gif)

## Prerequisites: ##

1. Cloned or downloaded repository.
2. [Python3](https://www.python.org) installed.
3. [pyxel](https://github.com/kitao/pyxel#how-to-install) installed. Manually by `pip install pyxel` or by `pipenv sync` in the root of the repository.
Installation with [Pipenv](https://github.com/pypa/pipenv) will make sure that all dependencies are correct.

## Starting: ##
Run `python main.py` in the terminal while in the root of the repository.

## Controls: ##

1. **** and **** arrow keys to select options in menus.
2. **ENTER** to choose option in menus.
3. **W** and **S** to move the first player's paddle up and down.
4. **** and **** arrow keys to move the second player's paddle up and down.
5. **Q** or **P** to pause the game.
6. **ESC** to quit the game.

## Features: ##

1. Navigation menus.
2. Single-player mode with three levels of difficulty.
3. Two-players mode.
4. Two trajectories of the ball (possibility to "curve" the ball).

## Ideas for further development: ##

1. Sound effects.
Binary file added assets/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
''' Module containing constants. '''

# SCREEN
SCREEN_WIDTH = 254
SCREEN_WIDTH_ONE_QUARTER = SCREEN_WIDTH * 0.25
SCREEN_WIDTH_HALF = SCREEN_WIDTH * 0.5
SCREEN_WIDTH_TWO_THIRDS = SCREEN_WIDTH * 0.67
SCREEN_WIDTH_THREE_QUARTERS = SCREEN_WIDTH * 0.75
SCREEN_HEIGHT = 150
SCREEN_HEIGHT_ONE_QUARTER = SCREEN_HEIGHT * 0.25
SCREEN_HEIGHT_HALF = SCREEN_HEIGHT * 0.5
SCREEN_HEIGHT_THREE_QUARTERS = SCREEN_HEIGHT * 0.75
SCALE = 3

# MARGINS
SMALL_MARGIN = 5
MEDIUM_MARGIN = 10
BIG_MARGIN = 20

# COLORS
BLACK = 0
DARK_GREEN = 3
DARK_GREY = 5
WHITE = 7
RED = 8
ORANGE = 9
BLUE = 12

# PADDLE
PADDLE_LENGTH = 20
PADDLE_WIDTH = 2
PADDLE_HALF = PADDLE_LENGTH * 0.5
PADDLE_SPEED = 3
PADDLE_TIP = 3

# BALL
BALL_RADIUS = 2
BALL_SPEED = 4

# MENUS
MENU_SCREENS = ['start', 'controls', 'bot', 'pause', 'win']
START_SCREEN = ['Single-player mode', 'Two-players mode', 'Controls', 'Quit']
CONTROLS_SCREEN_OPTIONS = ['Back']
BOT_SCREEN_OPTIONS = ['Easy', 'Medium', 'Hard', 'Back']
PAUSE_SCREEN_OPTIONS = ['Continue', 'Restart']
WIN_SCREEN = ['Play again', 'Quit']
CONTROLS = [
'W and S to move the first player\'s paddle,',
'UP and DOWN arrows to move the second player\'s paddle,',
'Q or P to pause the game,', 'ESC to quit the game.'
]
Empty file added game_objects/__init__.py
Empty file.
89 changes: 89 additions & 0 deletions game_objects/ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
''' Module containing Paddle class. '''

import consts
import pyxel

from time import time
from math import isclose
from random import randint, getrandbits


class Ball:
''' Class containing methods and variables related to the ball. '''
def __init__(self, x_direction_right):
self.x = consts.BIG_MARGIN + (
consts.BALL_RADIUS * 2
) if x_direction_right else consts.SCREEN_WIDTH - consts.BIG_MARGIN - (
consts.BALL_RADIUS * 2) + 1
self.y = randint(int(consts.SCREEN_HEIGHT_ONE_QUARTER),
int(consts.SCREEN_HEIGHT_THREE_QUARTERS))
self.speed = consts.BALL_SPEED
self.x_direction_right = x_direction_right
self.y_direction_up = bool(getrandbits(1))
self.curved = False

def draw(self):
''' Draws a ball. '''
pyxel.circ(self.x, self.y, consts.BALL_RADIUS, consts.ORANGE)

def update_position(self):
''' Updates ball's position according to current direction. '''

extra_speed = 0 if self.curved else 1

if self.x_direction_right and self.y_direction_up:
self.x += self.speed + extra_speed
self.y -= self.speed - extra_speed

elif not self.x_direction_right and self.y_direction_up:
self.x -= self.speed + extra_speed
self.y -= self.speed - extra_speed

elif self.x_direction_right and not self.y_direction_up:
self.x += self.speed + extra_speed
self.y += self.speed - extra_speed

else:
self.x -= self.speed + extra_speed
self.y += self.speed - extra_speed

def paddle_bounce(self, paddle):
''' Changes ball's direction and curve when bouncing off the
paddle. '''
paddle_edge = paddle.x + 1 if paddle.player == 1 else paddle.x
if isclose(self.x, paddle_edge, abs_tol=consts.BALL_RADIUS):
if (self.y + consts.BALL_RADIUS >=
paddle.y) and (self.y - consts.BALL_RADIUS <=
paddle.y + consts.PADDLE_LENGTH):
self.x_direction_right = not self.x_direction_right
if (self.y + consts.BALL_RADIUS >
paddle.y + consts.PADDLE_LENGTH -
consts.PADDLE_TIP) or (self.y - consts.BALL_RADIUS <
paddle.y + consts.PADDLE_TIP):
self.curved = True
else:
self.curved = False

def edge_bounce(self):
''' Changes ball's direction when bouncing off the table's edge. '''
if self.y - consts.BALL_RADIUS <= consts.MEDIUM_MARGIN:
self.y_direction_up = not self.y_direction_up
elif self.y + consts.BALL_RADIUS >= (consts.SCREEN_HEIGHT -
consts.MEDIUM_MARGIN):
self.y_direction_up = not self.y_direction_up

def score_event(self, game):
''' Increments paddle's score when ball is out of the table on the
opponent's side and repositions the ball. Returns time of the
score. '''
score_time = 0
if self.x >= consts.SCREEN_WIDTH or self.x <= 0:
if self.x >= consts.SCREEN_WIDTH:
game.first_paddle.score += 1
game.ball = Ball(x_direction_right=False)
else:
game.second_paddle.score += 1
game.ball = Ball(x_direction_right=True)
game.state = 'score'
score_time = time()
return score_time
36 changes: 36 additions & 0 deletions game_objects/bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
''' Module containing Bot class. '''

import consts


class Bot:
''' Class containing methods and variables related to the game bot. '''
def __init__(self, difficulty):
self.difficulty = difficulty

def update_paddle_position(self, ball, paddle):
''' Updates paddle position according to current position of the
ball and bot's difficulty. '''
if self.difficulty == 1:
if not ball.curved:
movement_start = consts.SCREEN_WIDTH_THREE_QUARTERS
else:
movement_start = consts.SCREEN_WIDTH_ONE_QUARTER
elif self.difficulty == 2:
movement_start = consts.SCREEN_WIDTH_HALF
else:
if not ball.curved:
movement_start = consts.SCREEN_WIDTH_HALF
else:
movement_start = consts.SCREEN_WIDTH_TWO_THIRDS

if ball.x <= movement_start or ball.x_direction_right is False:
if paddle.y + consts.PADDLE_HALF < consts.SCREEN_HEIGHT_HALF:
paddle.y += consts.PADDLE_SPEED
elif paddle.y + consts.PADDLE_HALF > consts.SCREEN_HEIGHT_HALF:
paddle.y -= consts.PADDLE_SPEED
else:
if ball.y <= paddle.y + consts.PADDLE_HALF:
paddle.y -= consts.PADDLE_SPEED
elif ball.y >= paddle.y + consts.PADDLE_HALF:
paddle.y += consts.PADDLE_SPEED
57 changes: 57 additions & 0 deletions game_objects/menus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
''' Module containing Menus related classes. '''

import consts
import pyxel

from helpers import get_center_x_coordinate, get_center_y_coordinate


class Menus:
''' Class containing methods and variables related to game's menus like
start or pause menus. '''
def __init__(self, options):
self.options = options
self.selected_option = 0
self.options_amount = len(self.options)

def draw(self):
''' Draws menu options and currently selected item. '''
longest_text = max(self.options, key=len)
center_x_cord = get_center_x_coordinate(longest_text)
center_y_cord = get_center_y_coordinate(self.options)
for index, text in enumerate(self.options):
pyxel.text(center_x_cord,
center_y_cord + index * consts.MEDIUM_MARGIN, text,
consts.WHITE)
pyxel.circ(center_x_cord - 6,
((center_y_cord + consts.BALL_RADIUS) +
(consts.MEDIUM_MARGIN * self.selected_option)),
consts.BALL_RADIUS, consts.ORANGE)

def update_selected_option(self):
''' Updates currently selected options item and returns it. '''
if pyxel.btnp(pyxel.KEY_UP) and self.selected_option > 0:
self.selected_option -= 1
if pyxel.btnp(pyxel.KEY_DOWN) and self.selected_option < (
len(self.options) - 1):
self.selected_option += 1
return self.selected_option


class Controls(Menus):
def display(self):
''' Displays controls info. '''
longest_text = max(consts.CONTROLS, key=len)
center_x_cord = get_center_x_coordinate(longest_text)
for index, text in enumerate(consts.CONTROLS):
pyxel.text(center_x_cord,
consts.BIG_MARGIN + index * consts.MEDIUM_MARGIN, text,
consts.WHITE)


class Win(Menus):
def display(self, winner):
''' Displays the winner. '''
text = f'The winner is: {winner}'
center_x_cord = get_center_x_coordinate(text)
pyxel.text(center_x_cord, consts.BIG_MARGIN, text, consts.WHITE)
Loading

0 comments on commit be35b05

Please sign in to comment.