External Exam Download Resources Web Applications Games Recycle Bin

event KEYDOWN

cardflip.py

import pygame
from cardimg import img #import img dictionary variable from cardimg.py
cardback = pygame.image.load(img["cardback"])
ace_of_spades = pygame.image.load(img["as"]) #Ace of Spades
yellow = (255,255,0) #RGB

pygame.init()
screen = pygame.display.set_mode((400, 300))
screen.fill(yellow)

current_image = cardback

done = False
while not done:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True

    #KEYDOWN - pressed once:
    if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
      current_image = ace_of_spades
      
  screen.blit(current_image, (200, 150))    
  pygame.display.flip()
  
pygame.quit()

cardimg.py

# -----------------------------------------------------------
# loads a deck of online card images into a local dictionary 
#
# (C) 2020 digisoln.com
# Released under GNU Public License (GPL)
# -----------------------------------------------------------

img = {}
import io, requests
img["cardback"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/cardback.gif").content)
img["2c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/2c.gif").content)
img["2d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/2d.gif").content)
img["2h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/2h.gif").content)
img["2s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/2s.gif").content)
img["3c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/3c.gif").content)
img["3d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/3d.gif").content)
img["3h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/3h.gif").content)
img["3s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/3s.gif").content)
img["4c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/4c.gif").content)
img["4d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/4d.gif").content)
img["4h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/4h.gif").content)
img["4s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/4s.gif").content)
img["5c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/5c.gif").content)
img["5d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/5d.gif").content)
img["5h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/5h.gif").content)
img["5s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/5s.gif").content)
img["6c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/6c.gif").content)
img["6d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/6d.gif").content)
img["6h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/6h.gif").content)
img["6s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/6s.gif").content)
img["7c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/7c.gif").content)
img["7d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/7d.gif").content)
img["7h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/7h.gif").content)
img["7s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/7s.gif").content)
img["8c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/8c.gif").content)
img["8d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/8d.gif").content)
img["8h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/8h.gif").content)
img["8s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/8s.gif").content)
img["9c"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/9c.gif").content)
img["9d"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/9d.gif").content)
img["9h"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/9h.gif").content)
img["9s"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/9s.gif").content)
img["tc"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/tc.gif").content)
img["td"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/td.gif").content)
img["th"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/th.gif").content)
img["ts"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/ts.gif").content)
img["jc"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/jc.gif").content)
img["jd"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/jd.gif").content)
img["jh"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/jh.gif").content)
img["js"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/js.gif").content)
img["qc"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/qc.gif").content)
img["qd"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/qd.gif").content)
img["qh"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/qh.gif").content)
img["qs"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/qs.gif").content)
img["kc"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/kc.gif").content)
img["kd"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/kd.gif").content)
img["kh"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/kh.gif").content)
img["ks"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/ks.gif").content)
img["ac"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/ac.gif").content)
img["ad"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/ad.gif").content)
img["ah"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/ah.gif").content)
img["as"] = io.BytesIO(requests.get("https://digisoln.com/resources/cards/as.gif").content)

put files in same folder:


pygame key constants
K_0 K_1 K_2 K_3 K_4 K_5 K_6 K_7 K_8 K_9
K_AMPERSAND K_ASTERISK K_AT K_BACKQUOTE K_BACKSLASH K_BACKSPACE K_BREAK K_CAPSLOCK K_CARET K_CLEAR
K_COLON K_COMMA K_DELETE K_DOLLAR K_DOWN K_END K_EQUALS K_ESCAPE K_EURO K_EXCLAIM
K_F1 K_F2 K_F3 K_F4 K_F5 K_F6 K_F7 K_F8 K_F9 K_F10
K_F11 K_F12 K_F13 K_F14 K_FIRST K_GREATER K_HASH K_HELP K_HOME K_INSERT
K_KP0 K_KP1 K_KP2 K_KP3 K_KP4 K_KP5 K_KP6 K_KP7 K_KP8 K_KP9
K_KP_DIVIDE K_KP_ENTER K_KP_EQUALS K_KP_MINUS K_KP_MULTIPLY K_KP_PERIOD K_KP_PLUS K_LALT K_LAST K_LCTRL
K_LEFT K_LEFTBRACKET K_LEFTPAREN K_LESS K_LMETA K_LSHIFT K_LSUPER K_MENU K_MINUS K_MODE
K_NUMLOCK K_PAGEDOWN K_PAGEUP K_PAUSE K_PERIOD K_PLUS K_POWER K_PRINT K_QUESTION K_QUOTE
K_QUOTEDBL K_RALT K_RCTRL K_RETURN K_RIGHT K_RIGHTBRACKET K_RIGHTPAREN K_RMETA K_RSHIFT K_RSUPER
K_SCROLLOCK K_SEMICOLON K_SLASH K_SPACE K_SYSREQ K_TAB K_UNDERSCORE K_UNKNOWN K_UP K_F15
K_a K_b K_c K_d K_e K_f K_g K_h K_i K_j
K_k K_l K_m K_n K_o K_p K_q K_r K_s K_t
K_u K_v K_w K_x K_y K_z