'''
Function:
主游戏
'''
import sys
import time
import random
import pygame
'''拼图精灵类'''
class pacerSprite(pygame.sprite.Sprite):
def __init__(self, img_path, size, position, downlen, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(img_path)
self.image = pygame.transform.smoothscale(self.image, size)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.downlen = downlen
self.target_x = position[0]
self.target_y = position[1] + downlen
self.type = img_path.split('/')[-1].split('.')[0]
self.fixed = False
self.speed_x = 9
self.speed_y = 9
self.direction = 'down'
def move(self):
# 下移
if self.direction == 'down':
self.rect.top = min(self.target_y, self.rect.top + self.speed_y)
if self.target_y == self.rect.top:
self.fixed = True
# 上移
elif self.direction == 'up':
self.rect.top = max(self.target_y, self.rect.top - self.speed_y)
if self.target_y == self.rect.top:
self.fixed = True
# 左移
elif self.direction == 'left':
self.rect.left = max(self.target_x, self.rect.left - self.speed_x)
if self.target_x == self.rect.left:
self.fixed = True
# 右移
elif self.direction == 'right':
self.rect.left = min(self.target_x, self.rect.left + self.speed_x)
if self.target_x == self.rect.left:
self.fixed = True
'''获取当前坐标'''
def getPosition(self):
return self.rect.left, self.rect.top
'''设置星星坐标'''
def setPosition(self, position):
self.rect.left, self.rect.top = position
'''主游戏类'''
class pacerGame():
def __init__(self, screen, sounds, font, pacer_imgs, cfg, **kwargs):
self.info = 'pacer'
self.screen = screen
self.sounds = sounds
self.font = font
self.pacer_imgs = pacer_imgs
self.cfg = cfg
self.reset()
'''开始游戏'''
def start(self):
clock = pygame.time.Clock()
# 遍历整个游戏界面更新位置
overall_moving = True
# 指定某些对象个体更新位置
individual_moving = False
# 定义一些必要的变量
pacer_selected_xy = None
pacer_selected_xy2 = None
swap_again = False
add_score = 0
add_score_showtimes = 10
time_pre = int(time.time())
# 游戏主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONUP:
if (not overall_moving) and (not individual_moving) and (not add_score):
position = pygame.mouse.get_pos()
if pacer_selected_xy is None:
pacer_selected_xy = self.checkSelected(position)
else:
pacer_selected_xy2 = self.checkSelected(position)
if pacer_selected_xy2:
if self.swappacer(pacer_selected_xy, pacer_selected_xy2):
individual_moving = True
swap_again = False
else:
pacer_selected_xy = None
if overall_moving:
overall_moving = not self.droppacers(0, 0)
# 移动一次可能可以拼出多个3连块
if not overall_moving:
res_match = self.isMatch()
add_score = self.removeMatched(res_match)
if add_score > 0:
overall_moving = True
if individual_moving:
pacer1 = self.getpacerByPos(*pacer_selected_xy)
pacer2 = self.getpacerByPos(*pacer_selected_xy2)
pacer1.move()
pacer2.move()
if pacer1.fixed and pacer2.fixed:
res_match = self.isMatch()
if res_match[0] == 0 and not swap_again:
swap_again = True
self.swappacer(pacer_selected_xy, pacer_selected_xy2)
self.sounds['mismatch'].play()
else:
add_score = self.removeMatched(res_match)
overall_moving = True
individual_moving = False
pacer_selected_xy = None
pacer_selected_xy2 = None
self.screen.fill((135, 206, 235))
self.drawGrids()
self.pacers_group.draw(self.screen)
if pacer_selected_xy:
self.drawBlock(self.getpacerByPos(*pacer_selected_xy).rect)
if add_score:
if add_score_showtimes == 10:
random.choice(self.sounds['match']).play()
self.drawAddScore(add_score)
add_score_showtimes -= 1
if add_score_showtimes < 1:
add_score_showtimes = 10
add_score = 0
self.remaining_time -= (int(time.time()) - time_pre)
time_pre = int(time.time())
self.showRemainingTime()
self.drawScore()
if self.remaining_time <= 0:
return self.score
pygame.display.update()
clock.tick(self.cfg.FPS)
'''初始化'''
def reset(self):
# 随机生成各个块(即初始化游戏地图各个元素)
while True:
self.all_pacers = []
self.pacers_group = pygame.sprite.Group()
for x in range(self.cfg.NUMGRID):
self.all_pacers.append([])
for y in range(self.cfg.NUMGRID):
pacer = pacerSprite(img_path=random.choice(self.pacer_imgs), size=(self.cfg.GRIDSIZE, self.cfg.GRIDSIZE), position=[self.cfg.XMARGIN+x*self.cfg.GRIDSIZE, self.cfg.YMARGIN+y*self.cfg.GRIDSIZE-self.cfg.NUMGRID*self.cfg.GRIDSIZE], downlen=self.cfg.NUMGRID*self.cfg.GRIDSIZE)
self.all_pacers[x].append(pacer)
self.pacers_group.add(pacer)
if self.isMatch()[0] == 0:
break
# 得分
self.score = 0
# 拼出一个的奖励
self.reward = 10
# 时间
self.remaining_time = 300
'''显示剩余时间'''
def showRemainingTime(self):
remaining_time_render = self.font.render('CountDown: %ss' % str(self.remaining_time), 1, (85, 65, 0))
rect = remaining_time_render.get_rect()
rect.left, rect.top = (self.cfg.SCREENSIZE[0]-201, 6)
self.screen.blit(remaining_time_render, rect)
'''显示得分'''
def drawScore(self):
score_render = self.font.render('SCORE:'+str(self.score), 1, (85, 65, 0))
rect = score_render.get_rect()
rect.left, rect.top = (10, 6)
self.screen.blit(score_render, rect)
'''显示加分'''
def drawAddScore(self, add_score):
score_render = self.font.render('+'+str(add_score), 1, (255, 100, 100))
rect = score_render.get_rect()
rect.left, rect.top = (250, 250)
self.screen.blit(score_render, rect)
'''生成新的拼图块'''
def generateNewpacers(self, res_match):
if res_match[0] == 1:
start = res_match[2]
while start >
30个Python游戏源码
需积分: 5 196 浏览量
2024-05-14
20:50:57
上传
评论
收藏 753.53MB RAR 举报
EnjoyEDU
- 粉丝: 9871
- 资源: 4
最新资源
- c51_2_2.c
- ASCII American Standard Code for Information Interchange
- 一个chm格式的 SQL 函数手册-SQL语言手册文档
- 计算当前月份的天数和剩余天数
- 基于ARM的指令调度和延迟分支
- 基于Vue和TypeScript的极简聊天应用设计源码 - HasChat
- 基于Vue2全家桶和Zcool数据的图片收集网站设计源码 - cool-picture
- 基于C和C++的二维绘制工具设计源码 - DrawPro
- Object.defineProperty 的 IE 补丁object-defineproperty-ie-master.zip
- 整卷预览.mhtml
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈