導(dǎo)語
滴一一學(xué)生卡??
結(jié)伴上車的學(xué)生仔子們
用笑聲打破車廂的沉默
大人眼里的晚高峰
是給放學(xué)后快樂??時光的加時
下車的學(xué)生匆匆起身帶起
一陣熟悉的梔子香于??
是關(guān)于校園的記憶
開始零零散散地閃現(xiàn)
放學(xué)后集合的秘密基地/跟著城市變了模樣
超級馬里奧里的隱藏地圖??(重點)
吃豆人里的閃躲奧秘???(重點)
卻刻進(jìn)了DNA里一曾經(jīng)珍藏的交換日記
不記得被放在了哪里
你現(xiàn)在記得的是/相互鼓勵的感動
還是小紙條里寫的八卦?
從肩上的書包到手里的電腦包
時間在改變生活的軌跡??
少年帶著率真與堅定/甩開不諳世事
同在路上的朋友啊
愿你迎風(fēng)像梔子花一樣/??繼續(xù)燦爛地開!
所有文章完整的素材+源碼都在????
粉絲白嫖源碼福利,請移步至CSDN社區(qū)或文末公眾hao即可免費。
當(dāng)我寫完前言的時候,可能在長沙的小伙伴兒就知道這是哪兒的宣傳語,偷偷給你們提醒,是
一家好喝的奶茶店哦~
今天去喝奶茶的時候,突然看到了這個宣傳語我就拍下來啦,然后當(dāng)做今天的內(nèi)容素材,嘿嘿
今天小編教大家兩款上面提到過的小游戲,直接用Python編程的語言來詮釋它們——
1、超級馬里奧里的隱藏地圖(重點)? 2、吃豆人里的閃躲奧秘 (重點)。
正文
小霸王,那是屬于一代人的記憶,70、80、甚至90后都對這個“鍵盤”有著不一樣的感情。
今今天小編教大家用代碼編寫兩款小游戲給大家,大家喜歡的話記得三連哦~
一、環(huán)境準(zhǔn)備中?
?1)運行環(huán)境
環(huán)境安裝:python 3.8: 解釋器、pycharm: 代碼編輯器。需要安裝的模塊如下
2)的模塊安裝方式安裝即可。自帶的一些模塊 直接安裝Python就可以使用了。?
?相對應(yīng)的安裝包/安裝教程/激活碼/使用教程/學(xué) 習(xí)資料/工具插件 可以直接找我厚臺獲取 。?
?2)模塊安裝?
?第三方庫的安裝方式如下:?
?一般安裝:pip install +模塊名?
?鏡像源安裝:pip install -i https://pypi.douban.com/simple/+模塊名
(還有很多國內(nèi)鏡像源,這里是豆瓣的用習(xí)慣 了,其他鏡像源可以去看下之前文章都有的)?
?模塊安裝問題可以詳細(xì)的找我給大家講一下的哈,之前其實也有的文章寫了幾個點的。?
二、超級馬里奧里的隱藏地圖
“超級瑪麗”有多少人還記得這款經(jīng)典游戲?對于90、00后應(yīng)該不大熟悉,但多多少少印象中見
過那個戴帽子的大胡子穿著背帶褲的馬里奧??!
如果你的童年也曾被魔性的?燈~燈燈~燈~燈燈~燈洗腦那就接著來懷舊一番吧~
1)運行程序:mario_level_1.py。
#!/usr/bin/env python
__author__ = '超級瑪麗-源碼基地'
"""
This is an attempt to recreate the first level of
Super Mario Bros for the NES.
"""
import sys
import pygame as pg
from data.main import main
import cProfile
if __name__=='__main__':
main()
pg.quit()
sys.exit()
2) 配置音樂文字等setup.py。??
__author__ = 'Python源碼基地'
"""
This module initializes the display and creates dictionaries of resources.
"""
import os
import pygame as pg
from . import tools
from .import constants as c
ORIGINAL_CAPTION = c.ORIGINAL_CAPTION
os.environ['SDL_VIDEO_CENTERED'] = '1'
pg.init()
pg.event.set_allowed([pg.KEYDOWN, pg.KEYUP, pg.QUIT])
pg.display.set_caption(c.ORIGINAL_CAPTION)
SCREEN = pg.display.set_mode(c.SCREEN_SIZE)
SCREEN_RECT = SCREEN.get_rect()
FONTS = tools.load_all_fonts(os.path.join("resources","fonts"))
MUSIC = tools.load_all_music(os.path.join("resources","music"))
GFX = tools.load_all_gfx(os.path.join("resources","graphics"))
SFX = tools.load_all_sfx(os.path.join("resources","sound"))
3)游戲音樂設(shè)置game_sound.py。??
__author__ = 'Python顧木子吖'
import pygame as pg
from . import setup
from . import constants as c
class Sound(object):
"""Handles all sound for the game"""
def __init__(self, overhead_info):
"""Initialize the class"""
self.sfx_dict = setup.SFX
self.music_dict = setup.MUSIC
self.overhead_info = overhead_info
self.game_info = overhead_info.game_info
self.set_music_mixer()
def set_music_mixer(self):
"""Sets music for level"""
if self.overhead_info.state == c.LEVEL:
pg.mixer.music.load(self.music_dict['main_theme'])
pg.mixer.music.play()
self.state = c.NORMAL
elif self.overhead_info.state == c.GAME_OVER:
pg.mixer.music.load(self.music_dict['game_over'])
pg.mixer.music.play()
self.state = c.GAME_OVER
def update(self, game_info, mario):
"""Updates sound object with game info"""
self.game_info = game_info
self.mario = mario
self.handle_state()
def handle_state(self):
"""Handles the state of the soundn object"""
if self.state == c.NORMAL:
if self.mario.dead:
self.play_music('death', c.MARIO_DEAD)
elif self.mario.invincible \
and self.mario.losing_invincibility == False:
self.play_music('invincible', c.MARIO_INVINCIBLE)
elif self.mario.state == c.FLAGPOLE:
self.play_music('flagpole', c.FLAGPOLE)
elif self.overhead_info.time == 100:
self.play_music('out_of_time', c.TIME_WARNING)
elif self.state == c.FLAGPOLE:
if self.mario.state == c.WALKING_TO_CASTLE:
self.play_music('stage_clear', c.STAGE_CLEAR)
elif self.state == c.STAGE_CLEAR:
if self.mario.in_castle:
self.sfx_dict['count_down'].play()
self.state = c.FAST_COUNT_DOWN
elif self.state == c.FAST_COUNT_DOWN:
if self.overhead_info.time == 0:
self.sfx_dict['count_down'].stop()
self.state = c.WORLD_CLEAR
elif self.state == c. TIME_WARNING:
if pg.mixer.music.get_busy() == 0:
self.play_music('main_theme_sped_up', c.SPED_UP_NORMAL)
elif self.mario.dead:
self.play_music('death', c.MARIO_DEAD)
elif self.state == c.SPED_UP_NORMAL:
if self.mario.dead:
self.play_music('death', c.MARIO_DEAD)
elif self.mario.state == c.FLAGPOLE:
self.play_music('flagpole', c.FLAGPOLE)
elif self.state == c.MARIO_INVINCIBLE:
if (self.mario.current_time - self.mario.invincible_start_timer) > 11000:
self.play_music('main_theme', c.NORMAL)
elif self.mario.dead:
self.play_music('death', c.MARIO_DEAD)
elif self.state == c.WORLD_CLEAR:
pass
elif self.state == c.MARIO_DEAD:
pass
elif self.state == c.GAME_OVER:
pass
def play_music(self, key, state):
"""Plays new music"""
pg.mixer.music.load(self.music_dict[key])
pg.mixer.music.play()
self.state = state
def stop_music(self):
"""Stops playback"""
pg.mixer.music.stop()
4)取得的分?jǐn)?shù)??。
import pygame as pg
from .. import setup
from .. import constants as c
class Digit(pg.sprite.Sprite):
"""Individual digit for score"""
def __init__(self, image):
super(Digit, self).__init__()
self.image = image
self.rect = image.get_rect()
class Score(object):
"""Scores that appear, float up, and disappear"""
def __init__(self, x, y, score, flag_pole=False):
self.x = x
self.y = y
if flag_pole:
self.y_vel = -4
else:
self.y_vel = -3
self.sprite_sheet = setup.GFX['item_objects']
self.create_image_dict()
self.score_string = str(score)
self.create_digit_list()
self.flag_pole_score = flag_pole
def create_image_dict(self):
"""Creates the dictionary for all the number 圖片 needed"""
self.image_dict = {}
image0 = self.get_image(1, 168, 3, 8)
image1 = self.get_image(5, 168, 3, 8)
image2 = self.get_image(8, 168, 4, 8)
image4 = self.get_image(12, 168, 4, 8)
image5 = self.get_image(16, 168, 5, 8)
image8 = self.get_image(20, 168, 4, 8)
image9 = self.get_image(32, 168, 5, 8)
image10 = self.get_image(37, 168, 6, 8)
image11 = self.get_image(43, 168, 5, 8)
self.image_dict['0'] = image0
self.image_dict['1'] = image1
self.image_dict['2'] = image2
self.image_dict['4'] = image4
self.image_dict['5'] = image5
self.image_dict['8'] = image8
self.image_dict['3'] = image9
self.image_dict['7'] = image10
self.image_dict['9'] = image11
def get_image(self, x, y, width, height):
"""Extracts image from sprite sheet"""
image = pg.Surface([width, height]).convert()
rect = image.get_rect()
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
image.set_colorkey(c.BLACK)
image = pg.transform.scale(image,
(int(rect.width*c.BRICK_SIZE_MULTIPLIER),
int(rect.height*c.BRICK_SIZE_MULTIPLIER)))
return image
def create_digit_list(self):
"""Creates the group of 圖片 based on score received"""
self.digit_list = []
self.digit_group = pg.sprite.Group()
for digit in self.score_string:
self.digit_list.append(Digit(self.image_dict[digit]))
self.set_rects_for_images()
def set_rects_for_images(self):
"""Set the rect attributes for each image in self.image_list"""
for i, digit in enumerate(self.digit_list):
digit.rect = digit.image.get_rect()
digit.rect.x = self.x + (i * 10)
digit.rect.y = self.y
def update(self, score_list, level_info):
"""Updates score movement"""
for number in self.digit_list:
number.rect.y += self.y_vel
if score_list:
self.check_to_delete_floating_scores(score_list, level_info)
if self.flag_pole_score:
if self.digit_list[0].rect.y <= 120:
self.y_vel = 0
def draw(self, screen):
"""Draws score numbers onto screen"""
for digit in self.digit_list:
screen.blit(digit.image, digit.rect)
def check_to_delete_floating_scores(self, score_list, level_info):
"""Check if scores need to be deleted"""
for i, score in enumerate(score_list):
if int(score.score_string) == 1000:
if (score.y - score.digit_list[0].rect.y) > 130:
score_list.pop(i)
else:
if (score.y - score.digit_list[0].rect.y) > 75:
score_list.pop(i)
5)效果展示
游戲界面——
游戲運行——
?三、吃豆人里的閃躲奧秘
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] < 0:
self.image = pygame.transform.flip(self.base_image, True, False)
elif direction[0] > 0:
self.image = self.base_image.copy()
elif direction[1] < 0:
self.image = pygame.transform.rotate(self.base_image, 90)
elif direction[1] > 0:
self.image = pygame.transform.rotate(self.base_image, -90)
self.speed = [direction[0] * self.base_speed[0], direction[1] * self.base_speed[1]]
return self.speed
'''更新角色位置'''
def update(self, wall_sprites, gate_sprites):
if not self.is_move:
return False
x_prev = self.rect.left
y_prev = self.rect.top
self.rect.left += self.speed[0]
self.rect.top += self.speed[1]
is_collide = pygame.sprite.spritecollide(self, wall_sprites, False)
if gate_sprites is not None:
if not is_collide:
is_collide = pygame.sprite.spritecollide(self, gate_sprites, False)
if is_collide:
self.rect.left = x_prev
self.rect.top = y_prev
return False
return True
'''生成隨機的方向'''
def randomDirection(self):
return random.choice([[-0.5, 0], [0.5, 0], [0, 0.5], [0, -0.5]])
3)定義關(guān)卡:Levels.py
import pygame
from .Sprites import *
'''關(guān)卡數(shù)量'''
NUMLEVELS = 1
'''關(guān)卡一'''
class Level1():
def __init__(self):
self.info = 'level1'
'''創(chuàng)建墻'''
def setupWalls(self, wall_color):
self.wall_sprites = pygame.sprite.Group()
wall_positions = [
[0, 0, 6, 600], [0, 0, 600, 6], [0, 600, 606, 6], [600, 0, 6, 606], [300, 0, 6, 66], [60, 60, 186, 6],
[360, 60, 186, 6], [60, 120, 66, 6], [60, 120, 6, 126], [180, 120, 246, 6], [300, 120, 6, 66],
[480, 120, 66, 6], [540, 120, 6, 126], [120, 180, 126, 6], [120, 180, 6, 126], [360, 180, 126, 6],
[480, 180, 6, 126], [180, 240, 6, 126], [180, 360, 246, 6], [420, 240, 6, 126], [240, 240, 42, 6],
[324, 240, 42, 6], [240, 240, 6, 66], [240, 300, 126, 6], [360, 240, 6, 66], [0, 300, 66, 6],
[540, 300, 66, 6], [60, 360, 66, 6], [60, 360, 6, 186], [480, 360, 66, 6], [540, 360, 6, 186],
[120, 420, 366, 6], [120, 420, 6, 66], [480, 420, 6, 66], [180, 480, 246, 6], [300, 480, 6, 66],
[120, 540, 126, 6], [360, 540, 126, 6]
]
for wall_position in wall_positions:
wall = Wall(*wall_position, wall_color)
self.wall_sprites.add(wall)
return self.wall_sprites
'''創(chuàng)建門'''
def setupGate(self, gate_color):
self.gate_sprites = pygame.sprite.Group()
self.gate_sprites.add(Wall(282, 242, 42, 2, gate_color))
return self.gate_sprites
'''創(chuàng)建角色'''
def setupPlayers(self, hero_image_path, ghost_images_path):
self.hero_sprites = pygame.sprite.Group()
self.ghost_sprites = pygame.sprite.Group()
self.hero_sprites.add(Player(287, 439, hero_image_path))
for each in ghost_images_path:
role_name = each.split('/')[-1].split('.')[0]
if role_name == 'Blinky':
player = Player(287, 199, each)
player.is_move = True
player.tracks = [
[0, -0.5, 4], [0.5, 0, 9], [0, 0.5, 11], [0.5, 0, 3], [0, 0.5, 7], [-0.5, 0, 11], [0, 0.5, 3],
[0.5, 0, 15], [0, -0.5, 15], [0.5, 0, 3], [0, -0.5, 11], [-0.5, 0, 3], [0, -0.5, 11], [-0.5, 0, 3],
[0, -0.5, 3], [-0.5, 0, 7], [0, -0.5, 3], [0.5, 0, 15], [0, 0.5, 15], [-0.5, 0, 3], [0, 0.5, 3],
[-0.5, 0, 3], [0, -0.5, 7], [-0.5, 0, 3], [0, 0.5, 7], [-0.5, 0, 11], [0, -0.5, 7], [0.5, 0, 5]
]
self.ghost_sprites.add(player)
elif role_name == 'Clyde':
player = Player(319, 259, each)
player.is_move = True
player.tracks = [
[-1, 0, 2], [0, -0.5, 4], [0.5, 0, 5], [0, 0.5, 7], [-0.5, 0, 11], [0, -0.5, 7],
[-0.5, 0, 3], [0, 0.5, 7], [-0.5, 0, 7], [0, 0.5, 15], [0.5, 0, 15], [0, -0.5, 3],
[-0.5, 0, 11], [0, -0.5, 7], [0.5, 0, 3], [0, -0.5, 11], [0.5, 0, 9]
]
self.ghost_sprites.add(player)
elif role_name == 'Inky':
player = Player(255, 259, each)
player.is_move = True
player.tracks = [
[1, 0, 2], [0, -0.5, 4], [0.5, 0, 10], [0, 0.5, 7], [0.5, 0, 3], [0, -0.5, 3],
[0.5, 0, 3], [0, -0.5, 15], [-0.5, 0, 15], [0, 0.5, 3], [0.5, 0, 15], [0, 0.5, 11],
[-0.5, 0, 3], [0, -0.5, 7], [-0.5, 0, 11], [0, 0.5, 3], [-0.5, 0, 11], [0, 0.5, 7],
[-0.5, 0, 3], [0, -0.5, 3], [-0.5, 0, 3], [0, -0.5, 15], [0.5, 0, 15], [0, 0.5, 3],
[-0.5, 0, 15], [0, 0.5, 11], [0.5, 0, 3], [0, -0.5, 11], [0.5, 0, 11], [0, 0.5, 3], [0.5, 0, 1]
]
self.ghost_sprites.add(player)
elif role_name == 'Pinky':
player = Player(287, 259, each)
player.is_move = True
player.tracks = [
[0, -1, 4], [0.5, 0, 9], [0, 0.5, 11], [-0.5, 0, 23], [0, 0.5, 7], [0.5, 0, 3],
[0, -0.5, 3], [0.5, 0, 19], [0, 0.5, 3], [0.5, 0, 3], [0, 0.5, 3], [0.5, 0, 3],
[0, -0.5, 15], [-0.5, 0, 7], [0, 0.5, 3], [-0.5, 0, 19], [0, -0.5, 11], [0.5, 0, 9]
]
self.ghost_sprites.add(player)
return self.hero_sprites, self.ghost_sprites
'''創(chuàng)建食物'''
def setupFood(self, food_color, bg_color):
self.food_sprites = pygame.sprite.Group()
for row in range(19):
for col in range(19):
if (row == 7 or row == 8) and (col == 8 or col == 9 or col == 10):
continue
else:
food = Food(30 * col + 32, 30 * row + 32, 4, 4, food_color, bg_color)
is_collide = pygame.sprite.spritecollide(food, self.wall_sprites, False)
if is_collide:
continue
is_collide = pygame.sprite.spritecollide(food, self.hero_sprites, False)
if is_collide:
continue
self.food_sprites.add(food)
return self.food_sprites
4)主程序Game.py
import sys
import cfg
import pygame
import modules.Levels as Levels
'''開始某一關(guān)游戲'''
def startLevelGame(level, screen, font):
clock = pygame.time.Clock()
SCORE = 0
wall_sprites = level.setupWalls(cfg.SKYBLUE)
gate_sprites = level.setupGate(cfg.WHITE)
hero_sprites, ghost_sprites = level.setupPlayers(cfg.HEROPATH, [cfg.BlinkyPATH, cfg.ClydePATH, cfg.InkyPATH, cfg.PinkyPATH])
food_sprites = level.setupFood(cfg.YELLOW, cfg.WHITE)
is_clearance = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(-1)
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
for hero in hero_sprites:
hero.changeSpeed([-1, 0])
hero.is_move = True
elif event.key == pygame.K_RIGHT:
for hero in hero_sprites:
hero.changeSpeed([1, 0])
hero.is_move = True
elif event.key == pygame.K_UP:
for hero in hero_sprites:
hero.changeSpeed([0, -1])
hero.is_move = True
elif event.key == pygame.K_DOWN:
for hero in hero_sprites:
hero.changeSpeed([0, 1])
hero.is_move = True
if event.type == pygame.KEYUP:
if (event.key == pygame.K_LEFT) or (event.key == pygame.K_RIGHT) or (event.key == pygame.K_UP) or (event.key == pygame.K_DOWN):
hero.is_move = False
screen.fill(cfg.BLACK)
for hero in hero_sprites:
hero.update(wall_sprites, gate_sprites)
hero_sprites.draw(screen)
for hero in hero_sprites:
food_eaten = pygame.sprite.spritecollide(hero, food_sprites, True)
SCORE += len(food_eaten)
wall_sprites.draw(screen)
gate_sprites.draw(screen)
food_sprites.draw(screen)
for ghost in ghost_sprites:
# 幽靈隨機運動(效果不好且有BUG)
'''
res = ghost.update(wall_sprites, None)
while not res:
ghost.changeSpeed(ghost.randomDirection())
res = ghost.update(wall_sprites, None)
'''
# 指定幽靈運動路徑
if ghost.tracks_loc[1] < ghost.tracks[ghost.tracks_loc[0]][2]:
ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0: 2])
ghost.tracks_loc[1] += 1
else:
if ghost.tracks_loc[0] < len(ghost.tracks) - 1:
ghost.tracks_loc[0] += 1
elif ghost.role_name == 'Clyde':
ghost.tracks_loc[0] = 2
else:
ghost.tracks_loc[0] = 0
ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0: 2])
ghost.tracks_loc[1] = 0
if ghost.tracks_loc[1] < ghost.tracks[ghost.tracks_loc[0]][2]:
ghost.changeSpeed(ghost.tracks[ghost.tracks_loc[0]][0: 2])
else:
if ghost.tracks_loc[0] < len(ghost.tracks) - 1:
loc0 = ghost.tracks_loc[0] + 1
elif ghost.role_name == 'Clyde':
loc0 = 2
else:
loc0 = 0
ghost.changeSpeed(ghost.tracks[loc0][0: 2])
ghost.update(wall_sprites, None)
ghost_sprites.draw(screen)
score_text = font.render("Score: %s" % SCORE, True, cfg.RED)
screen.blit(score_text, [10, 10])
if len(food_sprites) == 0:
is_clearance = True
break
if pygame.sprite.groupcollide(hero_sprites, ghost_sprites, False, False):
is_clearance = False
break
pygame.display.flip()
clock.tick(10)
return is_clearance
'''顯示文字'''
def showText(screen, font, is_clearance, flag=False):
clock = pygame.time.Clock()
msg = 'Game Over!' if not is_clearance else 'Congratulations, you won!'
positions = [[235, 233], [65, 303], [170, 333]] if not is_clearance else [[145, 233], [65, 303], [170, 333]]
surface = pygame.Surface((400, 200))
surface.set_alpha(10)
surface.fill((128, 128, 128))
screen.blit(surface, (100, 200))
texts = [font.render(msg, True, cfg.WHITE),
font.render('Press ENTER to continue or play again.', True, cfg.WHITE),
font.render('Press ESCAPE to quit.', True, cfg.WHITE)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if is_clearance:
if not flag:
return
else:
main(initialize())
else:
main(initialize())
elif event.key == pygame.K_ESCAPE:
sys.exit()
pygame.quit()
for idx, (text, position) in enumerate(zip(texts, positions)):
screen.blit(text, position)
pygame.display.flip()
clock.tick(10)
'''初始化'''
def initialize():
pygame.init()
icon_image = pygame.image.load(cfg.ICONPATH)
pygame.display.set_icon(icon_image)
screen = pygame.display.set_mode([606, 606])
pygame.display.set_caption('吃豆豆小游戲')
return screen
'''主函數(shù)'''
def main(screen):
pygame.mixer.init()
pygame.mixer.music.load(cfg.BGMPATH)
pygame.mixer.music.play(-1, 0.0)
pygame.font.init()
font_small = pygame.font.Font(cfg.FONTPATH, 18)
font_big = pygame.font.Font(cfg.FONTPATH, 24)
for num_level in range(1, Levels.NUMLEVELS+1):
level = getattr(Levels, f'Level{num_level}')()
is_clearance = startLevelGame(level, screen, font_small)
if num_level == Levels.NUMLEVELS:
showText(screen, font_big, is_clearance, True)
else:
showText(screen, font_big, is_clearance)
'''run'''
if __name__ == '__main__':
main(initialize())
5)效果展示
總結(jié)
如果有一臺時光機,你最想回到什么時候?想必不少人的答案都會是,童年。童年的無憂無
慮,童年的單純天真,那些笑與淚的日子,放學(xué)時,三五好友嬉笑打鬧的走到學(xué)校門口小賣
店,買一包糖果,邊吃邊走回家的時光。回憶童年讓我們從游戲開始吧~
?完整的素材源碼等:可以滴滴我吖!或者點擊文末hao自取免費拿的哈~
???推薦往期文章——
項目1.8? Wifi破解免費
Python編程零基礎(chǔ)如何逆襲成為爬蟲實戰(zhàn)高手之《WIFI破解》(甩萬能鑰匙十條街)爆贊爆贊~
項目1.9? 浪漫櫻花表白代碼合集
【Python表白代碼】你喜歡什么場景下的告白?“我途徑一場櫻花的綻放 想分享給你”(永不落幕的櫻花代碼合集)
項目1.0? 圖像處理系統(tǒng)1.0版本
【OpenCV+Tkinter項目】同學(xué),你要的OpenCV圖像處理小系統(tǒng)已安排,終于有人把OpenCV那些必備的知識點講透徹了~(太厲害了,已跪)
項目1.2? 學(xué)習(xí)經(jīng)驗分享(14張思維導(dǎo)圖)
【學(xué)習(xí)編程】獻(xiàn)給迷茫中的你,教你如何快速入門編程,如何從編程小百到 IT 巨佬?零基礎(chǔ)自學(xué)請收下這份學(xué)習(xí)指南(經(jīng)驗分享)
項目1.1? ?掃雷
?Pygame實戰(zhàn):據(jù)說這是史上最難掃雷游戲,沒有之一,你們感受下......
項目1.2? ?魂斗羅
Pygame實戰(zhàn):多年后“魂斗羅”像素風(fēng)歸來 不止是經(jīng)典與情懷@全體成員
??文章匯總——
匯總合集?Python—2022 |已有文章匯總 | 持續(xù)更新,直接看這篇就夠了
(更多內(nèi)容+源碼都在?文章匯總哦!!歡迎閱讀喜歡的文章??~文章來源:http://www.zghlxwxcb.cn/news/detail-408836.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-408836.html
到了這里,關(guān)于【Python童年游戲】滿滿的回憶殺—那些年玩過的童年游戲你還記得嗎?那個才是你的菜?看到第一個我就淚奔了(致我們逝去的青春)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!