Python开心消消乐源代码

更新时间:2023-08-09 08:12:30 阅读: 评论:0

Python开⼼消消乐源代码
Python开⼼消消乐⼩游戏源代码,源代码由三个py⽂件和⼀个资源包组成,cfg.py为配置⽂件,配置打开游戏屏幕⼤⼩等,game.py拼图精灵类:⾸先通过配置⽂件中,获取⽅块精灵的路径,加载到游戏⾥。定义move()移动模块的函数,这个移动⽐较简单。模块之间,只有相邻的可以相互移动,xxls.py在主程序中,通过读取配置⽂件,引⼊项⽬资源:包括图⽚、⾳频等,并从我们的modules⾥引⼊所有我们的模块。完整程序代码及资源包请往Python代码⼤全公众号下载:
cfg.py
'''主配置⽂件'''
import os
'''屏幕设置⼤⼩'''
SCREENSIZE =(700,700)
'''元素尺⼨'''
NUMGRID =8
GRIDSIZE =64
XMARGIN =(SCREENSIZE[0]- GRIDSIZE * NUMGRID)//2
YMARGIN =(SCREENSIZE[1]- GRIDSIZE * NUMGRID)//2
'''获取根⽬录'''
ROOTDIR = os.getcwd()
'''FPS'''
FPS =30
xxls.py
'''
Function:
消消乐
'''
where you areimport os
import sys
import cfg
import pygame
from XiaoXiaoLe import*
from XiaoXiaoLe.game import pacerGame
'''主程序'''
def main(game=None):
pygame.init()
screen = pygame.display.t_mode(cfg.SCREENSIZE)
pygame.display.t_caption('开⼼消消乐-Python代码⼤全')
# 加载背景⾳乐
提高英语pygame.mixer.init()
pygame.mixer.music.load(os.path.join(cfg.ROOTDIR,"res/audios/bg.mp3"))
pygame.mixer.music.t_volume(0.6)
pygame.mixer.music.play(-1)
# 加载⾳效
sounds ={}
sounds['mismatch']= pygame.mixer.Sound(os.path.join(cfg.ROOTDIR,'res/audios/badswap.wav'))
sounds['match']=[]
for i in range(6):
sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR,'res/audios/match%s.wav'% i)))
# 字体显⽰
font = pygame.font.Font(os.path.join(cfg.ROOTDIR,'res/font/font.TTF'),25)
# 星星
pacer_imgs =[]
for i in range(1,8):
outpacer_imgs.append(os.path.join(cfg.ROOTDIR,'res/imgs/pacer%s.png'% i))
# 循环
game = pacerGame(screen, sounds, font, pacer_imgs, cfg)
game = pacerGame(screen, sounds, font, pacer_imgs, cfg)
while True:
score = game.start()
flag =Fal
# 给出选择,玩家选择重玩或者退出
while True:
for event in ():
pe== pygame.QUIT pe== pygame.KEYUP and event.key == pygame.K_ESCAPE):                    pygame.quit()
pe== pygame.KEYUP and event.key == pygame.K_r:
flag =True
if flag:
break
screen.fill((136,207,236))
text0 ='Final score: %s'% score
text1 ='Press <R> to restart the game.'
text2 ='Press <Esc> to quit the game.'
y =150
for idx, text in enumerate([text0, text1, text2]):
text_render = der(text,1,(85,65,0))
rect = _rect()
if idx ==0:
rect.left, p =(223, y)
elif idx ==1:
rect.left, p =(133.5, y)
el:
rect.left, p =(126.5, y)
y +=99
screen.blit(text_render, rect)
pygame.display.update()
<()
'''游戏运⾏'''
if __name__ =='__main__':
main()
game.py
'''
Function:
主游戏
'''
import sys
import time
import random
import pygame
'''拼图精灵类'''
class pacerSprite(pygame.sprite.Sprite):
def__init__(lf, img_path, size, position, downlen,**kwargs):
pygame.sprite.Sprite.__init__(lf)
lf.image = pygame.image.load(img_path)
lf.image = ansform.smoothscale(lf.image, size)
< = _rect()
lf.downlen = downlen
lf.target_x = position[0]
lf.target_y = position[1]+ downlen
lf.fixed =Fal
lf.speed_x =9
lf.speed_y =9
lf.direction ='down'
lf.direction ='down'
def move(lf):
# 下移
if lf.direction =='down':
if lf.target_y == p:
lf.fixed =True
# 上移
elif lf.direction =='up':
if lf.target_y == p:
lf.fixed =True
# 左移
elif lf.direction =='left':
if lf.target_x == lf.rect.left:
lf.fixed =True
# 右移
elif lf.direction =='right':
if lf.target_x == lf.rect.left:
lf.fixed =True
'''获取当前坐标'''
def getPosition(lf):
left, p
'''设置星星坐标'''
def tPosition(lf, position):
'''主游戏类'''
class pacerGame():
ritual
def__init__(lf, screen, sounds, font, pacer_imgs, cfg,**kwargs):
lf.info ='pacer'
lf.screen = screen
lf.sounds = sounds
lf.font = font
lf.pacer_imgs = pacer_imgs
lf.cfg = cfg
<()
'''开始游戏'''
def start(lf):
clock = pygame.time.Clock()
# 遍历整个游戏界⾯更新位置
overall_moving =True
# 指定某些对象个体更新位置
individual_moving =Fal
# 定义⼀些必要的变量
pacer_lected_xy =None
pacer_lected_xy2 =None
swap_again =Fal
add_score =0
add_score_showtimes =10
time_pre =int(time.time())
# 游戏主循环
while True:
for event in ():
pe== pygame.QUIT pe== pygame.KEYUP and event.key == pygame.K_ESCAPE):                    pygame.quit()
pe== pygame.MOUSEBUTTONUP:
if(not overall_moving)and(not individual_moving)and(not add_score):
position = _pos()
position = _pos()
礼仪培训班if pacer_lected_xy is None:
pacer_lected_xy = lf.checkSelected(position)
el:
pacer_lected_xy2 = lf.checkSelected(position)
if pacer_lected_xy2:
if lf.swappacer(pacer_lected_xy, pacer_lected_xy2):                                    individual_moving =True
swap_again =Fal
el:
pacer_lected_xy =None
if overall_moving:
overall_moving =not lf.droppacers(0,0)
# 移动⼀次可能可以拼出多个3连块
if not overall_moving:
res_match = lf.isMatch()
add_score = lf.removeMatched(res_match)
if add_score >0:
overall_moving =True
if individual_moving:
pacer1 = lf.getpacerByPos(*pacer_lected_xy)
pacer2 = lf.getpacerByPos(*pacer_lected_xy2)
if pacer1.fixed and pacer2.fixed:
res_match = lf.isMatch()
if res_match[0]==0and not swap_again:
swap_again =True
lf.swappacer(pacer_lected_xy, pacer_lected_xy2)
lf.sounds['mismatch'].play()
el:
add_score = lf.removeMatched(res_match)
overall_moving =True
individual_moving =Fal
pacer_lected_xy =None
pacer_lected_xy2 =None
lf.screen.fill((135,206,235))
lf.drawGrids()
lf.pacers_group.draw(lf.screen)
onepiece是什么if pacer_lected_xy:
lf.pacerByPos(*pacer_lected_xy).rect) if add_score:
if add_score_showtimes ==10:
random.choice(lf.sounds['match']).play()
lf.drawAddScore(add_score)
add_score_showtimes -=1
if add_score_showtimes <1:
add_score_showtimes =10
add_score =0
蒂娜菲time_pre =int(time.time())
lf.showRemainingTime()
lf.drawScore()
aining_time <=0:
return lf.score
pygame.display.update()
clock.tick(lf.cfg.FPS)
'''初始化'''
def ret(lf):
# 随机⽣成各个块(即初始化游戏地图各个元素)
while True:
lf.all_pacers =[]
lf.pacers_group = pygame.sprite.Group()
for x in range(lf.cfg.NUMGRID):
lf.all_pacers.append([])
for y in range(lf.cfg.NUMGRID):
for y in range(lf.cfg.NUMGRID):
pacer = pacerSprite(img_path=random.choice(lf.pacer_imgs), size=(lf.cfg.GRIDSIZE, lf.cfg.GRIDSIZE), position=[lf.cfg.XMARGIN+x*s elf.cfg.GRIDSIZE, lf.cfg.YMARGIN+y*lf.cfg.GRIDSIZE-lf.cfg.NUMGRID*lf.cfg.GRIDSIZE], downlen=lf.cfg.NUMGRID*lf.cfg.GRIDSIZE)
lf.all_pacers[x].append(pacer)
lf.pacers_group.add(pacer)
if lf.isMatch()[0]==0:
break
# 得分
lf.score =0
# 拼出⼀个的奖励
英语词根# 时间
'''显⽰剩余时间'''
def showRemainingTime(lf):
remaining_time_render = der('CountDown: %ss'%aining_time),1,(85,65,0))
rect = remaining__rect()
rect.left, p =(lf.cfg.SCREENSIZE[0]-201,6)
lf.screen.blit(remaining_time_render, rect)
'''显⽰得分'''
def drawScore(lf):
score_render = der('SCORE:'+str(lf.score),1,(85,65,0))
rect = _rect()
rect.left, p =(10,6)
lf.screen.blit(score_render, rect)
'''显⽰加分'''
def drawAddScore(lf, add_score):
score_render = der('+'+str(add_score),1,(255,100,100))
rect = _rect()
rect.left, p =(250,250)
lf.screen.blit(score_render, rect)
'''⽣成新的拼图块'''
def generateNewpacers(lf, res_match):
if res_match[0]==1:
start = res_match[2]
while start >-2:
for each in[res_match[1], res_match[1]+1, res_match[1]+2]:
pacer = lf.getpacerByPos(*[each, start])
if start == res_match[2]:
lf.ve(pacer)
lf.all_pacers[each][start]=None
elif start >=0:
pacer.target_y += lf.cfg.GRIDSIZE
pacer.fixed =Fal
pacer.direction ='down'
lf.all_pacers[each][start+1]= pacer
el:
pacer = pacerSprite(img_path=random.choice(lf.pacer_imgs), size=(lf.cfg.GRIDSIZE, lf.cfg.G
RIDSIZE), position=[lf.cfg.XMARGIN+ each*lf.cfg.GRIDSIZE, lf.cfg.YMARGIN-lf.cfg.GRIDSIZE], downlen=lf.cfg.GRIDSIZE)
lf.pacers_group.add(pacer)
lf.all_pacers[each][start+1]= pacer
start -=1
elif res_match[0]==2:
start = res_match[2]
while start >-4:
if start == res_match[2]:
for each in range(0,3):
pacer = lf.getpacerByPos(*[res_match[1], start+each])
lf.ve(pacer)
lf.all_pacers[res_match[1]][start+each]=None
elif start >=0:
pacer = lf.getpacerByPos(*[res_match[1], start])
pacer.target_y += lf.cfg.GRIDSIZE *3
pacer.fixed =Fal
pacer.direction ='down'homecare
lf.all_pacers[res_match[1]][start+3]= pacer

本文发布于:2023-08-09 08:12:30,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/192056.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:游戏   移动   资源
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图