python写迷你小游戏下载_用Python写一个简单的小游戏

本文介绍如何使用Python编程实现一个简单的俄罗斯方块游戏。从创建py文件开始,定义所需模块,包括游戏区域的尺寸、颜色、边框等,接着详细解释了画背景图、网格线、游戏区域、单个方块以及得分信息的绘制方法。通过这段代码,你可以亲自动手制作一个属于自己的俄罗斯方块游戏。
摘要由CSDN通过智能技术生成

相信大家都玩过俄罗斯方块吧,应该是小时候的回忆吧,但是想不想了解一下这个程序是怎么写出来的呢,自己写出来的应该玩起来更有感觉吧!

感觉还是蛮好玩吧!

接下来,我就分享一下这个游戏的源码过程啊!

先用python创建一个py文件

定义这次程序所需要的类import sys

import time

import pygame

from pygame.localsimport *

import blocks

然后写出它所需要的模块SIZE =30 # 每个小方格大小

BLOCK_HEIGHT =25  # 游戏区高度

BLOCK_WIDTH =10  # 游戏区宽度

BORDER_WIDTH =4  # 游戏区边框宽度

BORDER_COLOR = (40, 40, 200)# 游戏区边框颜色

SCREEN_WIDTH = SIZE * (BLOCK_WIDTH +5)# 游戏屏幕的宽

SCREEN_HEIGHT = SIZE * BLOCK_HEIGHT# 游戏屏幕的高

BG_COLOR = (40, 40, 60)# 背景色

BLOCK_COLOR = (20, 128, 200)#

BLACK = (0, 0, 0)

RED = (200, 30, 30)# GAME OVER 的字体颜色

画背景图def _draw_background(screen):

# 填充背景色

screen.fill(BG_COLOR)

# 画游戏区域分隔线

pygame.draw.line(screen, BORDER_COLOR,

(SIZE * BLOCK_WIDTH + BORDER_WIDTH //2, 0),

(SIZE * BLOCK_WIDTH + BORDER_WIDTH //2, SCREEN_HEIGHT), BORDER_WIDTH)

画网格线def _draw_gridlines(screen):

# 画网格线 竖线

for xin range(BLOCK_WIDTH):

pygame.draw.line(screen, BLACK, (x * SIZE, 0), (x * SIZE, SCREEN_HEIGHT), 1)

# 画网格线 横线

for yin range(BLOCK_HEIGHT):

pygame.draw.line(screen, BLACK, (0, y * SIZE), (BLOCK_WIDTH * SIZE, y * SIZE), 1)

# 画已经落下的方块def _draw_game_area(screen, game_area):

if game_area:

for i, rowin enumerate(game_area):

for j, cellin enumerate(row):

if cell !='.':

pygame.draw.rect(screen, BLOCK_COLOR, (j * SIZE, i * SIZE, SIZE, SIZE), 0)

# 画单个方块def _draw_block(screen, block, offset_x, offset_y, pos_x, pos_y):

if block:

for iin range(block.start_pos.Y, block.end_pos.Y +1):

for jin range(block.start_pos.X, block.end_pos.X +1):

if block.template[i][j] !='.':

pygame.draw.rect(screen, BLOCK_COLOR,

(offset_x + (pos_x + j) * SIZE, offset_y + (pos_y + i) * SIZE, SIZE, SIZE), 0)

# 画得分等信息def _draw_info(screen, font, pos_x, font_height, score):

print_text(screen, font, pos_x, 10, f'得分: ')

print_text(screen, font, pos_x, 10 + font_height +6, f'{score}')

print_text(screen, font, pos_x, 20 + (font_height +6) *2, f'速度: ')

print_text(screen, font, pos_x, 20 + (font_height +6) *3, f'{score //10000}')

print_text(screen, font, pos_x, 30 + (font_height +6) *4, f'下一个:')

if __name__ =='__main__':

main()

c9989277ffda

这样就可以写出来一个十分简单的俄罗斯方块啦,是不是觉得还不错呢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值