{ "cells": [ { "cell_type": "markdown", "id": "f08c62f9-3103-4f54-949c-37c29ac3ecad", "metadata": {}, "source": [ "# Pygame \n", "\n", "Display sequence of persistent images on screen. " ] }, { "cell_type": "code", "execution_count": 1, "id": "6ba6a153-2d9d-4c7d-aca4-0fb7fb735d46", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pygame 2.2.0 (SDL 2.26.4, Python 3.9.16)\n", "Hello from the pygame community. https://www.pygame.org/contribute.html\n", "Linux.\n" ] } ], "source": [ "import numpy as np\n", "import pygame\n", "import glob\n", "import matplotlib.pyplot as plt\n", "import platform\n", "#\n", "# Find out what type of computer we are running on\n", "Windows = platform.system() == 'Windows'\n", "Linux = platform.system() == 'Linux'\n", "Mac = platform.system() == 'Darwin'\n", "if Windows:\n", " print(\"Windows.\")\n", "elif Linux:\n", " print(\"Linux.\")\n", "elif Mac:\n", " print(\"Macintosh.\")\n", "else:\n", " print(\"Unrecognised operating system\",platform.system())\n", " print(\"Stop\")\n", " sys.exit(0)\n", "#\n", "# Initialise pygame\n", "pygame.init()\n", "#\n", "# Set window size\n", "size = (width, height) = (800, 800)\n", "screen = pygame.display.set_mode(size)\n", "#\n", "# Start clock\n", "clock = pygame.time.Clock()\n", "FPS = 60 # Frames per second\n", "#\n", "# Set required image size\n", "IMAGE_SIZE = (69, 106)\n", "#\n", "# Set background colour\n", "screen.fill((0, 0, 0))\n", "#\n", "# Get information on images: look for files with name *.png in the Cards directory\n", "cards_read = glob.glob('Cards/*.png')\n", "#\n", "# Get the card file names \n", "card_files = [card.split(', ')[0] for card in cards_read]\n", "#\n", "# Get card names (by removing .png and then the directiry name)\n", "card_names = [card.split('.')[0] for card in card_files]\n", "#\n", "if Windows:\n", " card_names = [card.split('\\\\')[1] for card in card_names]\n", "else:\n", " card_names = [card.split('/')[1] for card in card_names]\n", "#\n", "# This is the order to display the cards\n", "show_card = ['AC', 'AD', 'AH', 'AS',\n", " '2C', '2D', '2H', '2S',\n", " '3C', '3D', '3H', '3S',\n", " '4C', '4D', '4H', '4S',\n", " '5C', '5D', '5H', '5S',\n", " '6C', '6D', '6H', '6S',\n", " '7C', '7D', '7H', '7S',\n", " '8C', '8D', '8H', '8S',\n", " '9C', '9D', '9H', '9S',\n", " '10C', '10D', '10H', '10S',\n", " 'JC', 'JD', 'JH', 'JS',\n", " 'QC', 'QD', 'QH', 'QS',\n", " 'KC', 'KD', 'KH', 'KS']\n", "#print(\"cards_read \\n\",cards_read)\n", "#print(\"card_files \\n\",card_files)\n", "#print(\"card_names \\n\",card_names)\n", "#print(\"show_card \\n\",show_card)\n", "#\n", "running = True\n", "#\n", "# Start psotition for first card\n", "row_start = 100\n", "col_start = 100\n", "#\n", "# Position steps for displaying cards\n", "row_step = 5\n", "col_step = 40\n", "#\n", "n = 0\n", "n_run = 12\n", "n_pack = len(card_names)\n", "#\n", "n_stop = 52\n", "#\n", "while running and n < n_stop:\n", " #\n", " # Iterate over event objects returned by pygame.event.get() method.\n", " for i in pygame.event.get():\n", " #\n", " # if event object is QUIT then quit pygame\n", " if i.type == pygame.QUIT:\n", " running = False\n", " #\n", " # Choose number of card to show (making sure it doesn't exceed number in pack!)\n", " n_show = n%n_pack\n", " #\n", " # Find the index (number in card_file) of the card you want to show\n", " index = card_names.index(show_card[n_show])\n", " #\n", " # Load that image\n", " image = card_files[index]\n", " image = pygame.image.load(image)\n", " # \n", " # Scale image to required size\n", " image = pygame.transform.scale(image, IMAGE_SIZE)\n", " #\n", " # Set image position (coordinate origin is top left corner) \n", " row = row_start + n*10 + n%n_run*row_step\n", " col = col_start + n%n_run*col_step\n", " IMAGE_POSITION = (row, col)\n", " #\n", " # Show the image\n", " screen.blit(image, IMAGE_POSITION)\n", " #\n", " # Part of event loop\n", " pygame.display.flip()\n", " clock.tick(FPS/60) \n", " #\n", " n += 1\n", " #\n", "pygame.quit()" ] }, { "cell_type": "markdown", "id": "0d986805-19b3-44bb-b122-5ae6c56bad5e", "metadata": {}, "source": [ "As above, but delete last image before next one shown!" ] }, { "cell_type": "code", "execution_count": 2, "id": "a91622c8-5025-4a2d-a99e-6d6d1df51a0a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Linux.\n" ] } ], "source": [ "import numpy as np\n", "import pygame\n", "import glob\n", "import matplotlib.pyplot as plt\n", "import platform\n", "#\n", "# Find out what type of computer we are running on\n", "Windows = platform.system() == 'Windows'\n", "Linux = platform.system() == 'Linux'\n", "Mac = platform.system() == 'Darwin'\n", "if Windows:\n", " print(\"Windows.\")\n", "elif Linux:\n", " print(\"Linux.\")\n", "elif Mac:\n", " print(\"Macintosh.\")\n", "else:\n", " print(\"Unrecognised operating system\",platform.system())\n", " print(\"Stop\")\n", " sys.exit(0)\n", "#\n", "# Initialise pygame\n", "pygame.init()\n", "#\n", "# Set window size\n", "size = (width, height) = (800, 800)\n", "screen = pygame.display.set_mode(size)\n", "#\n", "# Start clock\n", "clock = pygame.time.Clock()\n", "FPS = 60 # Frames per second\n", "#\n", "# Set required image size\n", "card_size = (69, 106)\n", "IMAGE_SIZE = card_size\n", "#\n", "# Set background colour\n", "background = (0, 0, 0)\n", "screen.fill(background)\n", "#\n", "# Get information on images: look for files with name *.png in the Cards directory\n", "cards_read = glob.glob('Cards/*.png')\n", "#\n", "# Get the card file names \n", "card_files = [card.split(', ')[0] for card in cards_read]\n", "#\n", "# Get card names (by removing .png and then the directiry name)\n", "card_names = [card.split('.')[0] for card in card_files]\n", "#\n", "if Windows:\n", " card_names = [card.split('\\\\')[1] for card in card_names]\n", "else:\n", " card_names = [card.split('/')[1] for card in card_names]\n", "#\n", "# This is the order to display the cards\n", "show_card = ['AC', 'AD', 'AH', 'AS',\n", " '2C', '2D', '2H', '2S',\n", " '3C', '3D', '3H', '3S',\n", " '4C', '4D', '4H', '4S',\n", " '5C', '5D', '5H', '5S',\n", " '6C', '6D', '6H', '6S',\n", " '7C', '7D', '7H', '7S',\n", " '8C', '8D', '8H', '8S',\n", " '9C', '9D', '9H', '9S',\n", " '10C', '10D', '10H', '10S',\n", " 'JC', 'JD', 'JH', 'JS',\n", " 'QC', 'QD', 'QH', 'QS',\n", " 'KC', 'KD', 'KH', 'KS']\n", "#print(\"cards_read \\n\",cards_read)\n", "#print(\"card_files \\n\",card_files)\n", "#print(\"card_names \\n\",card_names)\n", "#print(\"show_card \\n\",show_card)\n", "#\n", "running = True\n", "#\n", "# Start position for first card\n", "row_start = 100\n", "col_start = 100\n", "#\n", "# Position steps for displaying cards\n", "row_step = 5\n", "col_step = 40\n", "#\n", "n = 0\n", "n_run = 12\n", "n_pack = len(card_names)\n", "#\n", "n_stop = 52\n", "#\n", "while running and n < n_stop:\n", " #\n", " # Iterate over event objects returned by pygame.event.get() method.\n", " for i in pygame.event.get():\n", " #\n", " # if event object is QUIT then quit pygame\n", " if i.type == pygame.QUIT:\n", " running = False\n", " #\n", " # Choose number of card to show (making sure it doesn't exceed number in pack!)\n", " n_show = n%n_pack\n", " #\n", " # Find the index (number in card_file) of the card you want to show\n", " index = card_names.index(show_card[n_show])\n", " #\n", " # Load that image\n", " image = card_files[index]\n", " image = pygame.image.load(image)\n", " # \n", " # Scale image to required size\n", " image = pygame.transform.scale(image, IMAGE_SIZE)\n", " #\n", " # Delete previous card image (draw the background over it!)\n", " pygame.draw.rect(screen, background, (row, col, card_size[0], card_size[1]))\n", " #\n", " # Set image position (coordinate origin is top left corner) \n", " row = row_start + n*10 + n%n_run*row_step\n", " col = col_start + n%n_run*col_step\n", " IMAGE_POSITION = (row, col)\n", " #\n", " # Show the image\n", " screen.blit(image, IMAGE_POSITION)\n", " #\n", " # Part of event loop\n", " pygame.display.flip()\n", " clock.tick(FPS/10) \n", " #\n", " n += 1\n", " #\n", "pygame.quit()" ] }, { "cell_type": "code", "execution_count": null, "id": "60767125-f84a-4d8e-85a7-128458cb8380", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }