导语
昨晚玩起了小时候玩的游戏“吃豆豆”,但是我发现,一局游戏三条命,我根本不能吃完所有的豆豆,总是被敌人吃掉,
于是,我在想怎么能够保证我达到吃完所有豆豆的目标,然后我就想到了一个办法:
自己找资料找素材学习仿写了一款吃豆豆的小游戏,然后给我自己无限开挂!
哈哈哈!这不?完全解决了我的问题,完美的躺赢了!聪明的我.jpg
正文
一、首先
1)素材
首先找到吃豆豆游戏的界面按照上面的素材找找准备下相似的图片!如下:
2)环境
本文的环境都跟之前的差不多:Python3、Pycharm、Pygame模块。
模块安装:
pip install -i https://pypi.douban.com/simple/ pygame
二、正式开始
这款吃豆豆的小游戏:主要分为4块主要内容,分别是。Levels.py、Sprites.py、cfg.py、Game.py
1)配置文件:cfg.py
import os
'''定义一些颜色'''
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
PURPLE = (255, 0, 255)
SKYBLUE = (0, 191, 255)
'''游戏素材路径'''
BGMPATH = os.path.join(os.getcwd(), 'resources/sounds/bg.mp3')
ICONPATH = os.path.join(os.getcwd(), 'resources/images/icon.png')
FONTPATH = os.path.join(os.getcwd(), 'resources/font/ALGER.TTF')
HEROPATH = os.path.join(os.getcwd(), 'resources/images/pacman.png')
BlinkyPATH = os.path.join(os.getcwd(), 'resources/images/Blinky.png')
ClydePATH = os.path.join(os.getcwd(), 'resources/images/Clyde.png')
InkyPATH = os.path.join(os.getcwd(), 'resources/images/Inky.png')
PinkyPATH = os.path.join(os.getcwd(), 'resources/images/Pinky.png')
2)定义一些精灵类:Sprites.py
import random
import pygame
'''墙类'''
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.left = x
self.rect.top = y
'''食物类'''
class Food(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color, bg_color, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(bg_color)
self.image.set_colorkey(bg_color)
pygame.draw.ellipse(self.image, color, [0, 0, width, height])
self.rect = self.image.get_rect()
self.rect.left = x
self.rect.top = y
'''角色类'''
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, role_image_path):
pygame.sprite.Sprite.__init__(self)
self.role_name = role_image_path.split('/')[-1].split('.')[0]
self.base_image = pygame.image.load(role_image_path).convert()
self.image = self.base_image.copy()
self.rect = self.image.get_rect()
self.rect.left = x
self.rect.top = y
self.prev_x = x
self.prev_y = y
self.base_speed = [30, 30]
self.speed = [0, 0]
self.is_move = False
self.tracks = []
self.tracks_loc = [0, 0]
'''改变速度方向'''
def changeSpeed(self, direction):
if direction[0]