python如何使板子移动_python实现移动木板小游戏
本文实例为大家分享了python实现移动木板小游戏的具体代码,供大家参考,具体内容如下
一、游戏简介
本游戏是通过python编写的小游戏,给初学者熟悉python编程语言抛砖引玉,希望有所帮助。
成型的效果图如下:
二、编写步骤
1.引入库
代码如下:
###### AUTHOR:破茧狂龙 ######
###### DATE:20201002 ######
###### DESCRIPTION:移动的木板 ######
import pygame
from pygame.locals import *
import sys
import time
import random
2.初始化
代码如下:
pygame.init()
BLACK = (0, 0, 0) # 黑色
WHITE = (255, 255, 255) # 白色
bg_color = (0,0,70) # 背景颜色
red = (200, 0, 0)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)
smallText = pygame.font.SysFont('SimHei', 20) #comicsansms
midlText = pygame.font.SysFont('SimHei', 50)
barsize = [30, 10]
SCREEN_SIZE = [400, 500] # 屏幕大小
BALL_SIZE = [15, 15] # 球的尺寸
fontcolor = (255,255,255) # 定义字体的颜色
myimg = r"img\b1.jpg"
background = pygame.image.load(myimg) # 图片位置
background = pygame.transform.scale(background, SCREEN_SIZE)
# ball 初始位置
ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2
ball_pos_y = 0
# ball 移动方向
ball_dir_y = 1 # 1:down
ball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
clock = pygame.time.Clock() # 定时器
screen = pygame.display.set_mode(SCREEN_SIZE)
# 设置标题
pygame.display.set_caption('python小游戏-移动木板')
# 设置图标
image = pygame.image.load(myimg)
pygame.display.set_icon(image)
3.相关自定义函数
代码如下:
###### 自定义函数 ######
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(textSurf, textRect)
def text_objects(text, font):
textSurface = font.render(text, True, fontcolor)
return textSurface, textSurface.get_rect()
def quitgame():
pygame.quit()
quit()
def message_diaplay(text):
largeText = pygame.font.SysFont('SimHei', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((screen[0] / 2), (screen[1] / 2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
4.相关自定义函数
代码如下:
def game_first_win():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(bg_color)
###游戏名称
TextSurf, TextRect = text_objects('移动木板', midlText)
TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
screen.blit(TextSurf, TextRect)
###作者
TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:破茧狂龙', smallText)
TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
screen.blit(TextSurf_ZZ, TextRect_ZZ)
button("开始", 60, 400, 100, 50, green, bright_green, game_loop)
button("取消", 230, 400, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
###### 移动的木板游戏类 ######
def game_loop():
pygame.mouse.set_visible(1) # 移动鼠标不可见
###变量###
score = 0 #分数
count_O = 0 #循环的次数变量1 用于统计等级
count_N = 0 #循环的次数变量2 用于统计等级
c_level = 1 #等级
x_change = 0 #移动的变量
x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
y = SCREEN_SIZE[1] - barsize[1]
# ball 初始位置
ball_pos_pz = ball_pos
while True:
bar_move_left = False
bar_move_right = False
###当每次满X分后,升级等级
if count_O != count_N and score % 5 == 0:
c_level += 1
count_O = count_N
###### 获取键盘输入 ######
for event in pygame.event.get():
if event.type == QUIT: # 当按下关闭按键
pygame.quit()
sys.exit() # 接收到退出事件后退出程序
elif event.type == KEYDOWN:
##按键盘Q键 暂停
if event.key == pygame.K_q:
time.sleep(10)
##左移动
if event.key == pygame.K_LEFT:
bar_move_left = True
x_change = -30
else:
bar_move_left = False
##右移动
if event.key == pygame.K_RIGHT:
bar_move_right = True
x_change = +30
else:
bar_move_right = False
if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
bar_move_left = False
bar_move_right = False
##木板的位置移动
if bar_move_left == True and bar_move_right == False:
x += x_change
if bar_move_left == False and bar_move_right == True:
x += x_change
##填充背景
screen.blit(background, (0, 0)) # (0,0)代表图片位置起点x 轴 Y轴
##获取最新的木板位置,并渲染在前台
bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
bar_pos.left = x
pygame.draw.rect(screen, WHITE, bar_pos)
## 球移动,并渲染在前台
ball_pos_pz.bottom += ball_dir_y * 3
pygame.draw.rect(screen, WHITE, ball_pos_pz)
## 判断球是否落到板上
if bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
score += 1 # 分数每次加1
count_N += 1
elif bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
print("Game Over: ", score)
return score
## 更新球下落的初始位置
if bar_pos.top <= ball_pos_pz.bottom:
ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
######### 显示游戏等级 #########
TextSurf_lev, TextRect_lev = text_objects("等级 : " + str(c_level), smallText)
TextRect_lev.center = (60, 20)
screen.blit(TextSurf_lev, TextRect_lev)
######### 显示分数结果 #########
TextSurf_sco, TextRect_sco = text_objects("分数 : " + str(score), smallText)
TextRect_sco.center = (60, 50)
screen.blit(TextSurf_sco, TextRect_sco)
pygame.display.update() # 更新软件界面显示
clock.tick(60)
# 三、完整的代码
代码如下:
###### AUTHOR:破茧狂龙 ######
###### DATE:20201002 ######
###### DESCRIPTION:移动的木板 ######
import pygame
from pygame.locals import *
import sys
import time
import random
pygame.init()
BLACK = (0, 0, 0) # 黑色
WHITE = (255, 255, 255) # 白色
bg_color = (0,0,70) # 背景颜色
red = (200, 0, 0)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)
smallText = pygame.font.SysFont('SimHei', 20) #comicsansms
midlText = pygame.font.SysFont('SimHei', 50)
barsize = [30, 10]
SCREEN_SIZE = [400, 500] # 屏幕大小
BALL_SIZE = [15, 15] # 球的尺寸
fontcolor = (255,255,255) # 定义字体的颜色
myimg = r"img\b1.jpg"
background = pygame.image.load(myimg) # 图片位置
background = pygame.transform.scale(background, SCREEN_SIZE)
# ball 初始位置
ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2
ball_pos_y = 0
# ball 移动方向
ball_dir_y = 1 # 1:down
ball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
clock = pygame.time.Clock() # 定时器
screen = pygame.display.set_mode(SCREEN_SIZE)
# 设置标题
pygame.display.set_caption('python小游戏-移动木板')
# 设置图标
image = pygame.image.load(myimg)
pygame.display.set_icon(image)
###### 自定义函数 ######
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x + (w / 2)), (y + (h / 2)))
screen.blit(textSurf, textRect)
def text_objects(text, font):
textSurface = font.render(text, True, fontcolor)
return textSurface, textSurface.get_rect()
def quitgame():
pygame.quit()
quit()
def message_diaplay(text):
largeText = pygame.font.SysFont('SimHei', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((screen[0] / 2), (screen[1] / 2))
screen.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def game_first_win():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill(bg_color)
###游戏名称
TextSurf, TextRect = text_objects('移动木板', midlText)
TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
screen.blit(TextSurf, TextRect)
###作者
TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:破茧狂龙', smallText)
TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
screen.blit(TextSurf_ZZ, TextRect_ZZ)
button("开始", 60, 400, 100, 50, green, bright_green, game_loop)
button("取消", 230, 400, 100, 50, red, bright_red, quitgame)
pygame.display.update()
clock.tick(15)
###### 移动的木板游戏类 ######
def game_loop():
pygame.mouse.set_visible(1) # 移动鼠标不可见
###变量###
score = 0 #分数
count_O = 0 #循环的次数变量1 用于统计等级
count_N = 0 #循环的次数变量2 用于统计等级
c_level = 1 #等级
x_change = 0 #移动的变量
x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
y = SCREEN_SIZE[1] - barsize[1]
# ball 初始位置
ball_pos_pz = ball_pos
while True:
bar_move_left = False
bar_move_right = False
###当每次满X分后,升级等级
if count_O != count_N and score % 5 == 0:
c_level += 1
count_O = count_N
###### 获取键盘输入 ######
for event in pygame.event.get():
if event.type == QUIT: # 当按下关闭按键
pygame.quit()
sys.exit() # 接收到退出事件后退出程序
elif event.type == KEYDOWN:
##按键盘Q键 暂停
if event.key == pygame.K_q:
time.sleep(10)
##左移动
if event.key == pygame.K_LEFT:
bar_move_left = True
x_change = -30
else:
bar_move_left = False
##右移动
if event.key == pygame.K_RIGHT:
bar_move_right = True
x_change = +30
else:
bar_move_right = False
if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
bar_move_left = False
bar_move_right = False
##木板的位置移动
if bar_move_left == True and bar_move_right == False:
x += x_change
if bar_move_left == False and bar_move_right == True:
x += x_change
##填充背景
screen.blit(background, (0, 0)) # (0,0)代表图片位置起点x 轴 Y轴
##获取最新的木板位置,并渲染在前台
bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
bar_pos.left = x
pygame.draw.rect(screen, WHITE, bar_pos)
## 球移动,并渲染在前台
ball_pos_pz.bottom += ball_dir_y * 3
pygame.draw.rect(screen, WHITE, ball_pos_pz)
## 判断球是否落到板上
if bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
score += 1 # 分数每次加1
count_N += 1
elif bar_pos.top <= ball_pos_pz.bottom and (
bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
print("Game Over: ", score)
return score
## 更新球下落的初始位置
if bar_pos.top <= ball_pos_pz.bottom:
ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])
######### 显示游戏等级 #########
TextSurf_lev, TextRect_lev = text_objects("等级 : " + str(c_level), smallText)
TextRect_lev.center = (60, 20)
screen.blit(TextSurf_lev, TextRect_lev)
######### 显示分数结果 #########
TextSurf_sco, TextRect_sco = text_objects("分数 : " + str(score), smallText)
TextRect_sco.center = (60, 50)
screen.blit(TextSurf_sco, TextRect_sco)
pygame.display.update() # 更新软件界面显示
clock.tick(60)
####程序执行顺序######
game_first_win()
game_loop()
pygame.quit()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
python如何使板子移动_python实现移动木板小游戏相关推荐
- python飞机大战加背景音乐_python实现飞机大战小游戏 python飞机大战中的音频文件怎么改成MP3...
怎么样用Python写飞机大战游戏 python开发飞机大战外星人游戏怎么弄双人模式新的一年,哪怕仍是一个人,也要活得像一支队伍,为自己的头脑和心灵招兵买马,不气馁,有召唤,爱自由. 主函数 impo ...
- python成语接龙代码_python——成语接龙小游戏
小试牛刀的简易成语接龙. 思路-- 1.网上下载成语字典的txt版本 2.通过python进行处理得到格式化的成语,并整理成字典(python字典查找速度快) 3.python程序,查找 用户输入的最 ...
- python联机游戏制作_Python练习01-对战小游戏
Python练习01-对战小游戏 近期在学习Python,其中有一道练习题,写一个对战小游戏,随机人物,随机属性(血量.攻击).指定我方人物出场顺序,三局两胜,代码如下: import time, r ...
- Python版基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式
基于pygame的玛丽快跑小游戏源代码,玛丽冒险小游戏代码,支持双人模式 按空格进入单人模式,按't'进入双人模式,双人模式下玛丽1采用空格键上跳,玛丽2采用方向上键上跳. 完整代码下载地址:Pyth ...
- [python] 写个既有趣又可爱的弹球小游戏
用python写一个既有趣有可爱的弹球小游戏 这几天闲着无聊,就在家写着游戏玩一玩,发现一个这样有趣的小游戏,我通过pygame把它先做了出来,话不多少先上代码. 首先先导入包,导包是必不可少的一件事 ...
- python自学篇十[ 面向对象 (四) :王者荣耀小游戏+模拟一个简单的银行进行业务办理的类]
python基础系列: python自学篇一[ Anaconda3安装 ] python自学篇二[ pycharm安装及使用 ] python自学篇三[ 判断语句if的使用 ] python自学篇四[ ...
- python剪刀石头布小游戏源码下载_Python Tkinter实现剪刀石头布小游戏
Python Tkinter实现剪刀石头布小游戏 发布时间:2020-10-26 14:56:52 来源:亿速云 阅读:67 作者:Leah 本篇文章给大家分享的是有关Python Tkinter实现 ...
- python接水果游戏代码_Python开发接水果小游戏编程
我研发的Python游戏引擎Pylash已经更新到1.4了.现在我们就来使用它完成一个极其简单的小游戏:接水果.以下是游戏截图: vc/yvPy/2NbGyMvO79LGtq+jrMq5yMvO79P ...
- python贪吃蛇小游戏_python开发贪吃蛇小游戏
3.概要设计 3.1 程序功能模块 由设计应解决的问题可知,本次的设计是使用用方向键来实现一个简易的贪吃蛇小游戏的程序,具体的功能模块如图3-1所示. 图3-1 程序功能模块 Fig.3-1 prog ...
最新文章
- 2021年全球十大工程成就,中国有几个? | 科技袁人
- php cms帮助文档,phpcms手册
- excel 怎么让数字不用科学计数法
- JMeter 压力測试使用函数和 CSV 文件參数化 json 数据
- command not found: django-admin.py
- Educational Codeforces Round 112 E.Boring Segments-线段树+双指针
- scala 类中的对象是类_Scala中的类和对象
- 如何更改电脑ip地址租期_局域网通过IP地址如何找到电脑的位置
- 【微信小程序】带你做一个公众号留言系统(附源码)
- iPhone 12顶配版延期到10月:刘海仍在 后置3摄+雷达
- iPhone未来怎么走?将融入脸部以及指纹辨识系统
- 梦醒了,一切都结束了
- onfling滑动界面进行Activity切换
- FireFox2和FireFox3共存解决方案(附完整图解)
- C#-WinForm-打印控件
- Frsky X9D Plus遥控器和 Frisky R8 Pro接收机对频
- 本地计算机无法设置共享文件夹,共享服务,详细教您win10共享文件夹无法访问怎么办...
- 计算机网络基础课后习题,《计算机网络技术基础》课后习题参考答案
- MySQL索引机制-图灵教育诸葛老师
- 南京邮电大学计算机学院讲师黄璞,我所与南京邮电大学建立“计算机技术联合实验室”...
热门文章
- Yii2 ajax的post请求Csrf验证失败
- AirSim配置文件front_stereo_and_center_mono解析
- pks与终端服务器通讯,德国HIMA黑马通讯卡件F8627
- 这才是计算机科学_计算机安全
- ss linux安装教程,Linux安装ss5详细步骤
- 富士变频器专用软件,loader软件。可用于富士在售变频器所有机型
- MySql的数据找回
- 【Chapter 1】新博手的诞生
- springsecurity oauth2使用jwt实现单点登录
- 手把手教你简易上手GitHub(教程)