Das ist der Jeopardy-Clone, den ich in Anlehnung an das 36C3-Event geschrieben habe. Wer immer Lust hat, darf und soll Fragen beisteuern
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

225 lines
7.5 KiB

'''
Textinput
and
Textoutput
using pygame
'''
import pygame
from pygame.locals import *
class Keyinput():
def __init__(self):
self.text = ""
self.uppercase = False
self.line = False
def set_letter(self,letter):
# Convert to uppercase if uppercase is true
if self.uppercase:
self.text += letter.upper()
else:
self.text += letter
def event_handler(self,event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
self.uppercase = True
# Check for all the letter
elif event.key == pygame.K_a:
self.set_letter("a")
elif event.key == pygame.K_b:
self.set_letter("b")
elif event.key == pygame.K_c:
self.set_letter("c")
elif event.key == pygame.K_d:
self.set_letter("d")
elif event.key == pygame.K_e:
self.set_letter("e")
elif event.key == pygame.K_f:
self.set_letter("f")
elif event.key == pygame.K_g:
self.set_letter("g")
elif event.key == pygame.K_h:
self.set_letter("h")
elif event.key == pygame.K_i:
self.set_letter("i")
elif event.key == pygame.K_j:
self.set_letter("j")
elif event.key == pygame.K_k:
self.set_letter("k")
elif event.key == pygame.K_l:
self.set_letter("l")
elif event.key == pygame.K_m:
self.set_letter("m")
elif event.key == pygame.K_n:
self.set_letter("n")
elif event.key == pygame.K_o:
self.set_letter("o")
elif event.key == pygame.K_p:
self.set_letter("p")
elif event.key == pygame.K_q:
self.set_letter("q")
elif event.key == pygame.K_r:
self.set_letter("r")
elif event.key == pygame.K_s:
self.set_letter("s")
elif event.key == pygame.K_t:
self.set_letter("t")
elif event.key == pygame.K_u:
self.set_letter("u")
elif event.key == pygame.K_v:
self.set_letter("v")
elif event.key == pygame.K_w:
self.set_letter("w")
elif event.key == pygame.K_x:
self.set_letter("x")
elif event.key == pygame.K_y:
self.set_letter("y")
elif event.key == pygame.K_z:
self.set_letter("z")
# Check for space
elif event.key == pygame.K_SPACE:
self.text += " "
# Check for period
elif event.key == pygame.K_PERIOD:
self.text += "."
# Check for comma
elif event.key == pygame.K_COMMA:
self.text += ","
# Check for numbers
elif event.key == pygame.K_0:
self.text += "0"
elif event.key == pygame.K_1:
self.text += "1"
elif event.key == pygame.K_2:
self.text += "2"
elif event.key == pygame.K_3:
self.text += "3"
elif event.key == pygame.K_4:
self.text += "4"
elif event.key == pygame.K_5:
self.text += "5"
elif event.key == pygame.K_6:
self.text += "6"
elif event.key == pygame.K_7:
self.text += "7"
elif event.key == pygame.K_8:
self.text += "8"
elif event.key == pygame.K_9:
self.text += "9"
# check for backspace to delete the last character
elif event.key == pygame.K_BACKSPACE:
if len(self.text) > 1:
self.text = self.text[:len(self.text)-1]
else:
self.text = ""
if event.key == pygame.K_RETURN:
self.line = True
self.linetext=self.text
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
self.uppercase = False
def draw_text_in (text,area, color_rect=(0,0,255), color_text=(255,255,255),font_name="arial", font_size=25 ):
font = pygame.font.SysFont(font_name, font_size)
y_pos = x_pos = 2
area_out=pygame.Surface(area.get_size())
area_out.fill(color_rect)
#
for line in text.splitlines():
#
out_text = font.render(line, 1, color_text)
area_out.blit(out_text, (x_pos, y_pos))
y_pos += font_size
x_pos = 0
return area_out
def text_input (answer_surf,answer_rect, color_rect=(0,0,255), color_text=(255,255,255),font_name="arial", font_size=25):
answer = Keyinput()
area_for_answer = pygame.Surface((answer_rect.w,answer_rect.h))
while answer.line == False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == 27:
pygame.quit()
exit()
# Run keyboard event handler
answer.event_handler(event)
#print (answer.text)
area_ausgabe= draw_text_in ( answer.text,area_for_answer,color_rect , color_text,font_name,font_size)
answer_surf.blit(area_ausgabe,(answer_rect.x,answer_rect.y))
pygame.display.update()
return answer.linetext
def text_output (surf,text_rect, text_to_display, color_rect=(0,0,255), color_text=(255,255,255),font_name="arial", font_size=25):
area_for_text = pygame.Surface((text_rect.w,text_rect.h))
area_out= draw_text_in ( text_to_display,area_for_text,color_rect , color_text,font_name,font_size)
surf.blit(area_out,(text_rect.x,text_rect.y))
#pygame.display.update()
def main():
# Constants
# COLORS
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
YELLOW= (255,255,0)
GREEN = (0,255,0)
BLUE = (0,0,200)
# Initialize all imported pygame modules
pygame.init()
# Dimension of the display Window
x_dim =600
y_dim =308
# Set the width and height of the screen [width, height]
screen = pygame.display.set_mode((x_dim,y_dim))
# Set the current window caption
pygame.display.set_caption("Text input and output in Pygame")
#
# Test of text_input() and text_output()
#
text_output (screen,pygame.Rect(0,180,400,70),"text_output\nexample",BLUE,YELLOW,font_name="ocra",font_size=30)
# Use Surface at Rect to print Text, with BGColor, TextColor in Font With Size
text_output (screen,pygame.Rect(160,30,400,60),"WAS ist die Antwort?",BLUE,YELLOW,font_name="ocra",font_size=30)
# Use Surface at Rect to input Text, with BGColor, TextColor in Font With Size
answer= text_input (screen,pygame.Rect(160,90,400,90),YELLOW,RED,font_name="arial",font_size=50)
print (answer)
# Use Surface at Rect to print Text with default settings
text_output (screen,pygame.Rect(60,240,200,60),answer)
# Example: Waiting for RETURN - KEY
# Use Surface at Rect to input Text WITHOUT GETTING THE RESULT with default settings
text_input (screen,pygame.Rect(160,90,400,90))
if __name__ == '__main__':
main()