
Lv.1
QAQ
签名:隐
在 【Python作品分享】游戏 中回复
#pygame游戏库,sys操控python运行的环境
import pygame
import sys
import random
#这个模块包含所有pygame所使用的常亮
from pygame.locals import *
#1,定义颜色变量
#0-255 0黑色 255白色
redColor = pygame.Color(255, 0, 0)
#背景为黑色
blackColor = pygame.Color(0, 0, 0)
#贪吃蛇为白色
whiteColor = pygame.Color(255, 255, 255)
#定义游戏结束的函数
def gameover():
pygame.quit()
sys.exit()
#定义main函数--》定义我们的入口函数
def main():
#初始化pygame
pygame.init()
#定义一个变量来控制速度
fpsClock = pygame.time.Clock()
#创建pygame显示层,创建一个界面
playsurface = pygame.display.set_mode((喵0, 480))
pygame.display.set_caption('贪吃蛇')
#初始化变量
#贪吃蛇初始坐标位置 (先以100,100为基准)
snakePosition = [100, 100]
#初始化贪吃蛇的长度列表中有个元素就代表有几段身体
snakeBody = [[100, 100], [80, 100], [60, 100]]
#初始化目标方向额位置
targetPosition = [300, 300]
#目标方块的标记 目的:判断是否吃掉了这个目标方块1 就是没有吃 0就是吃掉
targetflag = 1
#初始化方向 --》往右
direction = 'right'
#定义一个方向变量(人为控制 按键)
changeDirection = direction
while True:
for event in pygame.event.get(): # 从队列中获取事件
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_d:
changeDirection = 'right'
if event.key == K_a:
changeDirection = 'left'
if event.key == K_w:
changeDirection = 'up'
if event.key == K_s:
changeDirection = 'down'
#对应键盘上的esc文件
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
#确定方向
if changeDirection == 'left' and not direction == 'right':
direction = changeDirection
if changeDirection == 'right' and not direction == 'left':
direction = changeDirection
if changeDirection == 'up' and not direction == 'down':
direction = changeDirection
if changeDirection == 'down' and not direction == 'up':
direction = changeDirection
#根据方向移动蛇头
if direction == 'right':
snakePosition[0] += 20
if direction == 'left':
snakePosition[0] -= 20
if direction == 'up':
snakePosition[1] -= 20
if direction == 'down':
snakePosition[1] += 20
#增加蛇的长度
snakeBody.insert(0, list(snakePosition))
#如果贪吃蛇和目标方块的位置重合
if snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]:
targetflag = 0
else:
snakeBody.pop()
if targetflag == 0:
x = random.randrange(1, 32)
y = random.randrange(1, 24)
targetPosition = [int(x*20), int(y*20)]
targetflag = 1
#填充背景颜色
playsurface.fill(blackColor)
for position in snakeBody:
#第一个参数serface指定一个serface编辑区,在这个区域内绘制
#第二个参数color:颜色
#第三个参数:rect:返回一个矩形(xy),(width,height)
#第四个参数:width:表示线条的粗细 width0填充 实心
#化蛇
pygame.draw.rect(playsurface, redColor, Rect(
position[0], position[1], 20, 20))
pygame.draw.rect(playsurface, whiteColor, Rect(
targetPosition[0], targetPosition[1], 20, 20))
#更新显示到屏幕表面
pygame.display.flip()
#判断是否游戏结束
if snakePosition[0] > 620 or snakePosition[0] < 0:
gameover()
elif snakePosition[1] > 460 or snakePosition[1] < 0:
gameover()
#控制游戏速度
fpsClock.tick(2)
# 启动入口函数
if __name__ == '__main__':
try:
main()
except SystemExit:
pass
2020-06-30T20:26:02 点赞:0
在 你还在破坏环境吗 中回复
2020-07-01T19:18:52 点赞:0
在 你还在破坏环境吗 中回复
空调是不能够直接造成温度上升的,众所周知,以前空调或者冰箱制冷都用的氟利昂,也是破坏臭氧层的元凶,这就导致臭氧稀薄,紫外线辐射到地表的增多,温度也会上升;再者用电量的增加,因为大部分地区还是煤炭发电,这样会产生大量二氧化碳气体,它是导致全球气候气候上升的最终元凶。总之保护环境,人人有责,我们要节能减排,多种树,锐劲特和大家一起为保护环境而努力!
2020-07-01T19:20:48 点赞:0
在 【Python作品分享】更好用的计算器【作品秀】 中回复
def calcfloat():
import time
a = int(input("计算模式:1.+ 2.- 3.* 4./ 5.^ 6.向下舍入/(已禁用) 请输入序号"))
if a == 1:
a1 = float(input("加数1"))
a2 = float(input("加数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1+a2
print("检查中。。。")
if ((a12-a2) == a1) and ((a12-a1) == a2):
time.sleep(0.01)
print(a1,"+",a2,"=",a12)
else:
print("检验失败。")
if a == 2:
a1 = float(input("减数1"))
a2 = float(input("减数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1-a2
print("检查中。。。")
if ((a12+a2) == a1) and ((a12+a1) == a2):
time.sleep(0.01)
print(a1,"-",a2,"=",a12)
else:
print("检验失败。")
if a == 3:
a1 = float(input("乘数1"))
a2 = float(input("乘数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1*a2
print("检查中。。。")
if ((a12//a2) == a1) and ((a12//a1) == a2):
time.sleep(0.01)
print(a1,"*",a2,"=",a12)
else:
print("检验失败。")
if a == 4:
print("注意!!!该模式不会进行检查!!!")
a1 = float(input("浮点数除数1"))
a2 = float(input("浮点数除数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1/a2
time.sleep(0.01)
print(a1,"*",a2,"=",a12)
if a == 5:
print("注意!!!该模式不会进行检查!!!")
a1 = float(input("多少^n"))
a2 = float(input("n^多少"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1**a2
time.sleep(0.01)
print(a1,"^",a2,"=",a12)
def calcint():
import time
a = int(input("计算模式:1.+ 2.- 3.* 4./ 5.^ 6.向下舍入/ 请输入序号"))
if a == 1:
a1 = int(input("加数1"))
a2 = int(input("加数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1+a2
print("检查中。。。")
if ((a12-a2) == a1) and ((a12-a1) == a2):
time.sleep(0.01)
print(a1,"+",a2,"=",a12)
else:
print("检验失败。")
if a == 2:
a1 = int(input("减数1"))
a2 = int(input("减数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1-a2
print("检查中。。。")
if ((a12+a2) == a1) and ((a12+a1) == a2):
time.sleep(0.01)
print(a1,"-",a2,"=",a12)
else:
print("检验失败。")
if a == 3:
a1 = int(input("乘数1"))
a2 = int(input("乘数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1*a2
print("检查中。。。")
if ((a12//a2) == a1) and ((a12//a1) == a2):
time.sleep(0.01)
print(a1,"*",a2,"=",a12)
else:
print("检验失败。")
if a == 4:
print("注意!!!该模式不会进行检查!!!")
a1 = int(input("浮点数除数1"))
a2 = int(input("浮点数除数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1/a2
time.sleep(0.01)
print(a1,"*",a2,"=",a12)
if a == 5:
print("注意!!!该模式不会进行检查!!!")
a1 = int(input("多少^n"))
a2 = int(input("n^多少"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1**a2
time.sleep(0.01)
print(a1,"^",a2,"=",a12)
if a == 6:
print("注意!!!该模式计算不会检查!!!")
a1 = int(input("除数1"))
a2 = int(input("除数2"))
print("正在进行计算。。。")
time.sleep(0.01)
print("计算中。。。")
time.sleep(0.01)
a12 = a1//a2
time.sleep(0.01)
print(a1,"/",a2,"=",a12)
a = int(input("1:int 2:float"))
if a == 1:
calcint()
else:
calcfloat()2020-07-02T13:09:05 点赞:1