在pygame中更改按钮为cli后的变量

2024-09-29 22:00:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用pygame在python中制作一个hangman游戏,问题是,当我点击按钮时,变量不变,我想让它在你点击字母后,如果猜测正确,它将显示在屏幕上。在

# EzText example
from pygame.locals import *
import pygame, sys, eztext
import time
import random
global oldwelcomemessage
global newwelcomemessage
global entertest
global mouse
global click
global positiona
pygame.init()
#pygame.mixer.init()
pygame.mixer.pre_init()
pygame.mixer.pre_init(22050, -16, 2, 4096)

Black      = (  0,   0,   0)
Grey       = (100, 100, 100)
DarkGrey   = (70,   70,  70)
White      = (255, 255, 255)
Blue       = (  0,   0, 255)
NavyBlue   = ( 60,  60, 100)
Cyan       = (  0, 255, 255)
Purple     = (255,   0, 255)
Red        = (255,   0,   0)
DarkRed    = (180,   0,   0)
Orange     = (255, 128,   0)
Yellow     = (255, 255,   0)
Green      = (  0, 255,   0)
DarkGreen  = (  0, 180,   0)
LightBlue  = ( 90, 164, 213)
Magenta    = (153,   0,  76)

screen = pygame.display.set_mode((1300,600),0,32)
screen.fill(Orange)

apicked = 0
positiona = None
def text_objects(text, font):
    textSurface = font.render(text, True, Black)
    return textSurface, textSurface.get_rect()
smallText = pygame.font.Font("freesansbold.ttf",20)

def button(msg,x,y,w,h,ic,ac,action=None):
    if x+w > mouse[0] > x and y+h > mouse[1] > y:  
        pygame.draw.rect(screen, ac,(x, y, w, h))     
        if click [0] == 1 and action!= None:          
            if action == "A":
                if 'a' in chars:
                    print('That character is in the word')
                    #print (position)
                    for position, item in enumerate(chars):
                        if item == 'a':
                            locationa = position
                            locationa = locationa +1
                            global positiona
                            #print (locationa)
                            apicked = 1
                            locationa = str(locationa)
                            print(locationa)




                else:
                    print('That character isnt in the word')
            if action == "B":
                guess = 'b'
            if action == "C":
                guess = 'c'
            if action == "D":
                guess = 'd'
            if action == "E":
                guess = 'e'
            if action == "F":
                guess = 'f'
            if action == "G":
                guess = 'g'
            if action == "H":
                guess = 'h'
            if action == "I":
                guess = 'i'
            if action == "J":
                guess = 'j'
            if action == "K":
                guess = 'k'
            if action == "L":
                guess = 'l'
            if action == "M":
                guess = 'm'
            if action == "N":
                guess = 'n'
            if action == "O":
                guess = 'o'
            if action == "P":
                guess = 'p'
            if action == "Q":
                guess = 'q'
            if action == "R":
                guess = 'r'
            if action == "S":
                guess = 's'
            if action == "T":
                guess = 't'
            if action == "U":
                guess = 'u'
            if action == "V":
                guess = 'v'
            if action == "W":
                guess = 'w'
            if action == "X":
                guess = 'x'
            if action == "Y":
                guess = 'y'
            if action == "Z":
                guess = 'z'



    else:
        pygame.draw.rect(screen, ic,(x, y, w, h))

    smallText = pygame.font.Font("freesansbold.ttf",20) 
    textSurf, textRect = text_objects(msg, smallText) 
    textRect.center = ((x+(w/2)), (y + (h/2))) 
    screen.blit(textSurf, textRect)

f = open('Everywordfixed.txt','r')
farray = f.readlines()
word =  (random.choice(farray))
print(word)
length = len(word)
length = length - 1
lengthforunder = len(word)
lengthforunder = lengthforunder - 1
print (length)
number = 1
underscorex = 490

t = open("Currentword.txt","w")
t.write(word)
t.close()
t = open("Currentword.txt","r")
chars = []
number = 0
letterx = 490
##for i in (word):
##
##    number = str(number)
##    charon = ('Char'+ number)
##    print(charon)
##    number = int(number)    
##    charon = i
##    number = number +1
##    label = smallText.render((charon), 1, (255,255,0))
##    screen.blit(label, (letterx, 250))
##    letterx = letterx + 50
##    print(charon)
for i in (word):
    chars.append(i)
print(chars)

while True:


    #pygame.draw.rect(screen, White,(10, 10, 980, 580)) 
    pygame.mixer.pre_init(22050, -16, 2, 4096)
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    events = pygame.event.get()
    for event in events:

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
    pygame.display.update()
    time.sleep(0.02)

    clock = pygame.time.Clock()   
    clock.tick(30)
    events = pygame.event.get()
    A = button("A",10, 9, 150, 50, White, DarkGreen,"A")
    B = button("B",10, 68, 150, 50, White, DarkGreen,"B")
    C = button("C",10, 127, 150, 50, White, DarkGreen,"C")
    D = button("D",10, 186, 150, 50, White, DarkGreen,"D")
    E = button("E",10, 245, 150, 50, White, DarkGreen,"E")
    F = button("F",10, 304, 150, 50, White, DarkGreen,"F")
    G = button("G",10, 363, 150, 50, White, DarkGreen,"G")
    H = button("H",10, 422, 150, 50, White, DarkGreen,"H")
    I = button("I",10, 481, 150, 50, White, DarkGreen,"I")
    J = button("J",10, 540, 150, 50, White, DarkGreen,"J")
    K = button("K",170, 9, 150, 50, White, DarkGreen,"K")
    L = button("L",170, 68, 150, 50, White, DarkGreen,"L")
    M = button("M",170, 127, 150, 50, White, DarkGreen,"M")
    N = button("N",170, 186, 150, 50, White, DarkGreen,"N")
    O = button("O",170, 245, 150, 50, White, DarkGreen,"O")
    P = button("P",170, 304, 150, 50, White, DarkGreen,"P")
    Q = button("Q",170, 363, 150, 50, White, DarkGreen,"Q")
    R = button("R",170, 422, 150, 50, White, DarkGreen,"R")
    S = button("S",170, 481, 150, 50, White, DarkGreen,"S")
    T = button("T",170, 540, 150, 50, White, DarkGreen,"T")
    U = button("U",330, 9, 150, 50, White, DarkGreen,"U")
    V = button("V",330, 68, 150, 50, White, DarkGreen,"V")
    W = button("W",330, 127, 150, 50, White, DarkGreen,"W")
    X = button("X",330, 186, 150, 50, White, DarkGreen,"X")
    Y = button("Y",330, 245, 150, 50, White, DarkGreen,"Y")
    Z = button("Z",330, 304, 150, 50, White, DarkGreen,"Z")

    print(positiona)

    if positiona == 1:
        pygame.draw.rect(screen, Black,(underscorex, 300, 90, 10))
    if positiona == 2:
        pygame.draw.rect(screen, Black,(underscorex, 330, 90, 10))
    if positiona == 3:
        pygame.draw.rect(screen, Black,(underscorex, 360, 90, 10))
    if positiona == 4:
        pygame.draw.rect(screen, Black,(underscorex, 400, 90, 10))    

    #print(apicked)
    #if apicked == 1:
        #pygame.draw.rect(screen, Black,(underscorex, 300, 90, 10))
    for i in range(lengthforunder):
        pygame.draw.rect(screen, Black,(underscorex, 300, 30, 10))
        underscorex = underscorex +50
        lengthforunder = lengthforunder-1




    pygame.display.flip()

if __name__ == '__main__': main()

如何在屏幕上正确显示字母?在


Tags: rectnumberifbuttonactionscreenpygameword

热门问题