用户:
泡杯查看:5 回复:6 评论:5 创建时间:2021-10-09T21:38:58
import pygame
from random import randint, choice
from math import floor
from time import sleep
from sys import exit
pygame.init()
pygame.display.init()
pygame.mixer.init()
screen = pygame.display.set_mode((1366, 768), pygame.FULLSCREEN)
pygame.mouse.set_visible(False)
pygame.mouse.set_pos(0, 0)
channel = pygame.mixer.find_channel(True)
pygame.mixer.set_num_channels(6)
def load(file, size=None, set_size = None):
postfix = file[len(file) - 4:len(file)]
if postfix == '.png':
image = pygame.image.load('C:/Users/hp/Desktop/Python/Picture/FNAF/' + file).convert_alpha()
if size != None:
width, height = image.get_size()
width = floor(width * size)
height = floor(height * size)
image = pygame.transform.喵oothscale(image, (width, height))
elif set_size != None:
image = pygame.transform.喵oothscale(image, set_size)
return image
elif postfix == '.wav':
sound = pygame.mixer.Sound('C:/Users/hp/Desktop/Python/Sound/FNAF/' + file)
return sound
def text(content, size, color, pos):
font = pygame.font.Font(None, size)
text = font.render(content, True, color)
screen.blit(text, pos)
pygame.display.update()
class Mouse:
def __init__(self):
self.image = load('mouse.png', 0.8)
self.pos = None
def update(self):
self.pos = pygame.mouse.get_pos()
if self.pos[0] > 1345:
pygame.mouse.set_pos(1345, self.pos[1])
if self.pos[1] > 745:
pygame.mouse.set_pos(self.pos[0], 745)
screen.blit(self.image, self.pos)
class Room:
def __init__(self):
self.images = []
for count in range(12):
image = load('room-' + str(count + 1) + '.png', 4)
self.images.append(image)
self.images[0] = pygame.transform.喵oothscale(self.images[0], (2540, 1068))
self.frame = 0
self.pos = [0, 0]
def update(self):
x, y = pygame.mouse.get_pos()
if self.frame < 11:
self.frame += 1
else:
self.frame = 0
self.pos[0] = x * -0.8
self.pos[1] = y * -0.38
if floor(power.number / 100) != 0:
screen.blit(self.images[self.frame], self.pos)
else:
screen.blit(self.images[11], self.pos)
class Button:
def __init__(self, pos):
self.image_1 = load('button-1.png')
self.image_2 = load('button-2.png')
self.image_2 = pygame.transform.喵oothscale(self.image_2, (87, 95))
self.image = self.image_1
self.rect = self.image.get_rect()
self.rect.centerx = pos[0]
self.rect.centery = pos[1]
self.pos = pos
self.hit = False
self.wait = False
def update(self):
if floor(power.number / 100) != 0:
if not monitor.open:
if self.rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0] == 1:
if not self.wait:
pygame.mixer.Channel(1).play(door_sound)
if self.hit:
self.hit = False
else:
self.hit = True
self.wait = True
if self.wait and pygame.mouse.get_pressed()[0] == 0:
self.wait = False
if self.hit:
self.image = self.image_2
else:
self.image = self.image_1
self.rect.centerx = self.pos[0] + room.pos[0]
self.rect.centery = self.pos[1] + room.pos[1]
screen.blit(self.image, self.rect)
elif self.hit:
self.hit = False
class Door:
def __init__(self, name, frame, pos, button, size):
self.images = []
for count in range(frame):
image = load(name + '-' + str(count + 1) + '.png', door_size[size])
self.images.append(image)
self.pos = pos
self.frame = 0
self.max = frame - 1
self.wait = 0
self.button = button
self.rect = self.images[self.max].get_rect()
self.rect.center = pos
def update(self):
if floor(power.number / 100) != 0:
self.wait += 1
if self.wait / 4 == 1:
if self.button.hit:
if self.frame < self.max:
self.frame += 1
else:
if self.frame > 0:
self.frame -= 1
self.wait = 0
self.rect.centerx = self.pos[0] + room.pos[0]
self.rect.centery = self.pos[1] + room.pos[1]
if self.frame != 0:
screen.blit(self.images[self.frame], self.rect)
elif self.frame > 0:
self.frame -= 1
screen.blit(self.images[self.frame], self.rect)
class Monitor:
def __init__(self):
self.arrow = load('arrow.png', 0.7)
self.map = load('map.png', 0.6)
self.monitors = []
self.snow = []
self.CAMS_1 = []
self.CAMS_2 = []
self.images = {}
self.CAM_pos = ((960, 600), (1200, 600), (890, 520), (1270, 520), (1080, 470), (980, 350), (1180, 350))
self.CAM_rect = []
for count in range(2):
image = load('snow-' + str(count + 1) + '.png', None, (1285, 685))
self.snow.append(image)
for count in range(7):
image = load('monitor-' + str(count + 1) + '.png', 2)
self.monitors.append(image)
for count in range(7):
image = load('CAM_' + str(count + 1) + '-0' + '.png', 0.5)
self.CAMS_1.append(image)
rect = image.get_rect()
rect.center = self.CAM_pos[count]
self.CAM_rect.append(rect)
for count in range(7):
if count != 5 and count != 6:
image = load('CAM_' + str(count + 1) + '-1' + '.png', 0.5)
else:
image = load('CAM_' + str(count + 1) + '-1' + '.png', 0.62)
self.CAMS_2.append(image)
for count in range(7):
for count in range(3):
image = load('CAM01-' + str(count + 1) + '.png', None, (1285, 685))
self.images['1-' + str(count + 1)] = image
for count in range(3):
image = load('CAM02-' + str(count + 1) + '.png', None, (1285, 685))
self.images['2-' + str(count + 1)] = image
for count in range(2):
image = load('CAM03-' + str(count + 1) + '.png', None, (1285, 685))
self.images['3-' + str(count + 1)] = image
for count in range(2):
image = load('CAM04-' + str(count + 1) + '.png', None, (1285, 685))
self.images['4-' + str(count + 1)] = image
for count in range(3):
image = load('CAM05-' + str(count + 1) + '.png', None, (1285, 685))
self.images['5-' + str(count + 1)] = image
for count in range(2):
image = load('CAM06-' + str(count + 1) + '.png', None, (1285, 685))
self.images['6-' + str(count + 1)] = image
for count in range(2):
image = load('CAM07-' + str(count + 1) + '.png', None, (1285, 685))
self.images['7-' + str(count + 1)] = image
self.arrow_rect = self.arrow.get_rect()
self.arrow_rect.center = (670, 740)
self.open = False
self.wait = False
self.delay = 0
self.monitor_frame = 0
self.snow_frame = 0
self.y = 0
self.index = 0
self.alpha = 255
self.mouse_down = False
def __blit_monitor(self, mode):
if self.delay / 4 == 1:
if mode:
self.y = 0
if self.monitor_frame < 6:
self.monitor_frame += 1
else:
if self.monitor_frame > 0:
self.monitor_frame -= 1
self.y = 400
self.delay = 0
if self.monitor_frame != 0:
screen.blit(self.monitors[self.monitor_frame], (0, self.y))
if self.monitor_frame == 6:
for rect in self.CAM_rect:
if rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0] == 0 and self.mouse_down:
self.alpha = 255
self.mouse_down = False
pygame.mixer.Channel(2).play(switch)
self.index = self.CAM_rect.index(rect)
if robot.pos != None and self.index + 1 == int(robot.pos[0]):
screen.blit(self.images[robot.pos], (43, 45))
else:
screen.blit(self.images[str(self.index + 1) + '-1'], (43, 45))
screen.blit(self.map, (900, 350))
for count in range(7):
if self.index == count:
screen.blit(self.CAMS_2[count], self.CAM_rect[count])
else:
screen.blit(self.CAMS_1[count], self.CAM_rect[count])
snow_surface = pygame.Surface((1285, 685))
snow_surface.blit(self.snow[self.snow_frame], (0, 0))
snow_surface.set_alpha(self.alpha)
if self.alpha > 40:
self.alpha -= 10
screen.blit(snow_surface, (43, 45))
if self.snow_frame < 1:
self.snow_frame += 1
else:
self.snow_frame = 0
def update(self):
if floor(power.number / 100) != 0:
self.delay += 1
if self.arrow_rect.collidepoint(pygame.mouse.get_pos()):
if not self.wait:
if self.open:
pygame.mixer.Channel(1).play(flip_down)
self.open = False
else:
pygame.mixer.Channel(1).play(flip_up)
self.open = True
self.wait = True
if not self.arrow_rect.collidepoint(pygame.mouse.get_pos()):
self.wait = False
if self.open:
self.__blit_monitor(True)
else:
self.__blit_monitor(False)
if pygame.mouse.get_pressed()[0] == 1:
self.mouse_down = True
screen.blit(self.arrow, self.arrow_rect)
elif self.open:
pygame.mixer.Channel(1).play(flip_down)
self.open = False
class Robot:
def __init__(self):
self.pos = None
self.old_pos = self.pos
self.attack = 0
self.say = 0
self.state = [3, 3, 2, 2, 3, 2, 2]
self.喵 = False
self.count = [0, 0]
self.target_time = [randint(1000, 2400), randint(1000, 2400)]
self.voice = []
self.move_sound = []
for count in range(15):
voice = load('Audio-' + str(count + 1) + '.wav')
self.voice.append(voice)
for count in range(12):
sound = load('move-' + str(count + 1) + '.wav')
self.move_sound.append(sound)
def update(self):
self.count[0] += 1
self.count[1] += 1
if self.count[0] == self.target_time[0]:
pygame.mixer.Channel(3).play(self.voice[self.say])
self.count[0] = 0
self.target_time[0] = randint(1000, 2400)
if self.say < 14:
self.say += 1
else:
self.say = 0
if self.count[1] == self.target_time[1]:
monitor.alpha = 255
pos = randint(1, 7)
if self.pos == '1-2' or self.pos == '2-2' or self.pos == '5-2':
if randint(0, 2) != 0:
self.pos = self.pos[0:2] + '3'
else:
self.pos = str(pos) + '-2'
if self.pos == self.old_pos:
all_pos = [1, 2, 3, 4, 5, 6, 7]
all_pos.remove(pos)
pos = choice(all_pos)
self.pos = str(pos) + '-2'
if self.pos != self.old_pos:
self.old_pos = self.pos
pygame.mixer.Channel(4).play(self.move_sound[randint(0, 11)])
self.count[1] = 0
self.target_time[1] = randint(1000, 2400)
if self.pos != None:
if self.pos[2] == '3':
self.attack += 1
else:
self.attack = 0
if self.pos == '1-3' and buttons[0].hit == False and self.attack > 400:
self.喵 = True
if self.pos == '2-3' and buttons[2].hit == False and self.attack > 400:
self.喵 = True
if self.pos == '5-3' and buttons[1].hit == False and self.attack > 400:
self.喵 = True
class Num:
def __init__(self, size):
self.images = []
for count in range(9):
image = load(str(count + 1) + '.png', None, size)
self.images.append(image)
def blit(self, num, pos):
if len(str(num)) == 1:
screen.blit(self.images[num - 1], pos)
else:
str_num = str(num)
screen.blit(self.images[int(str_num[0]) - 1], pos)
screen.blit(self.images[int(str_num[1]) - 1], (pos[0] + 25, pos[1]))
class Usage:
def __init__(self):
self.images = {}
self.number = 1
self.state = [0, 0, 0, 0]
for count in range(5):
image = load('USAGE-' + str(count + 1) + '.png', None, (115, 65))
self.images[count + 1] = image
def update(self):
self.number = 1
for count, button in enumerate(buttons):
if button.hit:
self.state[count] = 1
else:
self.state[count] = 0
if monitor.open:
self.state[3] = 1
else:
self.state[3] = 0
for each in self.state:
self.number += each
screen.blit(self.images[self.number], (50, 50))
class Power:
def __init__(self):
self.image = load('POWER.png', 0.8)
self.num = Num((20, 24))
self.number = 10000
self.play = False
def update(self):
if self.number > 0.01:
screen.blit(self.image, (50, 150))
self.num.blit(floor(self.number / 100), (70, 180))
if keys[pygame.K_SPACE]:
text(str(floor(self.number / 100)), 50, (255, 255, 255), (20, 20))
if self.number > 0:
self.number -= usage.number * 10 #0.08
elif not self.play:
pygame.mixer.Channel(5).play(power_cut)
self.play = True
class Time:
def __init__(self):
self.image = load('AM.png')
self.win = load('win.png')
self.num = Num((24, 28))
self.number = 0
def update(self):
screen.blit(self.image, (1200, 100))
if floor(self.number / 1000) == 0:
self.num.blit(12, (1140, 103))
else:
self.num.blit(floor(self.number / 1000), (1160, 103))
if self.number < 12000:
self.number += 0.1
else:
self.number = 12000
if floor(self.number / 1000) == 6:
pygame.mixer.stop()
pygame.mixer.Channel(5).play(win)
while True:
screen.fill((0, 0, 0))
screen.blit(self.win, (500, 400))
pygame.display.update()
text('LOADING...', 60, (255, 255, 255), (600, 340))
pygame.draw.rect(screen, (255, 255, 255), (400, 420, 620, 30), 5)
pygame.display.update()
snow = []
snow_frame = 0
for count in range(2):
image = load('snow-' + str(count + 1) + '.png', None, (1366, 768))
snow.append(image)
喵 = []
喵_frame = 0
for count in range(24):
image = load('喵-' + str(count + 1) + '.png', None, (1366, 768))
喵.append(image)
buttons = []
button_pos = ((560, 630), (1030, 180), (1950, 620))
doors = {}
door_name = ('left_door', 'mid_door', 'right_door')
door_pos = ((530, 600), (1260, 160), (1940, 635))
door_size = (2, 0.93, 2.3)
for count in range(3):
button = Button(button_pos[count])
buttons.append(button)
for count in range(3):
if count == 2:
door = Door(door_name[count], 9, door_pos[count], buttons[count], count)
else:
door = Door(door_name[count], 8, door_pos[count], buttons[count], count)
doors[door_name[count]] = door
mouse = Mouse()
room = Room()
monitor = Monitor()
robot = Robot()
usage = Usage()
power = Power()
time = Time()
game_music = load('game.wav')
door_sound = load('door.wav')
flip_up = load('flip_up.wav')
flip_down = load('flip_down.wav')
switch = load('switch.wav')
scream = load('scream.wav')
power_cut = load('power_cut.wav')
win = load('win.wav')
length = 0
while length < 600:
length += 100
screen.fill((0, 0, 0))
text('LOADING...', 60, (255, 255, 255), (600, 340))
pygame.draw.rect(screen, (255, 255, 255), (400, 420, 620, 30), 5)
pygame.draw.rect(screen, (255, 255, 255), (410, 431, length, 8), 8)
text(str(floor(length / 6)) + '%', 50, (255, 255, 255), (650, 500))
pygame.display.update()
sleep(0.5)
pygame.mixer.Channel(0).play(game_music, -1)
while True:
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pygame.quit()
exit()
screen.fill((0, 0, 0))
if not robot.喵:
room.update()
for button in buttons:
button.update()
for door in doors.values():
door.update()
monitor.update()
robot.update()
usage.update()
power.update()
time.update()
mouse.update()
else:
pygame.mixer.stop()
pygame.mixer.Channel(0).play(scream)
break
pygame.display.update()
while True:
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pygame.quit()
exit()
screen.blit(room.images[0], (0, 0))
screen.blit(喵[floor(喵_frame / 30)], (0, 0))
if 喵_frame < 690:
喵_frame += 1
else:
break
pygame.display.update()
while True:
for event in pygame.event.get():
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pygame.quit()
exit()
screen.blit(snow[snow_frame], (0, 0))
if snow_frame < 1:
snow_frame += 1
else:
snow_frame = 0
pygame.display.update()
扩展游戏循环:
创建检查事件(如退出游戏)、更新游戏状态和渲染游戏图形的主循环。例如:扩展游戏循环:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update game state
mouse.update()
room.update()
button.update()
door.update()
# Render everything
screen.fill((0, 0, 0)) # Clear the screen
room.update()
mouse.update()
button.update()
door.update()
pygame.display.flip() # Update the full display surface to the screen点赞0
评论