最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 實(shí)現(xiàn)簡(jiǎn)單的吃豆人游戲

 更新時(shí)間:2021年04月08日 16:59:17   作者:tinytsunami  
這篇文章主要介紹了python 如何實(shí)現(xiàn)簡(jiǎn)單的吃豆人游戲,幫助大家更好的理解和學(xué)習(xí)使用python制作游戲,感興趣的朋友可以了解下

效果展示:

程序簡(jiǎn)介

1.使用pygame模組
2.在material目錄下有一些素材
3.吃豆人的游戲主體
4.吃豆人怪物的AI(未使用深度學(xué)習(xí))

主要代碼

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

總結(jié):

程序還有許多地方可以完善,如怪物的AI,時(shí)間的判定等等,有興趣的大佬可以加以修改完善。

完整項(xiàng)目下載:https://github.com/tinytsunami/Python-Game

以上就是python 實(shí)現(xiàn)簡(jiǎn)單的吃豆人游戲的詳細(xì)內(nèi)容,更多關(guān)于python 實(shí)現(xiàn)吃豆人游戲的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)

    Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)

    這篇文章主要給大家介紹了關(guān)于Python requests庫(kù)參數(shù)提交的注意事項(xiàng),文中通過(guò)示例代碼和圖片介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • pandas高效讀取大文件的示例詳解

    pandas高效讀取大文件的示例詳解

    使用?pandas?進(jìn)行數(shù)據(jù)分析時(shí),第一步就是讀取文件,所以這篇文章主要來(lái)和大家討論一下pandas如何高效讀取大文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2024-01-01
  • Python中循環(huán)引用(import)失敗的解決方法

    Python中循環(huán)引用(import)失敗的解決方法

    在python中常常會(huì)遇到循環(huán)import即circular import的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于Python中循環(huán)引用(import)失敗的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • 詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等)

    詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstac

    這篇文章主要介紹了詳解Numpy中的數(shù)組拼接、合并操作(concatenate, append, stack, hstack, vstack, r_, c_等),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Python中最神秘missing()函數(shù)介紹

    Python中最神秘missing()函數(shù)介紹

    大家好,本篇文章主要講的是Python中最神秘missing()函數(shù)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • 解決python執(zhí)行較大excel文件openpyxl慢問(wèn)題

    解決python執(zhí)行較大excel文件openpyxl慢問(wèn)題

    這篇文章主要介紹了解決python執(zhí)行較大excel文件openpyxl慢問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • 對(duì)python中 math模塊下 atan 和 atan2的區(qū)別詳解

    對(duì)python中 math模塊下 atan 和 atan2的區(qū)別詳解

    今天小編就為大家分享一篇對(duì)python中 math模塊下 atan 和 atan2的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python實(shí)現(xiàn)自動(dòng)化的sql延時(shí)注入

    python實(shí)現(xiàn)自動(dòng)化的sql延時(shí)注入

    這篇文章主要為大家詳細(xì)介紹了如何基于python實(shí)現(xiàn)自動(dòng)化的sql延時(shí)注入腳本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • python編寫暴力破解zip文檔程序的實(shí)例講解

    python編寫暴力破解zip文檔程序的實(shí)例講解

    下面小編就為大家分享一篇python編寫暴力破解zip文檔程序的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • 詳解Django模板層過(guò)濾器和繼承的問(wèn)題

    詳解Django模板層過(guò)濾器和繼承的問(wèn)題

    今天抽空給大家介紹Django模板層過(guò)濾器和繼承的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-07-07

最新評(píng)論

常宁市| 科技| 互助| 临漳县| 浦县| 阿拉善左旗| 吴堡县| 吴桥县| 永年县| 福安市| 威远县| 资阳市| 正安县| 海南省| 白朗县| 蓝田县| 且末县| 若尔盖县| 威宁| 垣曲县| 洛隆县| 凤庆县| 托克逊县| 保德县| 隆子县| 宜阳县| 修水县| 本溪市| 双流县| 崇礼县| 石城县| 崇信县| 定远县| 阿坝县| 高雄市| 库伦旗| 于都县| 古蔺县| 专栏| 邯郸县| 抚宁县|