python 实现简单的吃豆人游戏

 更新时间:2021年04月08日 16:59:17   作者:tinytsunami  
这篇文章主要介绍了python 如何实现简单的吃豆人游戏,帮助大家更好的理解和学习使用python制作游戏,感兴趣的朋友可以了解下

效果展示:

程序简介

1.使用pygame模组
2.在material目录下有一些素材
3.吃豆人的游戏主体
4.吃豆人怪物的AI(未使用深度学习)

主要代码

main.py

import pygame, sys
from pygame.locals import *
from unit import user, enemy
import random

#constant initialize
FPS = 60
BLOCK_SIZE = 24
WIDTH = 29
HEIGHT = 15
WINDOW_WIDTH = WIDTH * BLOCK_SIZE
WINDOW_HEIGHT = HEIGHT * BLOCK_SIZE
MAP_NAME = "./material/map.maze"
BGM_NAME = "./material/bgm.ogg"
BLOCK_IMAGE = "./material/block.png"
FOOD_IMAGE = "./material/food.png"
GAMEOVER_IMAGE = "./material/gameover.png"
SERVER_PORT = 30000
ENEMY_COUNT = 4
OX = 1
OY = 1
DELAY = 8

#pygame initialize
pygame.init()
display = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
clock = pygame.time.Clock()
block_image = pygame.image.load(BLOCK_IMAGE)
food_image = pygame.image.load(FOOD_IMAGE)
gameover_image = pygame.image.load(GAMEOVER_IMAGE)
bgm = pygame.mixer.music.load(BGM_NAME)
scene = "game"
unit_list = []
game_map = []

#map initialize
def load_map(filename):
	global game_map
	game_map.clear()
	file = open(filename, 'r')
	for line in file.readlines():
		game_map.append(list(line.strip()))
		pass
	pass

#set passport
def through(position):
	x = position[0]
	y = position[1]
	in_range = (x >= 0 and x < WIDTH) and (y >= 0 and y < HEIGHT)
	in_space = (not game_map[y][x] == '1')
	return (in_range and in_space)
	pass

#gameover?
def check_gameover(user_pos, enemy_pos):
	global scene
	gameover = (enemy_pos[0] == user_pos[0] and enemy_pos[1] == user_pos[1])
	if gameover:
		scene = "gameover"
		pass
	return gameover
	pass

#gameover
def gameover():
	pygame.mixer.music.stop()
	keys = pygame.key.get_pressed()
	if keys[K_RETURN]:
		initialize()
		pass
	display.fill((0, 0, 0))
	x = (WINDOW_WIDTH-gameover_image.get_width())/2
	y = (WINDOW_HEIGHT-gameover_image.get_height())/2
	display.blit(gameover_image, (x, y))
	pygame.display.update()
	pass

#unit initialize
def initialize_unit():
	unit_list.clear()
	ox = random.randint(1, WIDTH - 2)
	oy = random.randint(1, HEIGHT - 2)
	while not through((ox, oy)):
		ox = random.randint(1, WIDTH - 2)
		oy = random.randint(1, HEIGHT - 2)
	unit_list.append(user(OX, OY))
	for i in range(0, ENEMY_COUNT):
		enemy_color = i % 4
		ox = random.randint(1, WIDTH - 2)
		oy = random.randint(1, HEIGHT - 2)
		while not through((ox, oy)):
			ox = random.randint(1, WIDTH - 2)
			oy = random.randint(1, HEIGHT - 2)
		unit_list.append(enemy(enemy_color, ox, oy))
		pass
	pass

#initialize
def initialize():
	global scene
	load_map(MAP_NAME)
	initialize_unit()
	scene = "game"
	pygame.mixer.music.play(-1)

#system update
def system_update():
	clock.tick(FPS)
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			sys.exit()
	pass

#update control
control_clock = [0, DELAY]
def control_update():
	#user control
	if control_clock[0] > control_clock[1]:
		user = unit_list[0]
		keys = pygame.key.get_pressed()
		passport = False
		pos = user.position
		if keys[K_UP]: 
			pos = user.move(through(user.next(0)))
		elif keys[K_RIGHT]: 
			pos = user.move(through(user.next(1)))
		elif keys[K_DOWN]:
			pos = user.move(through(user.next(2)))
		elif keys[K_LEFT]:
			pos = user.move(through(user.next(3)))
			pass
		game_map[pos[1]][pos[0]] = '0'
		#enemy control
		u_pos = unit_list[0].position
		for index in range(1, len(unit_list)):
			enemy = unit_list[index]
			if check_gameover(u_pos, enemy.position): break
			enemy.track(u_pos)
			passport = through(enemy.next())
			enemy.move(passport)
			while not passport:
				enemy.clockwise()
				passport = through(enemy.next())
				enemy.move(passport)
			pass
		control_clock[0] = 0
		pass
	else:
		control_clock[0] += 1
		pass
	pass

#update screen
def screen_update():
	display.fill((0, 0, 0))
	for i in range(0, HEIGHT):
		for j in range(0, WIDTH):
			x = j * BLOCK_SIZE
			y = i * BLOCK_SIZE
			if game_map[i][j] == '1':
				display.blit(block_image, (x, y))
			elif game_map[i][j] == '4':
				display.blit(food_image, (x, y))
				pass
			pass
		pass
	for unit in unit_list:
		unit.update()
		x = unit.position[0] * BLOCK_SIZE
		y = unit.position[1] * BLOCK_SIZE
		display.blit(unit.image, (x, y), unit.image_rect())
	pygame.display.update()
	pass

#first
initialize()

#main loop
while True:
	system_update()
	if scene == "game":
		control_update()
		screen_update()
	else:
		gameover()
		pass
	pass

unit.py

import pygame
import math
import random

USER_IMAGE = "./material/user.png"
ENEMY_IMAGE = [("./material/enemy%d.png" % i) for i in range(1, 5)]

class unit():
	def __init__(self, filename):
		super(unit, self).__init__()
		self.image = pygame.image.load(filename)
		self.clock = [0, 5]
		self.direction = 0
		self.position = [1, 1, 1, 1]
		self.index = 0
		self.source_rect = 0
		pass

	def update(self):
		self.animation_update()
		pass

	def animation_update(self):
		self.clock[0] += 1
		if self.clock[0] > self.clock[1]:
			if self.index < 4:
				self.index += 4
			else:
				self.index -= 4
			self.source_rect = self.image_rect()
			self.clock[0] = 0
			pass
		pass

	def move(self, passport):
		if passport:
			pos = self.position[:]
			self.position[0] = self.position[2]
			self.position[1] = self.position[3]
		else:
			self.position[2] = self.position[0]
			self.position[3] = self.position[1]
			pos = self.position
			pass
		return pos
		pass

	def next(self):
		self.ahead()
		return (self.position[2], self.position[3])
		pass

	def turn(self, direction):
		self.direction = direction % 4
		self.index = self.direction
		pass

	def ahead(self):
		if self.direction == 0:
			self.position[3] -= 1
		elif self.direction == 1:
			self.position[2] += 1
		elif self.direction == 2:
			self.position[3] += 1
		elif self.direction  == 3:
			self.position[2] -= 1
		pass

	def image_rect(self):
		w = self.image.get_width()
		h = self.image.get_height()
		ox = math.floor(w / 4 * (self.index % 4)) 
		oy = math.floor(h / 2 * math.floor(self.index / 4))
		return pygame.Rect((ox, oy), (24, 24))

class user(unit):
	def __init__(self, x, y):
		super(user, self).__init__(USER_IMAGE)
		self.position = [x, y, x, y]
		pass

	def next(self, direction):
		self.turn(direction)
		self.ahead()
		return (self.position[2], self.position[3])
		pass

class enemy(unit):
	def __init__(self, id, x, y):
		filename = ENEMY_IMAGE[id]
		super(enemy, self).__init__(filename)
		self.position = [x, y, x, y]
		pass

	def track(self, user_pos):
		rand_dir = [1,2,3,4]
		self.turn(random.choice(rand_dir))
		pass

	def clockwise(self):
		self.turn(self.direction + 1)
		pass

class enemy_user(unit):
	def __init__(self, x, y):
		filename = ENEMY_IMAGE[0]
		super(enemy_user, self).__init__(filename)
		self.position = [x, y, x, y]
		pass

	def move(self, x, y):
		self.position[0] = x
		self.position[1] = y
		pass

总结:

程序还有许多地方可以完善,如怪物的AI,时间的判定等等,有兴趣的大佬可以加以修改完善。

完整项目下载:https://github.com/tinytsunami/Python-Game

以上就是python 实现简单的吃豆人游戏的详细内容,更多关于python 实现吃豆人游戏的资料请关注脚本之家其它相关文章!

相关文章

  • Python图像运算之图像灰度非线性变换详解

    Python图像运算之图像灰度非线性变换详解

    这篇文章将详细讲解图像灰度非线性变换。图像灰度非线性变换主要包括对数变换、幂次变换、指数变换、分段函数变换,通过非线性关系对图像进行灰度处理,本文主要讲解三种常见类型的灰度非线性变换,感兴趣的可以了解一下
    2022-03-03
  • python数据分析之用sklearn预测糖尿病

    python数据分析之用sklearn预测糖尿病

    这篇文章主要介绍了python数据分析之用sklearn预测糖尿病,文中有非常详细的代码示例,对正在学习python数据分析的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-04-04
  • Python小程序之在图片上加入数字的代码

    Python小程序之在图片上加入数字的代码

    这篇文章主要介绍了Python小程序之在图片上加入数字的代码,这个是小编今天练手的小程序,代码简单易懂,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • python操作xls使用xlwings代提openpyxl基础

    python操作xls使用xlwings代提openpyxl基础

    这篇文章主要为大家介绍了python操作xls使用xlwings代提openpyxl示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Python实现图片和视频的相互转换

    Python实现图片和视频的相互转换

    有时候我们需要把很多的图片合成视频,或者说自己写一个脚本去加快或者放慢视频;也有时候需要把视频裁剪成图片,进行后续操作。这篇文章就将为大家介绍如何通过Python实现图片和视频的相互转换,需要的可以参考一下
    2021-12-12
  • Python如何操作office实现自动化及win32com.client的运用

    Python如何操作office实现自动化及win32com.client的运用

    这篇文章主要介绍了Python如何操作office实现自动化及win32com.client的运用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Python如何快速实现分布式任务

    Python如何快速实现分布式任务

    这篇文章主要介绍了Python如何快速实现分布式任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • 想学python 这5本书籍你必看!

    想学python 这5本书籍你必看!

    想学python,这5本书籍你必看!本文为大家推荐了学习python的5本书籍,5本经典书籍,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • python调用虹软2.0第三版的具体使用

    python调用虹软2.0第三版的具体使用

    这篇文章主要介绍了python调用虹软2.0第三版的具体使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • Python multiprocessing多进程原理与应用示例

    Python multiprocessing多进程原理与应用示例

    这篇文章主要介绍了Python multiprocessing多进程原理与应用,结合实例形式详细分析了基于multiprocessing包的多进程概念、原理及相关使用操作技巧,需要的朋友可以参考下
    2019-02-02

最新评论