赞
踩
【项目:九宫格拼图】:
效果图展示:
部分代码:
-
- # 初始化
- pygame.init()
- mainClock = pygame.time.Clock()
- # 加载图片
- gameImage = pygame.image.load('test.jpg')
- gameRect = gameImage.get_rect()
- # 设置窗口
- windowSurface = pygame.display.set_mode((gameRect.width, gameRect.height))
- pygame.display.set_caption('拼图')
- cellWidth = int(gameRect.width / VHNUMS)
- cellHeight = int(gameRect.height / VHNUMS)
- finish = False
- gameBoard, blackCell = newGameBoard()
- # 游戏主循环
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- terminate()
- if finish:
- 老师 WX:tz16494
- continue
- if event.type == KEYDOWN:
- if event.key == K_LEFT or event.key == ord('a'):
- blackCell = moveLeft(gameBoard, blackCell)
- if event.key == K_RIGHT or event.key == ord('d'):
- blackCell = moveRight(gameBoard, blackCell)
- if event.key == K_UP or event.key == ord('w'):
- blackCell = moveUp(gameBoard, blackCell)
- if event.key == K_DOWN or event.key == ord('s'):
- blackCell = moveDown(gameBoard, blackCell)
效果图展示:
部分代码:
- # 地雷数量
- MINE_COUNT = 99
-
- # 每个方格的大小(宽、高都为20)
- SIZE = 20
- # 方格的行数
- BLOCK_ROW_NUM = 16
- # 方格的列数
- BLOCK_COL_NUM = 30
- # 游戏窗口的宽、高
- SCREEN_WIDTH, SCREEN_HEIGHT = BLOCK_COL_NUM * SIZE, (BLOCK_ROW_NUM + 2) * SIZE
-
-
- def judge_win(board_list):
- """
- 判断是否获胜
- 只要有1个标记为地雷但实际不是地雷的方格,就表示未获胜,否则获胜
- """
- for line in board_list:
- for num_dict in line:
- if num_dict.get("opened_num") == "雷" and num_dict.get("closed_num") != "雷标记":
- return False
- else:
- return True
-
-
- def set_nums_blank(row, col, board_list):
- """
- 判断当前位置的周边位置是否为空,如果是则继续判断,
- 最终能够实现点击一个空位置后连续的空位置都能够显示出来
- """
- mine_num = get_mine_num(row, col, board_list)
- print("row=%d, col=%d, mine_num=%d" % (row, col, mine_num))
- if mine_num == 0:
- board_list[row][col]['opened'] = True
- board_list[row][col]["opened_num"] = 0
- board_list[row][col]["closed_num"] = "空"
- # 判断对角是否是数字
- for i, j in [(-1, -1), (1, 1), (1, -1), (-1, 1)]:
- if 0 <= row + i <= 15 and 0 <= col + j <= 29:
- mine_num = get_mine_num(row + i, col + j, board_list)
- if mine_num:
- board_list[row + i][col + j]['opened'] = True
- board_list[row + i][col + j]["opened_num"] = mine_num
- board_list[row + i][col + j]["closed_num"] = "空"
-
- # 判断剩下4个位置是否是也是0,即空
- for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
- if 0 <= row + i <= 15 and 0 <= col + j <= 29:
- if not board_list[row + i][col + j].get("opened"):
【项目:贪吃蛇】:
效果图展示:
部分代码:
-
-
- while True:
- for event in pygame.event.get():
- if event.type == locals.QUIT:
- # 接收到退出事件后退出程序
- exit()
- elif event.type == locals.KEYDOWN:
- if event.key == locals.K_RIGHT and setheading != "left":
- setheading = 'right'
- snake_head = right
- elif event.key == locals.K_LEFT and setheading != "right":
- setheading = 'left'
- snake_head = left
- elif event.key == locals.K_UP and setheading != "down":
- setheading = 'up'
- snake_head = up
- elif event.key == locals.K_DOWN and setheading != "up":
- setheading = 'down'
- snake_head = down
-
- # 设置贪吃蛇的头部坐标
- if setheading == "right":
- x += 30 #
- elif setheading == "left":
- x -= 30
- elif setheading == "up":
- y -= 30
- else:
- y += 30
- position.append((x, y))
- if x == apple_x and y == apple_y:
- # 随机生成一个格子的左上角坐标作为苹果(食物)的坐标
- num1 = random.randint(1, 22)
- num2 = random.randint(1, 16)
- apple_x = 30 * num1 - 30 # 苹果的x坐标
- apple_y = 30 * num2 - 30 # 苹果的y坐标
- score += 10
- else:
【项目:推箱子】:
效果图展示:
部分代码:
- '''游戏界面'''
- class gameInterface():
- def __init__(self, screen):
- self.screen = screen
- self.levels_path = Config.get('levels_path')
- self.initGame()
- '''导入关卡地图'''
- def loadLevel(self, game_level):
- with open(os.path.join(self.levels_path, game_level), 'r') as f:
- lines = f.readlines()
- # 游戏地图
- self.game_map = gameMap(max([len(line) for line in lines]) - 1, len(lines))
- # 游戏surface
- height = Config.get('block_size') * self.game_map.num_rows
- width = Config.get('block_size') * self.game_map.num_cols
- self.game_surface = pygame.Surface((width, height))
- self.game_surface.fill(Config.get('bg_color'))
- self.game_surface_blank = self.game_surface.copy()
- for row, elems in enumerate(lines):
- for col, elem in enumerate(elems):
- if elem == 'p':
- self.player = pusherSprite(col, row)
- elif elem == '*':
- self.game_map.addElement('wall', col, row)
- elif elem == '#':
- self.game_map.addElement('box', col, row)
- elif elem == 'o':
- self.game_map.addElement('target', col, row)
- '''游戏初始化'''
- def initGame(self):
- self.scroll_x = 0
- self.scroll_y = 0
- '''将游戏界面画出来'''
- def draw(self, *elems):
- self.scroll()
- self.game_surface.blit(self.game_surface_blank, dest=(0, 0))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。