From 5fedd0a4d39d0826e998af825f407492421bf86c Mon Sep 17 00:00:00 2001 From: huangjielun <2872405629@qq.com> Date: Fri, 26 Aug 2022 09:47:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=A7=8B=E9=A1=B5=E9=9D=A2=E5=92=8C?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E5=9D=90=E6=A0=87=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.py | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 155 insertions(+), 5 deletions(-) diff --git a/src/main.py b/src/main.py index 54feb2f..83de6e1 100644 --- a/src/main.py +++ b/src/main.py @@ -4,9 +4,30 @@ pygame.init() pygame.font.init() pygame.mixer.init() +WIDTH , HEIGHT = 1200 , 800 +def calc(x,y): + new_x = int(x / WIDTH * 9) + new_y = int(y / HEIGHT * 7) + return (new_x,new_y) +MAP = [[0 for i in range(7)] for i in range(9)]#1表示左侧的动物,2表示右侧的动物,3表示左侧的陷阱,4表示右侧的陷阱,5表示左侧的兽穴,6表示右侧的兽穴 +x,y=calc(87,278) +MAP[x][y] = 3 +x,y = calc(211,378) +MAP[x][y] = 3 +x,y = calc(85,496) +MAP[x][y] = 3 +x,y = calc(81,388) +MAP[x][y] = 5 +x,y = calc(1105,282) +MAP[x][y] = 4 +x,y = calc(976,381) +MAP[x][y] = 4 +x,y = calc(1114,497) +MAP[x][y] = 4 +x,y = calc(1102,379) +MAP[x][y] = 6 #模块############################### FPS = 60 -WIDTH , HEIGHT = 1200 , 800 WIN = pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("Jungle") SPACE = pygame.transform.scale(pygame.image.load('images/map.png'),(WIDTH,HEIGHT)) @@ -34,6 +55,7 @@ Tiger = pygame.mixer.Sound('audio/Tiger.mp3') Wolf = pygame.mixer.Sound('audio/Wolf.mp3') Fox = pygame.mixer.Sound('audio/Fox..mp3') Eagle = pygame.mixer.Sound('audio/Eagle.mp3') +pygame.mixer.music.set_volume(0.5) #################################################### #animals img #Left @@ -75,6 +97,84 @@ LightPink=(255,182,193) DeepSkyBlue=(0,191,255) ############################################################# +#开始页面# +font_addr = pygame.font.get_default_font() +font = pygame.font.Font(font_addr, 36) + + +class Button(object): + def __init__(self, text, color, x=None, y=None, **kwargs): + self.surface = font.render(text, True, color) + + self.WIDTH = self.surface.get_width() + self.HEIGHT = self.surface.get_height() + + if 'centered_x' in kwargs and kwargs['centered_x']: + self.x = WIDTH // 2 - self.WIDTH // 2 + else: + self.x = x + + if 'centered_y' in kwargs and kwargs['centered_y']: + self.y = HEIGHT //2 - self.HEIGHT // 2 + else: + self.y = y + + def display(self): + WIN.blit(self.surface, (self.x, self.y)) + + def check_click(self, position): + x_match = (position[0] > self.x) and (position[0] < self.x + self.WIDTH) + y_match = (position[1] > self.y) and (position[1] < self.y + self.HEIGHT) + if x_match and y_match: + return True + else: + return False + + +def starting_screen(): + WIN.blit(SPACE, (0, 0)) + + game_title = font.render('Starting Screen', True, WHITE) + + WIN.blit(game_title, (WIDTH//2 - game_title.get_width()//2, 150)) + + play_button = Button('Play!', WHITE, None, 350, centered_x=True) + exit_button = Button('Exit!', WHITE, None, 400, centered_x=True) + + play_button.display() + exit_button.display() + + pygame.display.update() + + while True: + + if play_button.check_click(pygame.mouse.get_pos()): + play_button = Button('Play!', RED, None, 350, centered_x=True) + else: + play_button = Button('Play!', WHITE, None, 350, centered_x=True) + + if exit_button.check_click(pygame.mouse.get_pos()): + exit_button = Button('Exit!', RED, None, 400, centered_x=True) + else: + exit_button = Button('Exit!', WHITE, None, 400, centered_x=True) + + play_button.display() + exit_button.display() + pygame.display.update() + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + pygame.quit() + raise SystemExit + if pygame.mouse.get_pressed()[0]: + if play_button.check_click(pygame.mouse.get_pos()): + return True + if exit_button.check_click(pygame.mouse.get_pos()): + return False + + +###################### + #TEXT def print_text(WIN,font,x,y,text,fcolor=WHITE): imgText = font .render(text,True,fcolor) @@ -111,7 +211,10 @@ def draw_window(l_elephant,l_eagle,l_wolf,l_lion,l_leopard,l_mouse,l_fox,r_eleph pygame.display.update() def main(): - + BGM.play(-1) + game_status = starting_screen() + if not game_status: + return True #animals rect l_elephant = pygame.Rect(281,10,CHESSSIZE_Y,CHESSSIZE_Y) l_eagle = pygame.Rect(157,120,CHESSSIZE_Y,CHESSSIZE_Y) @@ -129,10 +232,57 @@ def main(): r_fox = pygame.Rect(920,123,CHESSSIZE_Y,CHESSSIZE_Y) r_mouse = pygame.Rect(793,16,CHESSSIZE_Y,CHESSSIZE_Y) ############################################# + + #将动物的像素坐标转化为格子x-y轴坐标 + r_elephant_x = int((r_elephant.x+20) / WIDTH * 9) + r_elephant_y = int((r_elephant.y + 20 ) / HEIGHT * 7) + MAP[r_elephant_x][r_elephant_y]=2 + r_eagle_x = int((r_eagle.x+20) / WIDTH * 9) + r_eagle_y = int ((r_eagle.y + 20) / HEIGHT * 7) + MAP[r_eagle_x][r_eagle_y] = 2 + r_wolf_x = int((r_wolf.x ) / WIDTH * 9) + r_wolf_y = int(r_wolf.y / HEIGHT * 7) + MAP[r_wolf_x][r_wolf_y] = 2 + r_lion_x = int((r_lion.x ) / WIDTH * 9) + r_lion_y = int(r_lion.y / HEIGHT * 7) + MAP[r_lion_x][r_lion_y]=2 + r_leopard_x = int((r_leopard.x+20) / WIDTH * 9) + r_leopard_y = int(r_leopard.y / HEIGHT *7) + MAP[r_leopard_x][r_leopard_y] = 2 + r_mouse_x = int((r_mouse.x+20) / WIDTH * 9) + r_mouse_y = int(r_mouse.y / HEIGHT * 7) + MAP[r_mouse_x][r_mouse_y] = 2 + r_fox_x = int((r_fox.x+20) / WIDTH * 9) + r_fox_y = int((r_fox.y ) / HEIGHT * 7) + MAP[r_fox_x][r_fox_y] = 2 + + l_elephant_x = int(l_elephant.x / WIDTH * 9) + l_elephant_y = int(l_elephant.y / HEIGHT * 7) + MAP[l_elephant_x][l_elephant_y] = 1 + l_eagle_x = int(l_eagle.x / WIDTH * 9) + l_eagle_y = int(l_eagle.y / HEIGHT * 7) + MAP[l_eagle_x][l_eagle_y] = 1 + l_wolf_x = int(l_wolf.x / WIDTH * 9) + l_wolf_y = int(l_wolf.y / HEIGHT * 7) + MAP[l_wolf_x][l_wolf_y] = 1 + l_lion_x = int(l_lion.x / WIDTH * 9) + l_lion_y = int(l_lion.y / HEIGHT * 7) + MAP[l_lion_x][l_lion_y] = 1 + l_leopard_x = int(l_leopard.x / WIDTH * 9) + l_leopard_y = int((l_leopard.y + 20) / HEIGHT * 7) + MAP[l_leopard_x][l_leopard_y] = 1 + l_mouse_x = int(l_mouse.x / WIDTH * 9) + l_mouse_y = int((l_mouse.y + 30) / HEIGHT * 7) + MAP[l_mouse_x][l_mouse_y] = 1 + l_fox_x = int(l_fox.x / WIDTH * 9) + l_fox_y = int((l_fox.y + 20) / HEIGHT * 7) + MAP[l_fox_x][l_fox_y] = 1 + ############################# + print(MAP) run = True clock = pygame.time.Clock() font1 = pygame.font.SysFont('kaiti',40) - status = 0; + status = 0 while run: clock.tick(FPS) for event in pygame.event.get(): @@ -142,8 +292,8 @@ def main(): b1, b2, b3 = pygame.mouse.get_pressed() if b1 and not b3: mouse_x , mouse_y= event.pos - x = int(mouse_x / WIDTH * 9) + 1 - y = int(mouse_y / HEIGHT * 7) + 1 + x = int(mouse_x / WIDTH * 9) + y = int(mouse_y / HEIGHT * 7) print(mouse_x,mouse_y) '''if status == 1: if mouse_x >=800: