Amazing-Python-Scripts

Форк
0
121 строка · 3.1 Кб
1
import pygame
2
import math
3
import random
4

5
# setup display
6
pygame.init()
7
WIDTH, HEIGHT = 800, 500
8
win = pygame.display.set_mode((WIDTH, HEIGHT))
9
pygame.display.set_caption("Hangman Game")
10

11
# button variables
12
radius = 20
13
gap = 15
14
letters = []
15
startx = round((WIDTH - (radius * 2 + gap) * 13) / 2)
16
starty = 400
17
A = 65
18
for i in range(26):
19
    x = startx + gap * 2 + ((radius * 2 + gap) * (i % 13))
20
    y = starty + ((i//13) * (gap + radius * 2))
21
    letters.append([x, y, chr(A+i), True])
22

23
# load images
24
images = []
25
for i in range(7):
26
    img = pygame.image.load("./Hangman-Game/hangman" + str(i) + ".png")
27
    images.append(img)
28

29
# game variables
30
hangman_status = 0
31
with open("./Hangman-Game/words.txt", 'r') as f:
32
    content = f.read()
33
list_of_words = content.split(",")
34
word = random.choice(list_of_words).upper()
35
guessed = []
36

37
# colors
38
white = (255, 255, 255)
39
BLACK = (0, 0, 0)
40
BLUE = (180, 219, 251)
41
PINK = (232, 90, 202)
42

43
# fonts
44
LETTER_FONTS = pygame.font.SysFont('comicsans', 40)
45
WORD_FONTS = pygame.font.SysFont('comicsans', 60)
46
TITLE_FONTS = pygame.font.SysFont('comicsans', 70)
47

48

49
def draw():
50
    win.fill(BLUE)
51
    # draw title
52
    text = TITLE_FONTS.render("HANGMAN GAME", 1, BLACK)
53
    win.blit(text, (WIDTH/2 - text.get_width()/2, 20))
54
    # draw word
55
    display_word = ""
56
    for i in word:
57
        if i in guessed:
58
            display_word += i + " "
59
        else:
60
            display_word += "_ "
61

62
    text = WORD_FONTS.render(display_word, 1, BLACK)
63
    win.blit(text, (400, 200))
64
    # draw buttons
65
    for i in letters:
66
        x, y, ltr, visible = i
67
        if visible:
68
            pygame.draw.circle(win, BLACK, (x, y), radius, 3)
69
            text = LETTER_FONTS.render(ltr, 1, BLACK)
70
            win.blit(text, (x - text.get_width()/2, y - text.get_height()/2))
71
    win.blit(images[hangman_status], (150, 100))
72
    pygame.display.update()
73

74
# win/loose msg printing msg on screen
75

76

77
def display_message(message):
78
    pygame.time.delay(1000)
79
    win.fill(PINK)
80
    text = WORD_FONTS.render(message, 1, BLACK)
81
    win.blit(text, (WIDTH/2 - text.get_width() /
82
             2, HEIGHT/2 - text.get_height()/2))
83
    pygame.display.update()
84
    pygame.time.delay(3000)
85

86

87
# setup game loop
88
FPS = 60
89
clock = pygame.time.Clock()
90
run = True
91
while run:
92
    clock.tick(FPS)
93
    draw()
94
    for i in pygame.event.get():
95
        if i.type == pygame.QUIT:
96
            run = False
97
        if i.type == pygame.MOUSEBUTTONDOWN:
98
            m_x, m_y = pygame.mouse.get_pos()
99
            for i in letters:
100
                x, y, ltr, visible = i
101
                if visible:
102
                    dis = math.sqrt((x - m_x)**2 + (y - m_y)**2)
103
                    if dis < radius:
104
                        i[3] = False
105
                        guessed.append(ltr)
106
                        if ltr not in word:
107
                            hangman_status += 1
108
    draw()
109
    # checking for winner
110
    won = True
111
    for i in word:
112
        if i not in guessed:
113
            won = False
114
            break
115
    if won:
116
        display_message("Wohooo...!! You Won!")
117
        break
118
    if hangman_status == 6:
119
        display_message(f"Oopss..!! It was {word} You Lost!")
120
        break
121
pygame.quit()
122

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.