Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,3 @@ dmypy.json

# Pyre type checker
.pyre/
.vscode
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"python.analysis.extraPaths": [
"./src/gravity",
"./src/platformer",
"./src/platformer/level_creator",
"./src/tictactoe"
],
"python.linting.pylintArgs": [
"--disable=all",
"--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode",
"--extension-pkg-whitelist=pygame"
],
"python.formatting.provider": "black"
}
Binary file added Flappy_Bird/04B_19.TTF
Binary file not shown.
Binary file added Flappy_Bird/assets/background-day.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/background-night.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/base.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/bluebird-downflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/bluebird-midflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/bluebird-upflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/gameover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/pipe-green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/pipe-red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/redbird-downflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/redbird-midflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/redbird-upflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/yellowbird-downflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/yellowbird-midflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Flappy_Bird/assets/yellowbird-upflap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
176 changes: 176 additions & 0 deletions Flappy_Bird/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import pygame
import sys
import random


def draw_floor():
screen.blit(floor_surface, (floor_x_pos, 900))
screen.blit(floor_surface, (floor_x_pos + 576, 900))


def create_pipe():
random_pipe_pos = random.choice(pipe_height)

bottom_pipe = pipe_surface.get_rect(midtop=(700, random_pipe_pos))
top_pipe = pipe_surface.get_rect(midbottom=(700, random_pipe_pos-300))
return bottom_pipe, top_pipe


def move_pipes(pipes):
for pipe in pipes:
pipe.centerx -= 5
return pipes


def draw_pipes(pipes):
for pipe in pipes:
if pipe.bottom >= 1024:
screen.blit(pipe_surface, pipe)
else:
flip_pipe = pygame.transform.flip(pipe_surface, False, True)
screen.blit(flip_pipe, pipe)


def check_collision(pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return False
if bird_rect.top <= -100 or bird_rect.bottom >= 900:
return False
return True


def rotate_bird(bird):
new_bird = pygame.transform.rotozoom(bird, -bird_movement*3, 1)
return new_bird


def bird_animation():
new_bird = bird_frames[bird_index]
new_bird_rect = new_bird.get_rect(center=(100, bird_rect.centery))
return new_bird, new_bird_rect


def score_display(game_state):
if game_state == 'main_game':

score_surface = game_font.render(
f'Score:{int(score)}', True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(288, 100))
screen.blit(score_surface, score_rect)
if game_state == 'game_over':
score_surface = game_font.render(
f'Score:{int(score)}', True, (255, 255, 255))
score_rect = score_surface.get_rect(center=(288, 100))
screen.blit(score_surface, score_rect)

high_score_surface = game_font.render(
f'High Score:{int(high_score)}', True, (255, 255, 255))
high_score_rect = high_score_surface.get_rect(center=(288, 850))
screen.blit(high_score_surface, high_score_rect)


def update_score(score, high_score):
if score > high_score:
high_score = score
return high_score


pygame.init()
screen = pygame.display.set_mode((576, 1024))
clock = pygame.time.Clock()
game_font = pygame.font.Font('04B_19.ttf', 40)

# game variables
gravity = 0.25
bird_movement = 0
game_active = True
score = 0
high_score = 0

bg_surface = pygame.image.load('assets/background-day.png').convert()
bg_surface = pygame.transform.scale2x(bg_surface)

floor_surface = pygame.image.load('assets/base.png').convert()
floor_surface = pygame.transform.scale2x(floor_surface)
floor_x_pos = 0

bird_downflap = pygame.transform.scale2x(pygame.image.load(
'assets/bluebird-downflap.png')).convert_alpha()
bird_midflap = pygame.transform.scale2x(pygame.image.load(
'assets/bluebird-midflap.png')).convert_alpha()
bird_upflap = pygame.transform.scale2x(pygame.image.load(
'assets/bluebird-upflap.png')).convert_alpha()
bird_frames = [bird_downflap, bird_midflap, bird_upflap]
bird_index = 0
bird_surface = bird_frames[bird_index]
bird_rect = bird_surface.get_rect(center=(100, 512))


BIRDFLAP = pygame.USEREVENT + 1
pygame.time.set_timer(BIRDFLAP, 200)



pipe_surface = pygame.image.load('assets/pipe-green.png')
pipe_surface = pygame.transform.scale2x(pipe_surface)
pipe_list = []
pipe_height = [400, 600, 800]
SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)

game_over_surface = pygame.transform.scale2x(
pygame.image.load('assets/message.png').convert_alpha())
game_over_rect = game_over_surface.get_rect(center=(288, 512))

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird_movement = 0
bird_movement -= 12
if event.key == pygame.K_SPACE and game_active == False:
game_active = True
pipe_list.clear()
bird_rect.center = (100, 512)
bird_movement = 0
score = 0
if event.type == SPAWNPIPE:
pipe_list.extend(create_pipe())
if event.type == BIRDFLAP:
if bird_index < 2:
bird_index += 1
else:
bird_index = 0
bird_surface, bird_rect = bird_animation()
screen.blit(bg_surface, (0, 0))

if game_active:

bird_movement += gravity
rotated_bird = rotate_bird(bird_surface)
bird_rect.centery += bird_movement
screen.blit(rotated_bird, bird_rect)

game_active = check_collision(pipe_list)

pipe_list = move_pipes(pipe_list)
draw_pipes(pipe_list)
score += 0.01
score_display('main_game')
else:
screen.blit(game_over_surface, game_over_rect)
high_score = update_score(score, high_score)
score_display('game_over')

floor_x_pos -= 1
draw_floor()

if floor_x_pos <= -576:
floor_x_pos = 0

pygame.display.update()
clock.tick(120)
8 changes: 8 additions & 0 deletions Flappy_Bird/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Created at : 3rd August 2020

Flappy bird made using pygame .

![image](https://user-images.githubusercontent.com/53135035/193448320-63c9befb-845a-48bd-a7c2-dfcdace2623d.png)


run game.py to start the game!
6 changes: 6 additions & 0 deletions src/gravity/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WIDTH, HEIGHT = (1248, 702)
FPS = 60
BLACK = "#000000"
GREY = "#808080"
WHITE = "#FFFFFF"
G = 100000 # Gravitational constant
54 changes: 54 additions & 0 deletions src/gravity/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python
"""
Github repo can be found here:
https://github.com/sparshg/py-games
"""

import pygame as pg
import sys
from universe import Universe
from constants import *


# The main controller
class Main:
def __init__(self):
pg.init()
pg.display.set_caption("gravity")
self.win = pg.display.set_mode((WIDTH, HEIGHT))
self.clock = pg.time.Clock()
self.running = True

self.universe = Universe()

# Key press and close button functionality
def checkEvents(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False

# Update things
def update(self, dt):
self.universe.update(dt)

# Draw things
def render(self):
self.win.fill(BLACK)
self.universe.render(self.win)
pg.display.update()

# The main loop
def loop(self):
while self.running:
dt = self.clock.tick(FPS) / 1000
self.checkEvents()
self.update(dt)
self.render()
pg.quit()
sys.exit()


# Test if the script is directly ran
if __name__ == "__main__":
main = Main()
main.loop()
32 changes: 32 additions & 0 deletions src/gravity/planet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pygame as pg
from constants import *


class Planet:
def __init__(self, mass, radius, pos=(WIDTH / 2, HEIGHT / 2), vel=(0, 0)):
self.mass = mass
self.radius = radius
self.pos = pg.Vector2(pos)
self.vel = pg.Vector2(vel)
self.prevPositions = []

def calcUpdate(self, planets):
self.field = pg.Vector2(0, 0)
for planet in planets:
r = planet.pos - self.pos
mag = r.magnitude()
if mag != 0:
self.field += G * planet.mass * r * mag ** -3

def applyUpdate(self, dt):
self.prevPositions.append((self.pos.x, self.pos.y))
if len(self.prevPositions) > 100:
self.prevPositions.pop(0)

self.vel += self.field * dt
self.pos += self.vel * dt

def render(self, surf):
for i in range(len(self.prevPositions)):
pg.draw.circle(surf, GREY, self.prevPositions[len(self.prevPositions) - i - 1], self.radius - i/self.radius)
pg.draw.circle(surf, WHITE, self.pos, self.radius)
21 changes: 21 additions & 0 deletions src/gravity/universe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pygame as pg
from planet import Planet
from constants import *


class Universe:
def __init__(self):
self.planets = [
Planet(10, 10, (WIDTH / 2 - 100, HEIGHT / 2), (0, 50)),
Planet(10, 10, (WIDTH / 2 + 100, HEIGHT / 2), (0, -50)),
]

def update(self, dt):
for planet in self.planets:
planet.calcUpdate(self.planets)
for planet in self.planets:
planet.applyUpdate(dt)

def render(self, surf):
for planet in self.planets:
planet.render(surf)
13 changes: 13 additions & 0 deletions src/platformer/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Declare some constants and variables

WIDTH, HEIGHT = (600, 400)
FPS = 60
BLACK = "#000000"
DARKGRAY = "#404040"
GRAY = "#808080"
LIGHTGRAY = "#d3d3d3"
WHITE = "#FFFFFF"
ORANGE = "#FF6600"
RED = "#FF1F00"
PURPLE = "#800080"
DARKPURPLE = "#301934"
1 change: 1 addition & 0 deletions src/platformer/level.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"level":[["ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground","ground"],["ground",null,null,null,"ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground",null,null,null,null,null,null,"ground",null,null,"ground",null,"ground",null,null,"ground","ground","ground",null,null,null,null,null,"ground"],["ground",null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground",null,null,null,"ground"],["ground",null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,"ground",null,null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground"],["ground","ground",null,null,null,null,null,null,null,"ground","ground","ground","ground",null,null,"ground","ground",null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground"],["ground",null,"ground",null,null,null,"ground","ground","ground",null,null,null,null,null,null,null,null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground"],["ground",null,null,null,null,null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground"],["ground",null,null,"ground","ground",null,null,null,"ground",null,null,null,null,null,null,"ground","ground","ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground"],["ground",null,null,null,"ground",null,null,null,null,null,null,null,null,null,null,null,"ground","ground",null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"ground"],["ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground","ground"]]}
13 changes: 13 additions & 0 deletions src/platformer/level_creator/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Declare some constants and variables

WIDTH, HEIGHT = (800, 600)
FPS = 60
BLACK = "#000000"
DARKGRAY = "#404040"
GRAY = "#808080"
LIGHTGRAY = "#d3d3d3"
WHITE = "#FFFFFF"
ORANGE = "#FF6600"
RED = "#FF1F00"
PURPLE = "#800080"
DARKPURPLE = "#301934"
27 changes: 27 additions & 0 deletions src/platformer/level_creator/file_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# JSON level reader

import json, os

# Read level
def read_level():
# Open file from full path (just in case the script is being ran from somewhere other than py-games/src/platformer/level_creator)
file = open(os.path.join(os.path.dirname(__file__), os.pardir, "level.json"), "r")
# Python dict
try:
data = json.load(file)
except Exception:
print("Couldn't read level, data could be corrupted.")
# Close file
file.close()
# Return Python list
return data["level"]


# Write level
def write_level(data):
# Same as above...
file = open(os.path.join(os.path.dirname(__file__), os.pardir, "level.json"), "w")
# Write over file
file.write('{"level":' + json.dumps(data, separators=(",", ":")) + "}\n")
# Close file
file.close()
Loading