{"id":25005494,"url":"https://github.com/proxlight/pyglet","last_synced_at":"2025-03-29T23:15:47.502Z","repository":{"id":248386918,"uuid":"828552815","full_name":"Proxlight/Pyglet","owner":"Proxlight","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-14T13:52:34.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-05T00:41:22.321Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Proxlight.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-14T13:51:32.000Z","updated_at":"2024-07-14T13:52:37.000Z","dependencies_parsed_at":"2024-07-14T15:54:26.189Z","dependency_job_id":null,"html_url":"https://github.com/Proxlight/Pyglet","commit_stats":null,"previous_names":["proxlight/pyglet"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Proxlight%2FPyglet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Proxlight%2FPyglet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Proxlight%2FPyglet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Proxlight%2FPyglet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Proxlight","download_url":"https://codeload.github.com/Proxlight/Pyglet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246254147,"owners_count":20747949,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-05T00:40:23.990Z","updated_at":"2025-03-29T23:15:47.485Z","avatar_url":"https://github.com/Proxlight.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# How to Create an Endless Runner Game Using Pyglet\n\nHey everyone, welcome back to another exciting tutorial! Today, we're diving into the world of game development with Python and Pyglet. We'll walk you through step-by-step on how to build your very own endless runner game. By the end of this video, you'll have a game where your player can jump over obstacles and try to survive as long as possible. Let's get started!\n\n## Introduction\n\nSo, what exactly is an endless runner game? Well, think of classics like Temple Run or Subway Surfers, where the goal is to keep running, dodging obstacles, and scoring points. We're going to create something similar using Pyglet, which is an awesome library for making games and interactive applications in Python.\n\n## Prerequisites\n\nBefore we jump in, here's what you'll need:\n- Basic knowledge of Python programming.\n- Pyglet installed on your system. If you haven't already, you can easily install it with a simple pip command:\n  \n  ```bash\n  pip install pyglet\n  ```\n\n## Setting Up Our Project\n\nFirst things first, let's set up our project file (`endless_runner_game.py`). This is where all the magic happens!\n\n## Creating the Game Elements\n\n### 1. Player and Ground\n\nThe player and ground are essential parts of our game. The player will run on the ground and jump over obstacles. Here's how we set them up using Pyglet's shape drawing:\n\n```python\nimport pyglet\nfrom pyglet.window import key\nfrom pyglet import shapes\nimport random\n\n# Create a window\nwindow = pyglet.window.Window(width=800, height=600, caption=\"Endless Runner Game\")\n\n# Define gravity, jump speed, and player speed\ngravity = -900\njump_speed = 500\nplayer_speed = 300\n\n# Create a batch for better performance\nbatch = pyglet.graphics.Batch()\n\n# Create player using shapes\nplayer = shapes.Rectangle(50, 100, 50, 50, color=(50, 225, 30), batch=batch)\n\n# Create ground using shapes\nground = shapes.Rectangle(0, 50, 800, 20, color=(0, 0, 255), batch=batch)\n```\n\n### 2. Handling Player Movement\n\nNow, let's handle the player's movement. We'll track keyboard inputs to move the player left and right, as well as handle jumping:\n\n```python\n# Variables to track player movement and state\nkeys = key.KeyStateHandler()\nwindow.push_handlers(keys)\nplayer_velocity_y = 0\nis_jumping = False\n\n@window.event\ndef on_key_press(symbol, modifiers):\n    global player_velocity_y, is_jumping\n\n    # Handle jump\n    if symbol == key.SPACE and not is_jumping:\n        player_velocity_y = jump_speed\n        is_jumping = True\n```\n\n## Obstacle Management\n\nEvery good endless runner needs obstacles to dodge! Let's create and manage our obstacles:\n\n```python\n# List to hold obstacles\nobstacles = []\n\n# Function to create obstacles\ndef create_obstacle():\n    x = window.width\n    y = ground.y + ground.height\n    width = 20\n    height = random.randint(20, 50)\n    obstacle = shapes.Rectangle(x, y, width, height, color=(255, 0, 0), batch=batch)\n    obstacles.append(obstacle)\n\n# Function to update obstacles\ndef update_obstacles(dt):\n    for obstacle in obstacles:\n        obstacle.x -= player_speed * dt\n    # Remove obstacles that are off-screen\n    obstacles[:] = [obstacle for obstacle in obstacles if obstacle.x + obstacle.width \u003e 0]\n```\n\n## Game Mechanics\n\nNow, let's put everything together in our game loop and handle game mechanics like gravity, collisions, and drawing:\n\n```python\n# Update function to handle movement and gravity\ndef update(dt):\n    global player_velocity_y, is_jumping\n\n    # Apply gravity\n    player_velocity_y += gravity * dt\n\n    # Move player vertically\n    player.y += player_velocity_y * dt\n\n    # Check for collisions with the ground\n    if player.y \u003c= ground.y + ground.height:\n        player.y = ground.y + ground.height\n        player_velocity_y = 0\n        is_jumping = False\n\n    # Check for collisions with obstacles\n    for obstacle in obstacles:\n        if (player.x + player.width \u003e obstacle.x and player.x \u003c obstacle.x + obstacle.width and\n                player.y \u003c obstacle.y + obstacle.height):\n            print(\"Game Over!\")\n            pyglet.app.exit()\n\n    # Update obstacles\n    update_obstacles(dt)\n\n@window.event\ndef on_draw():\n    window.clear()\n    batch.draw()\n\n# Schedule the update function\npyglet.clock.schedule_interval(update, 1/60.0)\n# Schedule obstacle creation\npyglet.clock.schedule_interval(lambda dt: create_obstacle(), 1.5)\n\n# Start the game\npyglet.app.run()\n```\n\n## Conclusion\n\nAnd there you have it! We've just created a basic endless runner game using Pyglet. You've learned how to set up a game window, create game elements like the player and obstacles, handle player movement and collisions, and more. Feel free to expand on this game by adding features like scoring, power-ups, or even different levels.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproxlight%2Fpyglet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fproxlight%2Fpyglet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fproxlight%2Fpyglet/lists"}