当前位置:   article > 正文

python游戏开发合集各类练手项目及讲解_pytgame windowsurface

pytgame windowsurface

【项目:九宫格拼图】:

效果图展示:

部分代码:

 

  1. # 初始化
  2. pygame.init()
  3. mainClock = pygame.time.Clock()
  4. # 加载图片
  5. gameImage = pygame.image.load('test.jpg')
  6. gameRect = gameImage.get_rect()
  7. # 设置窗口
  8. windowSurface = pygame.display.set_mode((gameRect.width, gameRect.height))
  9. pygame.display.set_caption('拼图')
  10. cellWidth = int(gameRect.width / VHNUMS)
  11. cellHeight = int(gameRect.height / VHNUMS)
  12. finish = False
  13. gameBoard, blackCell = newGameBoard()
  14. # 游戏主循环
  15. while True:
  16. for event in pygame.event.get():
  17. if event.type == QUIT:
  18. terminate()
  19. if finish:
  20. 老师 WX:tz16494
  21. continue
  22. if event.type == KEYDOWN:
  23. if event.key == K_LEFT or event.key == ord('a'):
  24. blackCell = moveLeft(gameBoard, blackCell)
  25. if event.key == K_RIGHT or event.key == ord('d'):
  26. blackCell = moveRight(gameBoard, blackCell)
  27. if event.key == K_UP or event.key == ord('w'):
  28. blackCell = moveUp(gameBoard, blackCell)
  29. if event.key == K_DOWN or event.key == ord('s'):
  30. blackCell = moveDown(gameBoard, blackCell)

效果图展示:

部分代码:

  1. # 地雷数量
  2. MINE_COUNT = 99
  3. # 每个方格的大小(宽、高都为20
  4. SIZE = 20
  5. # 方格的行数
  6. BLOCK_ROW_NUM = 16
  7. # 方格的列数
  8. BLOCK_COL_NUM = 30
  9. # 游戏窗口的宽、高
  10. SCREEN_WIDTH, SCREEN_HEIGHT = BLOCK_COL_NUM * SIZE, (BLOCK_ROW_NUM + 2) * SIZE
  11. def judge_win(board_list):
  12. """
  13. 判断是否获胜
  14. 只要有1个标记为地雷但实际不是地雷的方格,就表示未获胜,否则获胜
  15. """
  16. for line in board_list:
  17. for num_dict in line:
  18. if num_dict.get("opened_num") == "雷" and num_dict.get("closed_num") != "雷标记":
  19. return False
  20. else:
  21. return True
  22. def set_nums_blank(row, col, board_list):
  23. """
  24. 判断当前位置的周边位置是否为空,如果是则继续判断,
  25. 最终能够实现点击一个空位置后连续的空位置都能够显示出来
  26. """
  27. mine_num = get_mine_num(row, col, board_list)
  28. print("row=%d, col=%d, mine_num=%d" % (row, col, mine_num))
  29. if mine_num == 0:
  30. board_list[row][col]['opened'] = True
  31. board_list[row][col]["opened_num"] = 0
  32. board_list[row][col]["closed_num"] = "空"
  33. # 判断对角是否是数字
  34. for i, j in [(-1, -1), (1, 1), (1, -1), (-1, 1)]:
  35. if 0 <= row + i <= 15 and 0 <= col + j <= 29:
  36. mine_num = get_mine_num(row + i, col + j, board_list)
  37. if mine_num:
  38. board_list[row + i][col + j]['opened'] = True
  39. board_list[row + i][col + j]["opened_num"] = mine_num
  40. board_list[row + i][col + j]["closed_num"] = "空"
  41. # 判断剩下4个位置是否是也是0,即空
  42. for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
  43. if 0 <= row + i <= 15 and 0 <= col + j <= 29:
  44. if not board_list[row + i][col + j].get("opened"):

【项目:贪吃蛇】:

效果图展示:

部分代码:

  1. while True:
  2. for event in pygame.event.get():
  3. if event.type == locals.QUIT:
  4. # 接收到退出事件后退出程序
  5. exit()
  6. elif event.type == locals.KEYDOWN:
  7. if event.key == locals.K_RIGHT and setheading != "left":
  8. setheading = 'right'
  9. snake_head = right
  10. elif event.key == locals.K_LEFT and setheading != "right":
  11. setheading = 'left'
  12. snake_head = left
  13. elif event.key == locals.K_UP and setheading != "down":
  14. setheading = 'up'
  15. snake_head = up
  16. elif event.key == locals.K_DOWN and setheading != "up":
  17. setheading = 'down'
  18. snake_head = down
  19. # 设置贪吃蛇的头部坐标
  20. if setheading == "right":
  21. x += 30 #
  22. elif setheading == "left":
  23. x -= 30
  24. elif setheading == "up":
  25. y -= 30
  26. else:
  27. y += 30
  28. position.append((x, y))
  29. if x == apple_x and y == apple_y:
  30. # 随机生成一个格子的左上角坐标作为苹果(食物)的坐标
  31. num1 = random.randint(1, 22)
  32. num2 = random.randint(1, 16)
  33. apple_x = 30 * num1 - 30 # 苹果的x坐标
  34. apple_y = 30 * num2 - 30 # 苹果的y坐标
  35. score += 10
  36. else:

 

【项目:推箱子】:

效果图展示:

部分代码:

 

  1. '''游戏界面'''
  2. class gameInterface():
  3. def __init__(self, screen):
  4. self.screen = screen
  5. self.levels_path = Config.get('levels_path')
  6. self.initGame()
  7. '''导入关卡地图'''
  8. def loadLevel(self, game_level):
  9. with open(os.path.join(self.levels_path, game_level), 'r') as f:
  10. lines = f.readlines()
  11. # 游戏地图
  12. self.game_map = gameMap(max([len(line) for line in lines]) - 1, len(lines))
  13. # 游戏surface
  14. height = Config.get('block_size') * self.game_map.num_rows
  15. width = Config.get('block_size') * self.game_map.num_cols
  16. self.game_surface = pygame.Surface((width, height))
  17. self.game_surface.fill(Config.get('bg_color'))
  18. self.game_surface_blank = self.game_surface.copy()
  19. for row, elems in enumerate(lines):
  20. for col, elem in enumerate(elems):
  21. if elem == 'p':
  22. self.player = pusherSprite(col, row)
  23. elif elem == '*':
  24. self.game_map.addElement('wall', col, row)
  25. elif elem == '#':
  26. self.game_map.addElement('box', col, row)
  27. elif elem == 'o':
  28. self.game_map.addElement('target', col, row)
  29. '''游戏初始化'''
  30. def initGame(self):
  31. self.scroll_x = 0
  32. self.scroll_y = 0
  33. '''将游戏界面画出来'''
  34. def draw(self, *elems):
  35. self.scroll()
  36. self.game_surface.blit(self.game_surface_blank, dest=(0, 0))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/111630
推荐阅读
相关标签
  

闽ICP备14008679号