Python小游戏-接苹果

简介: Python小游戏-接苹果

今天也是元气满满的一天,每天整整小游戏,老板都被实力和技术我惊呆了

直接上效果

游戏素材
1.背景图

2.篮子

3.苹果

代码
"""
接苹果小游戏,本程序实现手动控制帧率
Sprite类是继承自Turtle的一个类,所以归于海龟画图。
"""
1.新建屏幕
from sprites import *

screen = Screen() # 新建屏幕
screen.tracer(0,0) # 追踪命令
screen.setup(800,500)
2.导入图片
screen.bgpic('greenforest.png')

basket = Sprite('basket.png')
3.属性设置
counter = 0
fps = 60
start_time = time.perf_counter()
动态效果
1.产生一个苹果
while 1:

if random.randint(1,10)==1:          # 产生一个苹果
    x = random.randint(-380,380)
    y = 400
    a = Sprite('apple.png',pos=(x,y),tag='apple')        
    a.scale(max(0.5,random.random()))

2.移动逻辑
for apple in screen.turtles():

if apple.get_tag()!= 'apple':continue      
apple.move(0,-5)                   # 在水平和垂直方向移动
if apple.collide(basket):
    apple.remove()                 # 移除苹果
    counter += 1                   # 接到苹果了进行统计
    continue
if apple.ycor() < -250:apple.remove()

3.控制频率
mx,my = mousepos() # 获取鼠标指针的x,y坐标
basket.goto(mx,-180)
screen.update()
screen.title('大海老师接苹果游戏,已接到:' + str(counter) + '个苹果')

以下代码实现手动控制帧率为60

end_time = time.perf_counter()
if end_time - start_time < 1/fps:

time.sleep(1/fps - (end_time - start_time))

start_time = time.perf_counter()
这些用Python编写的小游戏很简单,祝大家摸鱼快乐,需要源码直接回复【苹果】即可

目录
相关文章
|
21天前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
1月前
|
Python
python小游戏7
python小游戏7
|
1月前
|
Python
python小游戏6
python小游戏6
|
1月前
|
Python
python小游戏5
python小游戏5
|
1月前
|
Python
python小游戏4
python小游戏4
|
1月前
|
Python
python小游戏1
python小游戏1
|
1月前
|
安全 C++ Python
小游戏实战-Python实现石头剪刀布+扫雷小游戏
小游戏实战-Python实现石头剪刀布+扫雷小游戏
35 0
|
1月前
|
数据可视化 安全 数据安全/隐私保护
使用Python做个可视化的“剪刀石头布”小游戏
使用Python做个可视化的“剪刀石头布”小游戏
29 0
|
1月前
|
Python
python小游戏3
python小游戏3
|
1月前
|
算法 编译器 C语言
C语言猜数字小游戏(也包含python实现的用法)
本文基于VS2022、pycharm和前面的知识,写一个凭借分支与循环的小游戏,比如: 写一个猜数字游戏 游戏要求: 电脑自动生成1~100的随机数 玩家猜数字,猜数的过程中,根据猜测数据的大小给出大了或小了的反馈,直到猜对,游戏结束 在pyhton中生成随机数是比较简单的,可以直接导入random的包,直接生成随机数,导致写猜数字小游戏变成了判读语句和循环语句嵌套就能写出来,所以我不做过多的介绍了,可以直接看后面的代码展示,想了解更多的python可看python的基础知识,这里面有我在学习python的过程中写的笔记
30 0