Python-炸弹人【附完整源码】

炸弹人

炸弹人是童年的一款经典电子游戏,玩家控制一个类似"炸弹人"的角色,这个角色可以放置炸弹,并在指定的时间内引爆它们消灭敌人以达到目标,此游戏共设有两节关卡,代码如下:

运行效果:方向键控制上下左右, 点击空格键丢炸弹
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

一:主程序:

import sys
import cfg
import random
import pygame
from modules import *

'''游戏主程序'''
def main(cfg):
    # 初始化
    pygame.init()
    pygame.mixer.init()
    pygame.mixer.music.load(cfg.BGMPATH)
    pygame.mixer.music.play(-1, 0.0)
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('炸弹人')
    # 开始界面
    Interface(screen, cfg, mode='game_start')
    # 游戏主循环
    font = pygame.font.SysFont('Consolas', 15)
    for gamemap_path in cfg.GAMEMAPPATHS:
        # -地图
        map_parser = mapParser(gamemap_path, bg_paths=cfg.BACKGROUNDPATHS, wall_paths=cfg.WALLPATHS, blocksize=cfg.BLOCKSIZE)
        # -水果
        fruit_sprite_group = pygame.sprite.Group()
        used_spaces = []
        for i in range(5):
            coordinate = map_parser.randomGetSpace(used_spaces)
            used_spaces.append(coordinate)
            fruit_sprite_group.add(Fruit(random.choice(cfg.FRUITPATHS), coordinate=coordinate, blocksize=cfg.BLOCKSIZE))
        # -我方Hero
        coordinate = map_parser.randomGetSpace(used_spaces)
        used_spaces.append(coordinate)
        ourhero = Hero(imagepaths=cfg.HEROZELDAPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='ZELDA')
        # -电脑Hero
        aihero_sprite_group = pygame.sprite.Group()
        coordinate = map_parser.randomGetSpace(used_spaces)
        aihero_sprite_group.add(Hero(imagepaths=cfg.HEROBATMANPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='BATMAN'))
        used_spaces.append(coordinate)
        coordinate = map_parser.randomGetSpace(used_spaces)
        aihero_sprite_group.add(Hero(imagepaths=cfg.HERODKPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name='DK'))
        used_spaces.append(coordinate)
        # -炸弹bomb
        bomb_sprite_group = pygame.sprite.Group()
        # -用于判断游戏胜利或者失败的flag
        is_win_flag = False
        # -主循环
        screen = pygame.display.set_mode(map_parser.screen_size)
        clock = pygame.time.Clock()
        while True:
            dt = clock.tick(cfg.FPS)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit(-1)
                # --↑↓←→键控制上下左右, 空格键丢炸弹
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                        ourhero.move('up')
                    elif event.key == pygame.K_DOWN:
                        ourhero.move('down')
                    elif event.key == pygame.K_LEFT:
                        ourhero.move('left')
                    elif event.key == pygame.K_RIGHT:
                        ourhero.move('right')
                    elif event.key == pygame.K_SPACE:
                        if ourhero.bomb_cooling_count <= 0:
                            bomb_sprite_group.add(ourhero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
            screen.fill(cfg.WHITE)
            # --电脑Hero随机行动
            for hero in aihero_sprite_group:
                action, flag = hero.randomAction(dt)
                if flag and action == 'dropbomb':
                    bomb_sprite_group.add(hero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
            # --吃到水果加生命值(只要是Hero, 都能加)
            ourhero.eatFruit(fruit_sprite_group)
            for hero in aihero_sprite_group:
                hero.eatFruit(fruit_sprite_group)
            # --游戏元素都绑定到屏幕上
            map_parser.draw(screen)
            for bomb in bomb_sprite_group:
                if not bomb.is_being:
                    bomb_sprite_group.remove(bomb)
                explode_area = bomb.draw(screen, dt, map_parser)
                if explode_area:
                    # --爆炸火焰范围内的Hero生命值将持续下降
                    if ourhero.coordinate in explode_area:
                        ourhero.health_value -= bomb.harm_value
                    for hero in aihero_sprite_group:
                        if hero.coordinate in explode_area:
                            hero.health_value -= bomb.harm_value
            fruit_sprite_group.draw(screen)
            for hero in aihero_sprite_group:
                hero.draw(screen, dt)
            ourhero.draw(screen, dt)
            # --左上角显示生命值
            pos_x = showText(screen, font, text=ourhero.hero_name+'(our):'+str(ourhero.health_value), color=cfg.YELLOW, position=[5, 5])
            for hero in aihero_sprite_group:
                pos_x, pos_y = pos_x+15, 5
                pos_x = showText(screen, font, text=hero.hero_name+'(ai):'+str(hero.health_value), color=cfg.YELLOW, position=[pos_x, pos_y])
            # --我方玩家生命值小于等于0/电脑方玩家生命值均小于等于0则判断游戏结束
            if ourhero.health_value <= 0:
                is_win_flag = False
                break
            for hero in aihero_sprite_group:
                if hero.health_value <= 0:
                    aihero_sprite_group.remove(hero)
            if len(aihero_sprite_group) == 0:
                is_win_flag = True
                break
            pygame.display.update()
            clock.tick(cfg.FPS)
        if is_win_flag:
            Interface(screen, cfg, mode='game_switch')
        else:
            break
    Interface(screen, cfg, mode='game_end')


'''run'''
if __name__ == '__main__':
    while True:
        main(cfg)

二:配置文件 – cfg.py

import os

'''屏幕大小'''
SCREENSIZE = (640, 480)
'''块大小'''
BLOCKSIZE = 30
'''FPS'''
FPS = 30
'''游戏地图路径'''
GAMEMAPPATHS = [os.path.join(os.getcwd(), path) for path in \
    ['resources/maps/1.map', 'resources/maps/2.map']]
'''墙路径'''
WALLPATHS = [os.path.join(os.getcwd(), path) for path in \
    ['resources/images/misc/wall0.png', 'resources/images/misc/wall1.png', 'resources/images/misc/wall2.png']]
'''英雄路径'''
HERODKPATHS = [os.path.join(os.getcwd(), path) for path in \
    ['resources/images/dk/left.png', 'resources/images/dk/right.png', 'resources/images/dk/up.png', 'resources/images/dk/down.png']]
HEROZELDAPATHS = [os.path.join(os.getcwd(), path) for path in \
    ['resources/images/zelda/left.png', 'resources/images/zelda/right.png', 'resources/images/zelda/up.png', 'resources/images/zelda/down.png']]
HEROBATMANPATHS = [os.path.join(os.getcwd(), path) for path in \
    ['resources/images/batman/left.png', 'resources/images/batman/right.png', 'resources/images/batman/up.png', 'resources/images/batman/down.png']]
'''水果路径'''
FRUITPATHS = [os.path.join(os.getcwd(), path) for path in \
    ['resources/images/misc/banana.png', 'resources/images/misc/cherry.png']]
'''背景路径'''
BACKGROUNDPATHS = [os.path.join(os.getcwd(), path) for path in \
    ['resources/images/misc/bg0.png', 'resources/images/misc/bg1.png', 'resources/images/misc/bg2.png']]
'''爆炸和发射路径'''
BOMBPATH = os.path.join(os.getcwd(), 'resources/images/misc/bomb.png')
FIREPATH = os.path.join(os.getcwd(), 'resources/images/misc/fire.png')
'''背景音乐'''
BGMPATH = os.path.join(os.getcwd(), 'resources/audio/bgm.mp3')
'''一些颜色'''
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

三:素材包 – resources

(1)audio文件:

bgm.mps音乐大家自己去找然后设置就好

(2)images文件:

图片太多了,做不到全部放置,大家根据配置文件自己去弄就好,或者私信我,我单独给大家发

(3)maps文件:

#1.map
xxxxxxxxxxxxx
x00000000000x
x0x0x0x0x0x0x
x00000000000x
x0x0x0x0x0x0x
x00000000000x
x0x0x0x0x0x0x
x00000000000x
x0x0x0x0x0x0x
x00000000000x
xxxxxxxxxxxxx
#2.map
2222222222222222222222222
2wwwww2wwwww2wwwww2wwwww2
2222222222222222222222222
2zz2zz2x1x1x1x1x1x2zz2zz2
2zz2zz2111111111112zz2zz2
2222222x1x1x1x1x1x2222222
2wwwww2111111111112wwwww2
2222222x1x1x1x1x1x2222222
2zz2zz2111111111112zz2zz2
2zz2zz2x1x1x1x1x1x2zz2zz2
2222222222222222222222222
2wwwww2wwwww2wwwww2wwwww2
2222222222222222222222222

四:文件包 – modules

(1)__init__.py

'''初始化'''
from .MAP import mapParser
from .misc import showText, Button, Interface
from .Sprites import Wall, Background, Fruit, Bomb, Hero

(2)MAP.py

import random
from .Sprites import *

'''.map文件解析器'''
class mapParser():
	def __init__(self, mapfilepath, bg_paths, wall_paths, blocksize, **kwargs):
		self.instances_list = self.__parse(mapfilepath)
		self.bg_paths = bg_paths
		self.wall_paths = wall_paths
		self.blocksize = blocksize
		self.height = len(self.instances_list)
		self.width = len(self.instances_list[0])
		self.screen_size = (blocksize * self.width, blocksize * self.height)
	'''地图画到屏幕上'''
	def draw(self, screen):
		for j in range(self.height):
			for i in range(self.width):
				instance = self.instances_list[j][i]
				if instance == 'w':
					elem = Wall(self.wall_paths[0], [i, j], self.blocksize)
				elif instance == 'x':
					elem = Wall(self.wall_paths[1], [i, j], self.blocksize)
				elif instance == 'z':
					elem = Wall(self.wall_paths[2], [i, j], self.blocksize)
				elif instance == '0':
					elem = Background(self.bg_paths[0], [i, j], self.blocksize)
				elif instance == '1':
					elem = Background(self.bg_paths[1], [i, j], self.blocksize)
				elif instance == '2':
					elem = Background(self.bg_paths[2], [i, j], self.blocksize)
				else:
					raise ValueError('instance parse error in mapParser.draw...')
				elem.draw(screen)
	'''随机获取一个空地'''
	def randomGetSpace(self, used_spaces=None):
		while True:
			i = random.randint(0, self.width-1)
			j = random.randint(0, self.height-1)
			coordinate = [i, j]
			if used_spaces and coordinate in used_spaces:
				continue
			instance = self.instances_list[j][i]
			if instance in ['0', '1', '2']:
				break
		return coordinate
	'''根据坐标获取元素类型'''
	def getElemByCoordinate(self, coordinate):
		return self.instances_list[coordinate[1]][coordinate[0]]
	'''解析.map文件'''
	def __parse(self, mapfilepath):
		instances_list = []
		with open(mapfilepath) as f:
			for line in f.readlines():
				instances_line_list = []
				for c in line:
					if c in ['w', 'x', 'z', '0', '1', '2']:
						instances_line_list.append(c)
				instances_list.append(instances_line_list)
		return instances_list

(3)misc.py

import sys
import pygame

'''在屏幕指定位置显示文字'''
def showText(screen, font, text, color, position):
	text_render = font.render(text, True, color)
	rect = text_render.get_rect()
	rect.left, rect.top = position
	screen.blit(text_render, rect)
	return rect.right


'''按钮'''
def Button(screen, position, text, buttoncolor=(120, 120, 120), linecolor=(20, 20, 20), textcolor=(255, 255, 255), bwidth=200, bheight=50):
	left, top = position
	pygame.draw.line(screen, linecolor, (left, top), (left+bwidth, top), 5)
	pygame.draw.line(screen, linecolor, (left, top-2), (left, top+bheight), 5)
	pygame.draw.line(screen, linecolor, (left, top+bheight), (left+bwidth, top+bheight), 5)
	pygame.draw.line(screen, linecolor, (left+bwidth, top+bheight), (left+bwidth, top), 5)
	pygame.draw.rect(screen, buttoncolor, (left, top, bwidth, bheight))
	font = pygame.font.SysFont('Consolas', 30)
	text_render = font.render(text, 1, textcolor)
	rect = text_render.get_rect()
	rect.centerx, rect.centery = left + bwidth / 2, top + bheight / 2
	return screen.blit(text_render, rect)

'''游戏开始/关卡切换/游戏结束界面'''
def Interface(screen, cfg, mode='game_start'):
	pygame.display.set_mode(cfg.SCREENSIZE)
	if mode == 'game_start':
		clock = pygame.time.Clock()
		while True:
			screen.fill((41, 36, 33))
			button_1 = Button(screen, (220, 150), 'START')
			button_2 = Button(screen, (220, 250), 'QUIT')
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					pygame.quit()
					sys.exit(-1)
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if button_1.collidepoint(pygame.mouse.get_pos()):
						return True
					elif button_2.collidepoint(pygame.mouse.get_pos()):
						pygame.quit()
						sys.exit(-1)
			pygame.display.update()
			clock.tick(cfg.FPS)
	elif mode == 'game_switch':
		clock = pygame.time.Clock()
		while True:
			screen.fill((41, 36, 33))
			button_1 = Button(screen, (220, 150), 'NEXT')
			button_2 = Button(screen, (220, 250), 'QUIT')
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					pygame.quit()
					sys.exit(-1)
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if button_1.collidepoint(pygame.mouse.get_pos()):
						return True
					elif button_2.collidepoint(pygame.mouse.get_pos()):
						pygame.quit()
						sys.exit(-1)
			pygame.display.update()
			clock.tick(cfg.FPS)
	elif mode == 'game_end':
		clock = pygame.time.Clock()
		while True:
			screen.fill((41, 36, 33))
			button_1 = Button(screen, (220, 150), 'RESTART')
			button_2 = Button(screen, (220, 250), 'QUIT')
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					pygame.quit()
					sys.exit(-1)
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if button_1.collidepoint(pygame.mouse.get_pos()):
						return True
					elif button_2.collidepoint(pygame.mouse.get_pos()):
						pygame.quit()
						sys.exit(-1)
			pygame.display.update()
			clock.tick(cfg.FPS)
	else:
		raise ValueError('Interface.mode unsupport %s...' % mode)

(4)Sprites.py

import copy
import random
import pygame

'''墙类'''
class Wall(pygame.sprite.Sprite):
	def __init__(self, imagepath, coordinate, blocksize, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load(imagepath)
		self.image = pygame.transform.scale(self.image, (blocksize, blocksize))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = coordinate[0] * blocksize, coordinate[1] * blocksize
		self.coordinate = coordinate
		self.blocksize = blocksize
	'''画到屏幕上'''
	def draw(self, screen):
		screen.blit(self.image, self.rect)
		return True


'''背景类'''
class Background(pygame.sprite.Sprite):
	def __init__(self, imagepath, coordinate, blocksize, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load(imagepath)
		self.image = pygame.transform.scale(self.image, (blocksize, blocksize))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = coordinate[0] * blocksize, coordinate[1] * blocksize
		self.coordinate = coordinate
		self.blocksize = blocksize
	'''画到屏幕上'''
	def draw(self, screen):
		screen.blit(self.image, self.rect)
		return True


'''水果类'''
class Fruit(pygame.sprite.Sprite):
	def __init__(self, imagepath, coordinate, blocksize, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.kind = imagepath.split('/')[-1].split('.')[0]
		if self.kind == 'banana':
			self.value = 5
		elif self.kind == 'cherry':
			self.value = 10
		else:
			raise ValueError('Unknow fruit %s...' % self.kind)
		self.image = pygame.image.load(imagepath)
		self.image = pygame.transform.scale(self.image, (blocksize, blocksize))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = coordinate[0] * blocksize, coordinate[1] * blocksize
		self.coordinate = coordinate
		self.blocksize = blocksize
	'''画到屏幕上'''
	def draw(self, screen):
		screen.blit(self.image, self.rect)
		return True


'''炸弹类'''
class Bomb(pygame.sprite.Sprite):
	def __init__(self, imagepath, coordinate, blocksize, digitalcolor, explode_imagepath, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.image.load(imagepath)
		self.image = pygame.transform.scale(self.image, (blocksize, blocksize))
		self.explode_imagepath = explode_imagepath
		self.rect = self.image.get_rect()
		# 像素位置
		self.rect.left, self.rect.top = coordinate[0] * blocksize, coordinate[1] * blocksize
		# 坐标(元素块为单位长度)
		self.coordinate = coordinate
		self.blocksize = blocksize
		# 爆炸倒计时
		self.explode_millisecond = 6000 * 1 - 1
		self.explode_second = int(self.explode_millisecond / 1000)
		self.start_explode = False
		# 爆炸持续时间
		self.exploding_count = 1000 * 1
		# 炸弹伤害能力
		self.harm_value = 1
		# 该炸弹是否还存在
		self.is_being = True
		self.font = pygame.font.SysFont('Consolas', 20)
		self.digitalcolor = digitalcolor
	'''画到屏幕上'''
	def draw(self, screen, dt, map_parser):
		if not self.start_explode:
			# 爆炸倒计时
			self.explode_millisecond -= dt
			self.explode_second = int(self.explode_millisecond / 1000)
			if self.explode_millisecond < 0:
				self.start_explode = True
			screen.blit(self.image, self.rect)
			text = self.font.render(str(self.explode_second), True, self.digitalcolor)
			rect = text.get_rect(center=(self.rect.centerx-5, self.rect.centery+5))
			screen.blit(text, rect)
			return False
		else:
			# 爆炸持续倒计时
			self.exploding_count -= dt
			if self.exploding_count > 0:
				return self.__explode(screen, map_parser)
			else:
				self.is_being = False
				return False
	'''爆炸效果'''
	def __explode(self, screen, map_parser):
		explode_area = self.__calcExplodeArea(map_parser.instances_list)
		for each in explode_area:
			image = pygame.image.load(self.explode_imagepath)
			image = pygame.transform.scale(image, (self.blocksize, self.blocksize))
			rect = image.get_rect()
			rect.left, rect.top = each[0] * self.blocksize, each[1] * self.blocksize
			screen.blit(image, rect)
		return explode_area
	'''计算爆炸区域'''
	def __calcExplodeArea(self, instances_list):
		explode_area = []
		# 区域计算规则为墙可以阻止爆炸扩散, 且爆炸范围仅在游戏地图范围内
		for ymin in range(self.coordinate[1], self.coordinate[1]-5, -1):
			if ymin < 0 or instances_list[ymin][self.coordinate[0]] in ['w', 'x', 'z']:
				break
			explode_area.append([self.coordinate[0], ymin])
		for ymax in range(self.coordinate[1]+1, self.coordinate[1]+5):
			if ymax >= len(instances_list) or instances_list[ymax][self.coordinate[0]] in ['w', 'x', 'z']:
				break
			explode_area.append([self.coordinate[0], ymax])
		for xmin in range(self.coordinate[0], self.coordinate[0]-5, -1):
			if xmin < 0 or instances_list[self.coordinate[1]][xmin] in ['w', 'x', 'z']:
				break
			explode_area.append([xmin, self.coordinate[1]])
		for xmax in range(self.coordinate[0]+1, self.coordinate[0]+5):
			if xmax >= len(instances_list[0]) or instances_list[self.coordinate[1]][xmax] in ['w', 'x', 'z']:
				break
			explode_area.append([xmax, self.coordinate[1]])
		return explode_area


'''角色类'''
class Hero(pygame.sprite.Sprite):
	def __init__(self, imagepaths, coordinate, blocksize, map_parser, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.imagepaths = imagepaths
		self.image = pygame.image.load(imagepaths[-1])
		self.image = pygame.transform.scale(self.image, (blocksize, blocksize))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = coordinate[0] * blocksize, coordinate[1] * blocksize
		self.coordinate = coordinate
		self.blocksize = blocksize
		self.map_parser = map_parser
		self.hero_name = kwargs.get('hero_name')
		# 生命值
		self.health_value = 50
		# 炸弹冷却时间
		self.bomb_cooling_time = 5000
		self.bomb_cooling_count = 0
		# 随机移动冷却时间(仅AI电脑用)
		self.randommove_cooling_time = 100
		self.randommove_cooling_count = 0
	'''角色移动'''
	def move(self, direction):
		self.__updateImage(direction)
		if direction == 'left':
			if self.coordinate[0]-1 < 0 or self.map_parser.getElemByCoordinate([self.coordinate[0]-1, self.coordinate[1]]) in ['w', 'x', 'z']:
				return False
			self.coordinate[0] = self.coordinate[0] - 1
		elif direction == 'right':
			if self.coordinate[0]+1 >= self.map_parser.width or self.map_parser.getElemByCoordinate([self.coordinate[0]+1, self.coordinate[1]]) in ['w', 'x', 'z']:
				return False
			self.coordinate[0] = self.coordinate[0] + 1
		elif direction == 'up':
			if self.coordinate[1]-1 < 0 or self.map_parser.getElemByCoordinate([self.coordinate[0], self.coordinate[1]-1]) in ['w', 'x', 'z']:
				return False
			self.coordinate[1] = self.coordinate[1] - 1
		elif direction == 'down':
			if self.coordinate[1]+1 >= self.map_parser.height or self.map_parser.getElemByCoordinate([self.coordinate[0], self.coordinate[1]+1]) in ['w', 'x', 'z']:
				return False
			self.coordinate[1] = self.coordinate[1] + 1
		else:
			raise ValueError('Unknow direction %s...' % direction)
		self.rect.left, self.rect.top = self.coordinate[0] * self.blocksize, self.coordinate[1] * self.blocksize
		return True
	'''随机行动(AI电脑用)'''
	def randomAction(self, dt):
		# 冷却倒计时
		if self.randommove_cooling_count > 0:
			self.randommove_cooling_count -= dt
		action = random.choice(['left', 'left', 'right', 'right', 'up', 'up', 'down', 'down', 'dropbomb'])
		flag = False
		if action in ['left', 'right', 'up', 'down']:
			if self.randommove_cooling_count <= 0:
				flag = True
				self.move(action)
				self.randommove_cooling_count = self.randommove_cooling_time
		elif action in ['dropbomb']:
			if self.bomb_cooling_count <= 0:
				flag = True
				self.bomb_cooling_count = self.bomb_cooling_time
		return action, flag
	'''生成炸弹'''
	def generateBomb(self, imagepath, digitalcolor, explode_imagepath):
		return Bomb(imagepath=imagepath, coordinate=copy.deepcopy(self.coordinate), blocksize=self.blocksize, digitalcolor=digitalcolor, explode_imagepath=explode_imagepath)
	'''画到屏幕上'''
	def draw(self, screen, dt):
		# 冷却倒计时
		if self.bomb_cooling_count > 0:
			self.bomb_cooling_count -= dt
		screen.blit(self.image, self.rect)
		return True
	'''吃水果'''
	def eatFruit(self, fruit_sprite_group):
		eaten_fruit = pygame.sprite.spritecollide(self, fruit_sprite_group, True, None)
		for fruit in eaten_fruit:
			self.health_value += fruit.value
	'''更新角色朝向'''
	def __updateImage(self, direction):
		directions = ['left', 'right', 'up', 'down']
		idx = directions.index(direction)
		self.image = pygame.image.load(self.imagepaths[idx])
		self.image = pygame.transform.scale(self.image, (self.blocksize, self.blocksize))

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/222380.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

介绍几个有意思的 GitHub 仓库

大家好&#xff0c;我是风筝。 今天介绍几个很有意思的 github 开源项目&#xff0c;看过之后就会发现&#xff0c;github 果然深意暗藏。 GitHub对于程序员来说&#xff0c;再熟悉不过了&#xff0c;绝大多数时候&#xff0c;我们到上面都是为了学习高质量的源代码&#xff…

12 月 10 日,融云在 Google DevFest 上海站等你!

Welcome to DevFest!RongCloud2023 Google DevFest 上海站关注【融云全球互联网通信云】了解更多 时间&#xff1a;2023 年 12 月 10 日&#xff08;周日&#xff09;地点&#xff1a;上海市浦东新区新金桥路 1599 号&#xff0c;东方万国宴会中心 (下沉式广场)主讲&#xff1a…

Unity3D对CSV文件操作(创建、读取、写入、修改)

系列文章目录 Unity工具 文章目录 系列文章目录前言一、Csv是什么&#xff1f;二、创建csv文件2-1、构建表数据2-2、创建表方法2-3、完整的脚本&#xff08;第一种方式&#xff09;2-4、运行结果2-5、完整的脚本&#xff08;第二种方式&#xff09;2-6、运行结果2-7、想用哪种…

【性能测试】业务/吞吐量与存量数据设计关系+压测常见解决方案

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 1、性能测试中业务…

设计模式之GoF23介绍

深入探讨设计模式&#xff1a;构建可维护、可扩展的软件架构 一、设计模式的背景1.1 什么是设计模式1.2 设计模式的历史 二、设计模式的分类2.1 创建型模式2.2 结构型模式2.3 行为型模式 三、七大设计原则四、设计模式关系结论 :rocket: :rocket: :rocket: 在软件开发领域&…

软件工程之系统质量

从公众号转载&#xff0c;关注微信公众号掌握更多技术动态 --------------------------------------------------------------- 一 、质量标准化 1.什么是质量标准化 通过标准化各条业务线的研发流程&#xff0c;以做的比较好的业务线作为标准样板间&#xff0c;规范出一套标…

使用squid配置高匿代理

背景介绍 为什么要设置高匿代理&#xff1f; 在家和开放平台交互的时候&#xff0c;需要设置白名单&#xff0c;否则无法交互。家里的白名单一直变。 服务部署到服务器太麻烦&#xff0c;调试不方便。 于是就想通过代理的方式&#xff0c;让服务器替我发送这次请求&#xf…

使用Java语言进行账户登录和密码输入

一、操作原理 使用Scanner扫描器进行扫描&#xff0c;使用if语句、if-else语句和else进行账户和密码的验证。 二、相关代码 import java.util.Scanner; public class CheckLoginDemo {public static void main(String[] args){try (Scanner scan new Scanner(System.in)) …

方格取数问题

更好的阅读体验 方格取数。 题目&#xff1a;方格取数 设有 NN 的方格图&#xff0c;我们在其中的某些方格中填入正整数&#xff0c;而其它的方格中则放入数字0。如下图所示&#xff1a; 某人从图中的左上角 A 出发&#xff0c;可以向下行走&#xff0c;也可以向右行走&#x…

设备制造行业CRM:提升客户满意度,驱动业务增长

设备制造行业客户需求多样化、服务链路长&#xff0c;企业在关注APS、EMS等工业软件之余还要以客户为中心&#xff0c;做好客户服务。设备制造行业CRM管理系统是企业管理客户关系的利器&#xff0c;设备制造行业CRM的作用有哪些&#xff1f;一文带您看懂。 设备制造行业需要解…

金蝶云星空单据编辑界面,不允许批量填充操作

文章目录 金蝶云星空单据编辑界面&#xff0c;不允许批量填充操作案例演示开发设计测试 金蝶云星空单据编辑界面&#xff0c;不允许批量填充操作 案例演示 售后单&#xff0c;明细信息单据体&#xff0c;物料编码字段禁止批量填充。 开发设计 编写表单插件&#xff0c;在Be…

绝地求生游戏一定要先训练吗?

绝地求生&#xff08;PlayerUnknowns Battlegrounds&#xff0c;简称PUBG&#xff09;作为一款大热的多人在线生存游戏&#xff0c;自上线以来一直备受玩家追捧。对于新手玩家来说&#xff0c;刚接触这款游戏时常常觉得难以上手&#xff0c;需要进行一定的训练才能够在游戏中取…

Java毕业设计—vue+SpringBoot人事管理OA系统前后端分离

1&#xff0c;项目介绍 本系统主要分四个模块&#xff0c;分别是系统管理和权限管理、薪资管理、考勤管理 2&#xff0c;技术框架 前端 Vue、Axios、ElementUI、Vue-Router、Vuex、ECharts后端 Spring Boot、JWT、MyBatis-Plus、MySQL、Hutool 3&#xff0c;开发环境 JAVA…

【刘二大人】pytorch深度学习实践(三):如何实现线性模型的反向传播+代码实现详解(Tensor、backward函数)

目录 参考资料一、反向传播流程1.1 问题1.2 方法1.3 步骤1.4 例题 二、Pytorch中前向传播和反馈的计算2.1 tensor数据类型2.2 定义线性模型并且计算损失2.2.1 torch.tensor.item()2.2.2 代码 2.3 反向传播2.3.1 torch.tensor.backward()2.3.2 tensor.zero_( )2.3.3 代码实现 三…

企业机密无忧!好用的文件加密系统大揭秘,尽在这里!

由于众多企业内部都存储着大量机密数据&#xff0c;以电子文档形式存在&#xff0c;且传播手段多样&#xff0c;文件泄密问题容易发生。员工通过网络泄密重要文件&#xff0c;或黑客入侵窃取机密数据等情况&#xff0c;都可能导致企业业务和声誉受到严重损害。因此&#xff0c;…

C++断言assert

2023年12月6日&#xff0c;周三上午 在C中&#xff0c;assert 是一个宏定义&#xff0c;用于在程序运行期间检查一些条件是否满足。如果条件不满足&#xff0c;则 assert 会终止程序并输出一条错误消息。 assert 宏定义的语法如下&#xff1a; #include <cassert>asser…

【ESP8266】ESP8266集成开发环境对比

当涉及到ESP8266开发环境的选择时&#xff0c;有几个常见的选择可供开发人员使用。在本篇文章中&#xff0c;我们将对比一些目前最流行的ESP8266集成开发环境&#xff08;IDE&#xff09;&#xff0c;以帮助您选择最适合您的需求的开发环境。 总结&#xff1a;Arduino IDE和Pl…

学校图书管理系统的开发

目 录 摘要 1 Abstract. 1 1 引言 2 1.1 图书管理的现状 2 1.2 现有图书管理系统的概述 3 1.3 选题的目的、意义 3 1.4 图书管理系统的可行性分析 4 1.5 系统开发运行环境 4 2 图书管理系统开发相关技术的介绍 5 2.1 Asp.net的介绍 5 2.1.1 Asp.net的优势介绍 5 2.1.2 Asp.net…

codeforces 题目 Fadi and LCM

目录 题目&#xff1a; 题目描述&#xff1a; 思路&#xff1a; AC代码&#xff1a; 题目&#xff1a; 题目描述&#xff1a; 给你一个长整型 X ①你需要找到一对 a 和 b &#xff0c;使得 LCM&#xff08;a&#xff0c;b&#xff09; X ②你需要保证 max(a&#xff…

智能优化算法应用:基于水基湍流算法无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于水基湍流算法无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于水基湍流算法无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.水基湍流算法4.实验参数设定5.算法结果6.参考…