{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# PyGame\n", "http://openbookproject.net/thinkcs/python/english3e/pygame.html\n", "\n", "- third-party package that is used in Game development using Python\n", "- must install using pip - https://www.pygame.org/download.shtml\n", "- pip install pygame\n", "- see: http://openbookproject.net/thinkcs/python/english3e/pygame.html\n", "- see pygame folder within the repository to run the provided source codes" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pygame in /Users/rbasnet/miniconda3/lib/python3.7/site-packages (1.9.4)\n" ] } ], "source": [ "%%bash\n", "# install pygame\n", "pip install pygame" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The game loop\n", "\n", "\n", "\n", "In every game, in the setup section we’ll create a window, load and prepare some content, and then enter the game loop. The game loop continuously does four main things:\n", "\n", "- it polls for events — i.e. asks the system whether events have occurred — and responds appropriately,\n", "- it updates whatever internal data structures or objects need changing,\n", "- it draws the current state of the game into a (non-visible) surface,\n", "- it puts the just-drawn surface on display." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pygame 1.9.4\n", "Hello from the pygame community. https://www.pygame.org/contribute.html\n" ] } ], "source": [ "import pygame\n", "\n", "def main():\n", " \"\"\" Set up the game and run the main game loop \"\"\"\n", " pygame.init() # Prepare the pygame module for use\n", " surface_sz = 480 # Desired physical surface size, in pixels.\n", "\n", " # Create surface of (width, height), and its window.\n", " main_surface = pygame.display.set_mode((surface_sz, surface_sz))\n", "\n", " # Set up some data to describe a small rectangle and its color\n", " small_rect = (300, 200, 150, 90)\n", " some_color = (255, 0, 0) # A color is a mix of (Red, Green, Blue)\n", "\n", " while True:\n", " ev = pygame.event.poll() # Look for any event\n", " if ev.type == pygame.QUIT: # Window close button clicked?\n", " break # ... leave game loop\n", "\n", " # Update your game objects and data structures here...\n", "\n", " # We draw everything from scratch on each frame.\n", " # So first fill everything with the background color\n", " main_surface.fill((0, 200, 255))\n", "\n", " # Overpaint a smaller rectangle on the main surface\n", " main_surface.fill(some_color, small_rect)\n", "\n", " # Now the surface is ready, tell pygame to display it!\n", " pygame.display.flip()\n", "\n", " pygame.quit() # Once we leave the loop, close the window.\n", "\n", "main()" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pygame 1.9.4\n", "Hello from the pygame community. https://www.pygame.org/contribute.html\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/rbasnet/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:18: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead\n", "/Users/rbasnet/miniconda3/lib/python3.7/site-packages/ipykernel_launcher.py:30: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead\n" ] } ], "source": [ "import pygame\n", "import time\n", "\n", "def main():\n", "\n", " pygame.init() # Prepare the PyGame module for use\n", " main_surface = pygame.display.set_mode((480, 240))\n", "\n", " # Load an image to draw. Substitute your own.\n", " # PyGame handles gif, jpg, png, etc. image types.\n", " ball = pygame.image.load(\"pygame/ball.png\")\n", " ball = pygame.transform.scale(ball, [20, 20])\n", " # Create a font for rendering text\n", " my_font = pygame.font.SysFont(\"Courier\", 16)\n", "\n", " frame_count = 0\n", " frame_rate = 0\n", " t0 = time.clock()\n", "\n", " while True:\n", "\n", " # Look for an event from keyboard, mouse, joystick, etc.\n", " ev = pygame.event.poll()\n", " if ev.type == pygame.QUIT: # Window close button clicked?\n", " break # Leave game loop\n", "\n", " # Do other bits of logic for the game here\n", " frame_count += 1\n", " if frame_count % 500 == 0:\n", " t1 = time.clock()\n", " frame_rate = 500 / (t1-t0)\n", " t0 = t1\n", "\n", " # Completely redraw the surface, starting with background\n", " main_surface.fill((0, 200, 255))\n", "\n", " # Put a red rectangle somewhere on the surface\n", " main_surface.fill((255,0,0), (300, 100, 150, 90))\n", "\n", " # Copy our image to the surface, at this (x,y) posn\n", " main_surface.blit(ball, (100, 120))\n", "\n", " # Make a new surface with an image of the text\n", " the_text = my_font.render(\"Frame = {0}, rate = {1:.2f} fps\"\n", " .format(frame_count, frame_rate), True, (0,0,0))\n", " # Copy the text surface to the main surface\n", " main_surface.blit(the_text, (10, 10))\n", "\n", " # Now that everything is drawn, put it on display!\n", " pygame.display.flip()\n", "\n", " pygame.quit()\n", "\n", "\n", "main()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }