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编写的小游戏很简单,祝大家摸鱼快乐,需要源码直接回复【苹果】即可

猜你喜欢

转载自blog.csdn.net/AI19970205/article/details/123535875