Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cf01c8
commit 3de8e91
Showing
15 changed files
with
463 additions
and
1 deletion.
There are no files selected for viewing
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
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] | ||
pygame = "==2.0.0.dev8" | ||
|
||
[requires] | ||
python_version = "3.7" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# japong | ||
# japong | ||
|
||
Recreación en Python de Pong, el primer arcade mundialmente popular. | ||
|
||
Desarrollado en python con pygame. |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import random | ||
|
||
import pygame | ||
|
||
import pong | ||
import pong.config | ||
|
||
|
||
class Ball(pygame.sprite.Sprite): | ||
def __init__(self, color, radius): | ||
super().__init__() | ||
|
||
self.color = color | ||
self.radius = radius | ||
self.dx = 0 | ||
self.dy = 0 | ||
self.rx = 2 | ||
self.ry = 2 | ||
self.remaining = 0 | ||
|
||
self.image = pygame.Surface((self.radius * 2, self.radius * 2)) | ||
|
||
self.image.fill(pong.config.white) | ||
self.image.set_colorkey(pong.config.white) | ||
pygame.draw.ellipse(self.image, self.color, [0, 0, self.radius * self.rx, self.radius * self.ry]) | ||
|
||
self.rect = self.image.get_rect() | ||
self.restart() | ||
|
||
self.borders = None | ||
self.pads = None | ||
|
||
def restart(self): | ||
self.rect.x = 400 | ||
self.rect.y = 300 | ||
|
||
self._set_random_direction() | ||
|
||
def _set_random_direction(self): | ||
direction = random.choice([(-1, -1), (1, -1), (1, 1), (-1, 1)]) | ||
self.dx = direction[0] | ||
self.dy = direction[1] | ||
|
||
def update(self): | ||
self.rect.x += self.dx | ||
self.rect.y += self.dy | ||
|
||
border_collisions = pygame.sprite.spritecollide(self, self.borders, False) | ||
for _ in border_collisions: | ||
self.rect.y -= self.dy | ||
self._start_transformation_count_down() | ||
self._play_side_hit_sound() | ||
self.ry = 1.3 | ||
self.dy *= -1 | ||
|
||
pad_collisions = pygame.sprite.spritecollide(self, self.pads, False) | ||
for _ in pad_collisions: | ||
self.rect.x -= self.dx | ||
self._start_transformation_count_down() | ||
self._play_pad_hit_sound() | ||
self.rx = 1.3 | ||
self.dx *= -1 | ||
|
||
if self.remaining > 0: | ||
self.remaining -= 1 | ||
else: | ||
self.rx = 2 | ||
self.ry = 2 | ||
|
||
self.image.fill(pong.config.white) | ||
self.image.set_colorkey(pong.config.white) | ||
|
||
width = self.radius * self.rx | ||
height = self.radius * self.ry | ||
|
||
y = self.radius - (height / 2) | ||
x = self.radius - (width / 2) | ||
pygame.draw.ellipse(self.image, self.color, [x, y, width, height]) | ||
|
||
@staticmethod | ||
def _play_pad_hit_sound(): | ||
from pong.main import playerHit | ||
playerHit.play() | ||
|
||
@staticmethod | ||
def _play_side_hit_sound(): | ||
from pong.main import sideHit | ||
sideHit.play() | ||
|
||
def _start_transformation_count_down(self): | ||
from pong.main import FPS | ||
self.remaining = FPS / 16 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pygame | ||
|
||
import pong.config | ||
|
||
|
||
class Border(pygame.sprite.Sprite): | ||
def __init__(self, y): | ||
super().__init__() | ||
|
||
self.image = pygame.Surface((800, 10)) | ||
self.image.fill(pong.config.white) | ||
|
||
self.rect = self.image.get_rect() | ||
self.rect.y = y | ||
self.rect.x = 0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
black = (0, 0, 0) | ||
white = (255, 255, 255) | ||
green = (36, 102, 38) | ||
red = (255, 0, 0) | ||
yellow = (247, 214, 25) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pygame | ||
|
||
import pong.player | ||
import pong.config | ||
|
||
GOAL_HIGHLIGHT_IN_SECONDS = 1.5 | ||
|
||
|
||
class Goal(pygame.sprite.Sprite): | ||
player: pong.player.Player | ||
|
||
def __init__(self, x, player): | ||
super().__init__() | ||
|
||
self.image = pygame.Surface((10, 580)) | ||
self.image.fill(pong.config.white) | ||
|
||
self.rect = self.image.get_rect() | ||
self.rect.y = 10 | ||
self.rect.x = x | ||
|
||
self.remaining = 0 | ||
self.player = player | ||
|
||
def hit(self): | ||
from pong.main import sideHit | ||
sideHit.play() | ||
self.image.fill(pong.config.red) | ||
from pong.main import FPS | ||
self.remaining = FPS * GOAL_HIGHLIGHT_IN_SECONDS | ||
|
||
def update(self): | ||
if self.remaining > 0: | ||
self.remaining -= 1 | ||
return | ||
self.image.fill(pong.config.white) | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import os | ||
|
||
import pygame | ||
|
||
import pong.ball | ||
import pong.border | ||
import pong.config | ||
import pong.goal | ||
import pong.pad | ||
import pong.player | ||
import pong.scoreboard | ||
|
||
# Init game engine | ||
FPS = 180 | ||
|
||
pygame.init() | ||
|
||
# Prepare the screen | ||
size = (800, 600) | ||
screen = pygame.display.set_mode(size) | ||
|
||
pygame.display.set_caption("Ja pong!") | ||
|
||
# Prepare sound effects | ||
pygame.mixer.init() | ||
playerHit = pygame.mixer.Sound(os.getcwd() + '/sounds/player.wav') | ||
sideHit = pygame.mixer.Sound(os.getcwd() + '/sounds/side.wav') | ||
point = pygame.mixer.Sound(os.getcwd() + '/sounds/ohno.wav') | ||
|
||
# game loop control | ||
done = False | ||
|
||
# screen updates | ||
clock = pygame.time.Clock() | ||
|
||
ball = pong.ball.Ball(pong.config.yellow, 10) | ||
|
||
pad_left = pong.pad.Pad('left') | ||
pad_right = pong.pad.Pad('right') | ||
|
||
pads = pygame.sprite.Group() | ||
pads.add(pad_left) | ||
pads.add(pad_right) | ||
|
||
border_top = pong.border.Border(0) | ||
border_bottom = pong.border.Border(590) | ||
|
||
player1 = pong.player.Player('left') | ||
player2 = pong.player.Player('computer') | ||
score_board = pong.scoreboard.ScoreBoard(player1, player2) | ||
goal_left = pong.goal.Goal(0, player2) | ||
goal_right = pong.goal.Goal(790, player1) | ||
|
||
# Prepare sprites | ||
all_sprites = pygame.sprite.Group() | ||
|
||
all_sprites.add(ball) | ||
|
||
all_sprites.add(border_top) | ||
all_sprites.add(border_bottom) | ||
all_sprites.add(goal_left) | ||
all_sprites.add(goal_right) | ||
|
||
all_sprites.add(pad_left) | ||
all_sprites.add(pad_right) | ||
|
||
borders = pygame.sprite.Group() | ||
borders.add(border_top) | ||
borders.add(border_bottom) | ||
|
||
ball.borders = borders | ||
pad_left.borders = borders | ||
pad_right.borders = borders | ||
ball.pads = pads | ||
|
||
goals = pygame.sprite.Group() | ||
goals.add(goal_left) | ||
goals.add(goal_right) | ||
|
||
scoreFont = pygame.font.Font(pygame.font.get_default_font(), 64) | ||
|
||
# Game loop | ||
while not done: | ||
# Event | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
done = True | ||
|
||
# Game logic | ||
pygame.event.pump() | ||
key = pygame.key.get_pressed() | ||
if key[pygame.K_w]: | ||
pad_left.up() | ||
elif key[pygame.K_s]: | ||
pad_left.down() | ||
else: | ||
pad_left.stop() | ||
|
||
pad_right.follow(ball) | ||
|
||
all_sprites.update() | ||
|
||
# Manage collisions | ||
goal_collisions = pygame.sprite.spritecollide(ball, goals, False) | ||
for goal in goal_collisions: | ||
goal.hit() | ||
goal.player.point() | ||
ball.restart() | ||
|
||
# Game draw | ||
screen.fill(pong.config.green) | ||
score_board.draw(screen) | ||
all_sprites.draw(screen) | ||
|
||
# Screen update | ||
pygame.display.flip() | ||
|
||
if score_board.stop(): | ||
done = True | ||
|
||
clock.tick(FPS) | ||
|
||
text = scoreFont.render('Game finished', True, pong.config.yellow, pong.config.green) | ||
score_board.winner(screen) | ||
text_rect = text.get_rect() | ||
text_rect.center = (800 // 2, 600 // 2) | ||
screen.blit(text, text_rect) | ||
pygame.display.flip() | ||
|
||
done = False | ||
while not done: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.KEYDOWN: | ||
done = True | ||
|
||
pygame.quit() | ||
quit() |
Oops, something went wrong.