Skip to content

Commit

Permalink
Initial commit: Project setup, main game loop, CI
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Nov 26, 2023
0 parents commit 5cc4615
Show file tree
Hide file tree
Showing 16 changed files with 526 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Set update schedule for GitHub Actions

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
89 changes: 89 additions & 0 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: CI

on:
push:
branches: [ main ]
paths-ignore:
- '**/*.md'
- '**/*.rst'
pull_request:
branches: [ main ]
paths-ignore:
- '**/*.md'
- '**/*.rst'
workflow_dispatch:

jobs:
tests:
name: pytest on ${{ matrix.python-version }}
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Upgrade pip
run: python -m pip install --upgrade pip
- name: Install requirements (Python 3)
run: pip install -r requirements/ci.txt -r requirements/dev.txt
- name: Install jump_and_run_game
run: pip install .

- name: Run tests
run: pytest -vv

codestyle:
name: Check code style issues
runs-on: ubuntu-20.04
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
submodules: 'recursive'

- name: Upgrade pip
run: python -m pip install --upgrade pip
- name: Install requirements
run: pip install -r requirements/ci.txt
- name: Install jump_and_run_game
run: pip install .

- name: Lint with black
run: black --check .
- name: Lint with mypy
run: mypy . --ignore-missing-imports --exclude build
- name: Test with ruff
run: |
echo `ruff --version`
ruff jump_and_run_game/
package:
name: Build & verify package
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: ${{env.PYTHON_LATEST}}

- run: python -m pip install flit check-wheel-contents
- run: flit build
- run: ls -l dist
- run: check-wheel-contents dist/*.whl

- name: Test installing package
run: python -m pip install .

- name: Test running installed package
working-directory: /tmp
run: python -c "import jump_and_run_game;print(jump_and_run_game.__version__)"
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Python cache files
*.pyc
__pycache__/

# pyenv: Used to define which version of Python is used locally
.python-version

# direnv
.envrc

# temporary files created by flit
dist
52 changes: 52 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# pre-commit run --all-files
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-ast
- id: check-byte-order-marker
- id: check-case-conflict
- id: check-docstring-first
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
exclude: "resources/.*|docs/make.bat"
- id: trailing-whitespace
- id: mixed-line-ending
args: ['--fix=lf']
exclude: "docs/make.bat"
- id: check-added-large-files
args: ['--maxkb=1000']
- repo: https://github.com/psf/black
rev: 23.11.0
hooks:
- id: black
args: [--target-version, py36]
- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs
additional_dependencies: [black==22.1.0]
exclude: "docs/user/robustness.md"
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.1.6
hooks:
- id: ruff
args: ['--fix']
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
args: ["--ignore", "E,W,F"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.7.1'
hooks:
- id: mypy
additional_dependencies: [types-Pillow==10.0.0.2]
files: ^jump_and_run_game/.*
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) 2023 Martin Thoma

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.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
maint:
pre-commit autoupdate
pip-compile -U requirements/ci.in
pip-compile -U requirements/dev.in

upload:
flit publish

install:
pip install -e .

start:
jump_and_run_game
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Your Jump-and-Run Game

Welcome to `jump_and_run_game` – a fun and exciting jump-and-run game developed
in Python!

## Overview

It's really just a simple Jump- and Run game: 2D, jumping ... and walking.
Let's be honest, it's not running so far.

## Table of Contents

- [Installation](#installation)
- [Usage](#usage)
- [Controls](#controls)
- [Contributing](#contributing)

## Installation

1. Ensure you have Python 3.8 or later installed.

2. Install dependencies:

```bash
pip install jump_and_run_game
```

## Usage

To play the game, run the following command:

```bash
jump_and_run_game
```

This is how it looks like at the moment:

![](docs/main-action.png)

## Controls

* Left Arrow: Move the player left.
* Right Arrow: Move the player right.
* Space: Make the player jump.

## Contributing

If you'd like to contribute to the project, feel free to open issues or add PRs.
It's typically wise to open an issue first and wait for feedback, before adding
tons of code that might get rejected.
Binary file added docs/main-action.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 106 additions & 0 deletions jump_and_run_game/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from ._version import __version__
from os import environ
import sys

environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "1"

__all__ = [
"__version__",
]

import pygame # noqa: E402


# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


class Player:
JUMP_HEIGHT = 10

def __init__(self, x, y):
self.x = x
self.y = y
self.speed = 5
self.jump_count = Player.JUMP_HEIGHT
self.is_jumping = False
self.width = 50
self.height = 50

def move_left(self) -> None:
self.x -= self.speed

def move_right(self) -> None:
self.x += self.speed

def jump(self) -> None:
if not self.is_jumping:
self.is_jumping = True

def update(self) -> None:
if self.is_jumping:
if self.jump_count >= -Player.JUMP_HEIGHT:
neg = 1
if self.jump_count < 0:
neg = -1
self.y -= (self.jump_count**2) * 0.5 * neg
self.jump_count -= 1
else:
self.is_jumping = False
self.jump_count = Player.JUMP_HEIGHT

def render(self, screen: pygame.surface.Surface) -> None:
pygame.draw.rect(screen, BLACK, (self.x, self.y, self.width, self.height))


def core_game_loop(
screen: pygame.surface.Surface, clock: pygame.time.Clock, player: Player
) -> None:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

# Handle user input
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.x > 0:
player.move_left()
if keys[pygame.K_RIGHT] and player.x < WIDTH - 50:
player.move_right()
if keys[pygame.K_SPACE]:
player.jump()

# Update game state
player.update()

# Render graphics
screen.fill(WHITE)
player.render(screen)

pygame.display.flip()
clock.tick(FPS)


def main():
pygame.init()

# Setup the window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Jump and Run Game")

# Create core objects
clock = pygame.time.Clock()
player = Player(50, HEIGHT - 100)

while True:
core_game_loop(screen, clock, player)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions jump_and_run_game/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
Loading

0 comments on commit 5cc4615

Please sign in to comment.