2000年诺基亚最为经典的机型3310正式推出,其中包含三款预装游戏,作为著名的便是贪吃蛇和俄罗斯方块,这也是贪吃蛇和俄罗斯方块系列的第一次亮相,不可否认的是,它们能成为一代经典被玩家们记住,而且后面出现各种各样的贪吃蛇、俄罗斯方块以及连连看。
使用工具:
Python3.8、pycharm2020
也可以不安装pycharm2020也可以直接运行~但是安装了些会好一点
人人都玩过的俄罗斯方块
完整源码分享
import os
import sys
import random
from modules import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
'''定义俄罗斯方块游戏类'''
class TetrisGame(QMainWindow):
def __init__(self, parent=None):
super(TetrisGame, self).__init__(parent)
# 是否暂停ing
self.is_paused = False
# 是否开始ing
self.is_started = False
self.initUI()
'''界面初始化'''
def initUI(self):
# icon
self.setWindowIcon(QIcon(os.path.join(os.getcwd(), 'resources/icon.jpg')))
# 块大小
self.grid_size = 22
# 游戏帧率
self.fps = 200
self.timer = QBasicTimer()
# 焦点
self.setFocusPolicy(Qt.StrongFocus)
# 水平布局
layout_horizontal = QHBoxLayout()
self.inner_board = InnerBoard()
self.external_board = ExternalBoard(self, self.grid_size, self.inner_board)
layout_horizontal.addWidget(self.external_board)
self.side_panel = SidePanel(self, self.grid_size, self.inner_board)
layout_horizontal.addWidget(self.side_panel)
self.status_bar = self.statusBar()
self.external_board.score_signal[str].connect(self.status_bar.showMessage)
self.start()
self.center()
self.setWindowTitle('Tetris —— 九歌')
self.show()
self.setFixedSize(self.external_board.width() +
self.side_panel.width(), self.side_panel.height() +
self.status_bar.height())
'''游戏界面移动到屏幕中间'''
def center(self):
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width()) // 2, (screen.height() - size.height()) // 2)
'''更新界面'''
def updateWindow(self):
self.external_board.updateData()
self.side_panel.updateData()
self.update()
'''开始'''
def start(self):
if self.is_started:
return
self.is_started = True
self.inner_board.createNewTetris()
self.timer.start(self.fps, self)
'''暂停/不暂停'''
def pause(self):
if not self.is_started:
return
self.is_paused = not self.is_paused
if self.is_paused:
self.timer.stop()
self.external_board.score_signal.emit('Paused')
else:
self.timer.start(self.fps, self)
self.updateWindow()
'''计时器事件'''
def timerEvent(self, event):
if event.timerId() == self.timer.timerId():
removed_lines = self.inner_board.moveDown()
self.external_board.score += removed_lines
self.updateWindow()
else:
super(TetrisGame, self).timerEvent(event)
'''按键事件'''
def keyPressEvent(self, event):
if not self.is_started or self.inner_board.current_tetris == tetrisShape().shape_empty:
super(TetrisGame, self).keyPressEvent(event)
return
key = event.key()
# P键暂停
if key == Qt.Key_P:
self.pause()
return
if self.is_paused:
return
# 向左
elif key == Qt.Key_Left:
self.inner_board.moveLeft()
# 向右
elif key == Qt.Key_Right:
self.inner_board.moveRight()
# 旋转
elif key == Qt.Key_Up:
self.inner_board.rotateAnticlockwise()
# 快速坠落
elif key == Qt.Key_Space:
self.external_board.score += self.inner_board.dropDown()
else:
super(TetrisGame, self).keyPressEvent(event)
self.updateWindow()
'''run'''
if __name__ == '__main__':
app = QApplication([])
tetris = TetrisGame()
sys.exit(app.exec_())
最原始的贪吃蛇
完整源码分享
五颜六色的连连看
完整源码分享
import os
import sys
import cfg
import pygame
from modules import *
'''游戏主程序'''
def main():
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('Gemgem —— 九歌')
# 加载背景音乐
pygame.mixer.init()
pygame.mixer.music.load(os.path.join(cfg.ROOTDIR, "resources/audios/bg.mp3"))
pygame.mixer.music.set_volume(0.6)
pygame.mixer.music.play(-1)
# 加载音效
sounds = {}
sounds['mismatch'] = pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/badswap.wav'))
sounds['match'] = []
for i in range(6):
sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/match%s.wav' % i)))
# 加载字体
font = pygame.font.Font(os.path.join(cfg.ROOTDIR, 'resources/font/font.TTF'), 25)
# 图片加载
gem_imgs = []
for i in range(1, 8):
gem_imgs.append(os.path.join(cfg.ROOTDIR, 'resources/images/gem%s.png' % i))
# 主循环
game = gemGame(screen, sounds, font, gem_imgs, cfg)
while True:
score = game.start()
flag = False
# 一轮游戏结束后玩家选择重玩或者退出
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.KEYUP and event.key == pygame.K_r:
flag = True
if flag:
break
screen.fill((135, 206, 235))
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 = font.render(text, 1, (85, 65, 0))
rect = text_render.get_rect()
if idx == 0:
rect.left, rect.top = (212, y)
elif idx == 1:
rect.left, rect.top = (122.5, y)
else:
rect.left, rect.top = (126.5, y)
y += 100
screen.blit(text_render, rect)
pygame.display.update()
game.reset()
'''run'''
if __name__ == '__main__':
main()
总结
我是悦悦,一名喜欢分享知识的程序媛,感兴趣的就赶紧来点击关注我叭~哪里有不明白或有不同观点的地方欢迎留言!