Python game - pick up apple

Today is also a day full of vitality. Every day for the whole small game, the boss is stunned by the strength and technology.

direct effect

game material

1. Background image

2. Basket

3. Apple

code

"""
   接苹果小游戏,本程序实现手动控制帧率
   Sprite类是继承自Turtle的一个类,所以归于海龟画图。
"""

 1. New screen

from sprites import *

screen = Screen()                        # 新建屏幕
screen.tracer(0,0)                       # 追踪命令                  
screen.setup(800,500)

 2. Import pictures

screen.bgpic('greenforest.png')

basket = Sprite('basket.png')

3. Property settings

counter = 0
fps = 60
start_time = time.perf_counter()

dynamic effect

1. Generate an apple

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. Mobile logic

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. Control frequency

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()

These small games written in Python are very simple. I wish you all a happy fishing experience. You can directly reply to [Apple] if you need the source code.

Guess you like

Origin blog.csdn.net/AI19970205/article/details/123535875