import pygame,sys
import random,time
class gridworld:
size = width, height = 720, 720
background_color = 250, 220, 220
divding_line_color = 50, 100, 50
divding_line_width = 10
rect_color = 234, 88, 6
def __init__(self,w=10,h=10):
self.w = w
self.h = h
self.state = random.randint(1, self.w * self.h - 1)
def number_list(self, Grid=0):
h = 1
self.Grid = [[Grid for i in range(self.w)] for j in range(self.h)]
for i in range(self.w):
for j in range(self.h):
self.Grid[i][j] = h
h += 1
return self.Grid
class Main_Game:
pygame.init()
pygame.mixer.init()
music_1 = pygame.mixer.Sound('D://jianmooo//新版剑魔语音包标注+内容对照//非语音//1huaaaaa~.wav')
music_2 = pygame.mixer.Sound('D://jianmooo//新版剑魔语音包标注+内容对照//非语音//a~sihasiha.wav')
music_3 = pygame.mixer.Sound('D://jianmooo//新版剑魔语音包标注+内容对照//非语音//heaaaaa~.wav')
music_4 = pygame.mixer.Sound('D://jianmooo//哈哈哈哈啊,谢谢你.wav')
grid = gridworld()
font = pygame.font.SysFont(None,int((grid.width+grid.height)/(grid.w+grid.h)))
screen = pygame.display.set_mode(grid.size)
pygame.display.set_caption('Grid World Game')
random_number = random.randint(1, grid.w * grid.h)
random_quotient = random_number // grid.w
random_remainder = random_number % grid.w
if random_remainder == 0:
random_remainder = grid.h - 1
random_quotient -= 1
rect_path_start = [random_remainder * grid.width / grid.w + 5,random_quotient * grid.height / grid.h + 5]
else:
random_remainder -= 1
rect_path_start = [random_remainder * grid.width / grid.w + 5, random_quotient * grid.height / grid.h + 5]
FPS = 60
clock = pygame.time.Clock()
path_number = random_number
print(random_number,random_quotient,random_remainder,rect_path_start)
reward = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(grid.background_color)
for i in range(grid.w): #draw 横线
pygame.draw.line(screen,grid.divding_line_color,
start_pos=((grid.width/grid.w)*i,0),
end_pos=((grid.width/grid.w)*i,grid.height),
width=grid.divding_line_width)
for i in range(grid.h): #draw 竖线
pygame.draw.line(screen, grid.divding_line_color,
start_pos=(0,(grid.height / grid.h) * i),
end_pos=(grid.width,(grid.height / grid.h) * i),
width=grid.divding_line_width)
pygame.draw.line(screen, grid.divding_line_color, #补充线
start_pos=(grid.width, 0),
end_pos=(grid.width, grid.height),
width=grid.divding_line_width)
pygame.draw.line(screen, grid.divding_line_color,
start_pos=(0,grid.width),
end_pos=(grid.height, grid.width),
width=grid.divding_line_width)
if event.type == pygame.KEYDOWN: #move判断
if event.key == pygame.K_UP:
if random_quotient != 0:
random_quotient -= 1
rect_path_start = [random_remainder * grid.width / grid.w + 5,
random_quotient * grid.height / grid.h + 5]
path_number -= grid.w
print('up',random_number,path_number)
elif event.key == pygame.K_DOWN:
if random_quotient != grid.h - 1:
random_quotient += 1
rect_path_start = [random_remainder * grid.width / grid.w + 5,
random_quotient * grid.height / grid.h + 5]
path_number += grid.w
print('down',random_number,path_number)
elif event.key == pygame.K_LEFT:
if random_remainder != 0:
random_remainder -= 1
rect_path_start = [random_remainder * grid.width / grid.w + 5,
random_quotient * grid.height / grid.h + 5]
path_number -= 1
print('left',random_number,path_number)
elif event.key == pygame.K_RIGHT:
if random_remainder != grid.w - 1:
random_remainder += 1
rect_path_start = [random_remainder * grid.width / grid.w + 5,
random_quotient * grid.height / grid.h + 5]
path_number += 1
print('right',random_number,path_number)
if path_number != grid.w*grid.h: #reward判断
reward -= 1
i_music = random.randint(1,3)
if i_music == 1:
music_1.play()
elif i_music == 2:
music_2.play()
else:
music_3.play()
else:
reward += 100
music_4.play()
print(reward)
pygame.display.set_caption('Help 亚托克斯 Find 木瓜星灵 当前得分:{0}'.format(reward))
orangerect = pygame.draw.rect(screen, grid.rect_color,
[rect_path_start, (grid.width / grid.w - grid.divding_line_width,
grid.height / grid.h - grid.divding_line_width)])
image = pygame.image.load('D://剑魔.png')
image = pygame.transform.smoothscale(image,(grid.width/grid.w - 0.5 * grid.divding_line_width,
grid.height/grid.h - 0.5 * grid.divding_line_width))
image_2 = pygame.image.load('D://暮光星灵.jpg')
image_2 = pygame.transform.smoothscale(image_2,(grid.width/grid.w - 0.5 * grid.divding_line_width,
grid.height/grid.h - 0.5 * grid.divding_line_width))
for i in range(grid.w):
for j in range(grid.h):
if grid.number_list()[i][j] != 0:
text_surface_obj = font.render(str(grid.number_list()[i][j]),True,(0,0,0))
text_rect_obj = text_surface_obj.get_rect()
text_rect_path = [(j+1)*grid.width/grid.w-0.5*grid.width/grid.w,
(i+1)*grid.height/grid.h-0.5*grid.height/grid.h]
text_rect_obj.center = tuple(text_rect_path)
screen.blit(text_surface_obj,text_rect_obj)
screen.blit(image_2, (grid.width - (grid.width / grid.w - 0.5 * grid.divding_line_width),
grid.height - (grid.height / grid.h - 0.5 * grid.divding_line_width)))
screen.blit(image, rect_path_start)
clock.tick(FPS)
pygame.display.update()
if path_number == grid.w*grid.h:
pygame.display.set_caption('Help 亚托克斯 Find 木瓜星灵 挑战成功休息一秒钟,得分{0}'.format(reward))
time.sleep(0.1)
pygame.display.update()
if __name__ == "__main__":
play = Main_Game()
代码运行结果:
修改gridworld的__init__方法里的w,h参数能改变方块个数,如下图:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)