{ "cells": [ { "cell_type": "markdown", "id": "d47fae7a-e60a-44e1-a2e9-1eb387915889", "metadata": {}, "source": [ "# PyGame\n", "\n", "Steer display of images on screen using keyboard. For example, to get ace of heart press `a` and `h` keys at same time. To get 5 of spades press `5` and `s`, to get 10 of clubs press `0` and `c` etc. " ] }, { "cell_type": "code", "execution_count": 2, "id": "f92199c3-b1f9-4cb0-8b2d-df7e13d6db9a", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pygame\n", "import glob\n", "import matplotlib.pyplot as plt\n", "#\n", "def get_card_name(keys):\n", " '''\n", " Given pygame keys (indicating which keys are depressed), return string if key combination corresponds to valid card.\n", " String forms part of name of the file that is an image of the requested card.\n", " '''\n", " one = ''\n", " two = ''\n", " if keys[pygame.K_a]:\n", " one = 'A'\n", " elif keys[pygame.K_2]:\n", " one = '2'\n", " elif keys[pygame.K_3]:\n", " one = '3'\n", " elif keys[pygame.K_4]:\n", " one = '4'\n", " elif keys[pygame.K_5]:\n", " one = '5'\n", " elif keys[pygame.K_6]:\n", " one = '6'\n", " elif keys[pygame.K_7]:\n", " one = '7'\n", " elif keys[pygame.K_8]:\n", " one = '8'\n", " elif keys[pygame.K_9]:\n", " one = '9'\n", " elif keys[pygame.K_0]:\n", " one = '10'\n", " elif keys[pygame.K_j]:\n", " one = 'J'\n", " elif keys[pygame.K_q]:\n", " one = 'Q'\n", " elif keys[pygame.K_k]:\n", " one = 'K'\n", " #\n", " if keys[pygame.K_c]:\n", " two = 'C'\n", " elif keys[pygame.K_d]:\n", " two = 'D'\n", " elif keys[pygame.K_h]:\n", " two = 'H'\n", " elif keys[pygame.K_s]:\n", " two = 'S'\n", " #\n", " if len(one) > 0 and len(two) > 0:\n", " card_name = one + two\n", " else:\n", " card_name = ''\n", " #\n", " return card_name\n", "#\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", "# Image directory\n", "card_dir = 'Cards'\n", "#\n", "running = True\n", "row_start = 100\n", "col_start = 200\n", "row_step = IMAGE_SIZE[0] + 5\n", "col_step = 0\n", "#\n", "n_card = 0\n", "while running:\n", " #\n", " # Iterate over Event objects returned by pygame.event.get() method.\n", " for evnt in pygame.event.get():\n", " #\n", " # if too many cards, the window was closed or escape was entered then stop the game\n", " if n_card > 5:\n", " running = False\n", " if evnt.type == pygame.QUIT:\n", " running = False\n", " if evnt.type == pygame.KEYDOWN:\n", " if evnt.key == pygame.K_ESCAPE:\n", " running = False\n", " #\n", " keys = pygame.key.get_pressed()\n", " #\n", " # Get the name of the card (and image file) corresponding to the depressed keys\n", " card_name = get_card_name(keys)\n", " #\n", " if len(card_name) > 0:\n", " #\n", " # Construct image file name\n", " image_file = card_dir + '/' + card_name + '.png'\n", " #\n", " # Load image\n", " image = pygame.image.load(image_file)\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_card*row_step\n", " col = col_start + n_card*col_step\n", " IMAGE_POSITION = (row, col)\n", " #\n", " # Put the image on the screen\n", " screen.blit(image, IMAGE_POSITION)\n", " #\n", " # Display the screen\n", " pygame.display.flip()\n", " #\n", " n_card += 1\n", " #\n", " # Control the speed of the event loop\n", " clock.tick(FPS) \n", " #\n", "pygame.quit()" ] } ], "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 }