猫史档案馆


【Python作品分享】main【作品秀】

用户:BenmzXnBenmzXn查看:0 回复:0 评论:0 创建时间:2023-08-11T16:38:10


【作品展示】

center_image

 

【作品介绍】

这是一个口算大战

 

【作品源代码】

import pgzrun
import random
import time

WIDTH = 480
HEIGHT=700
TITLE = '口算练习-飞机大战版'
bgHeight=852
isLosse = False
score=0

#背景
background1 = Actor('background')
background2 = Actor('background')
background1.y=bgHeight/2;
background2.y=-bgHeight/2;

queBackground = Actor('timubj')

#玩家飞机
hero=Actor('hero')
hero.x = WIDTH/2
hero.y = HEIGHT/2
#子弹
bullet=Actor('bullet')
bullet.x=WIDTH/2
bullet.y=-30
#敌机
enemy1=Actor('enemy')
enemy2=Actor('enemy')
enemy3=Actor('enemy')
answer1='11'
answer2 = '22'
answer3 = '33'


def initEnemy():
    enemy1.x = 100
    enemy2.x = 250
    enemy3.x = 400
    enemy1.y = 100
    enemy2.y = 100
    enemy3.y = 100
    bullet.y = -30


question=''
op='+'
result=0
answerIndex=1
tip=''
def getQuestion():
    global op, question,result,answer1,answer2,answer3,answerIndex
    opindex = random.randint(0, 1)
    if(opindex==0):
        op='+'
    else:
        op = '-'
    data1 = random.randint(0, 100)
    if(op=='-'):
        data2=random.randint(0,data1)
        result=data1-data2
    else:
        data2=random.randint(0,100)   
        result=data1+data2     
    question=str(data1)+op+str(data2)+"=?"
    if(random.randint(0,1)==0):
        resultErr1 = result+random.randint(1, 20)
    else:
        resultErr1 = result-random.randint(1, 20)
    if(random.randint(0,1)==0):
        resultErr2 = result+random.randint(1, 20)
    else:
        resultErr2 = result-random.randint(1, 20) 
    answerIndex=random.randint(1,3)
    if(answerIndex==1):
        answer1=str(result)
        answer2=str(resultErr1)
        answer3=str(resultErr2)
    elif(answerIndex==2):
        answer2=str(result)
        answer1=str(resultErr1)
        answer3=str(resultErr2)
    elif(answerIndex==3):
        answer3=str(result)
        answer1=str(resultErr1)
        answer2=str(resultErr2)
    tip=''
    initEnemy()


getQuestion()

def draw():
     
    background1.draw()
    background2.draw()
    queBackground.draw()
    hero.draw()
    bullet.draw()
    enemy1.draw()
    enemy2.draw()
    enemy3.draw()

    screen.draw.text(question,(40,10),fontsize=50,fontname='s', color='red')
    screen.draw.text(answer1,(enemy1.x-20,enemy1.y+20),fontsize=30,fontname='s',color='yellow')
    screen.draw.text(answer2,(enemy2.x-20,enemy2.y+20),fontsize=30,fontname='s',color='yellow')
    screen.draw.text(answer3,(enemy3.x-20,enemy3.y+20),fontsize=30,fontname='s',color='yellow')

    screen.draw.text('得分:'+str(score), (200,HEIGHT-50),fontsize=30, fontname='s', color='black')
    if(tip=='答对啦'):
        screen.draw.text(tip, (200, HEIGHT-100), fontsize=30,fontname='s', color='red')
    else:
        screen.draw.text(tip, (200, HEIGHT-100), fontsize=30,fontname='s', color='black')
    screen.draw.text('作者:二年8班 刘哲安', (270,60), fontsize=20,fontname='s', color='black')

def update():
    global score,tip
    if background1.y>bgHeight/2+bgHeight:
        background1.y=-bgHeight/2
    if background2.y>bgHeight/2+bgHeight:
        background2.y=-bgHeight/2    
    background1.y+=1
    background2.y+=1

    if bullet.y>-30:
        bullet.y-=10
    
    #飞机下落
    enemy1.y+=1;
    enemy2.y+=1;
    enemy3.y+=1;
    if(enemy1.y>HEIGHT or  enemy2.y>HEIGHT or enemy3.y>HEIGHT):
       getQuestion()
    if bullet.colliderect(enemy1):
        if(answerIndex==1):
            tip='答对啦'
            score+=1
        else:
            tip='答错啦'       
        getQuestion()         
    if bullet.colliderect(enemy2):
        if(answerIndex == 2):
            tip = '答对啦'
            score += 1
        else:
            tip = '答错啦'       
        getQuestion()
    if bullet.colliderect(enemy3):
        if(answerIndex == 3):
            tip = '答对啦'
            score += 1
        else:
            tip = '答错啦'        
        getQuestion()

def on_mouse_move(pos,rel,buttons):
    hero.x=pos[0]
    hero.y=pos[1]

def on_mouse_down():
    bullet.x=hero.x
    bullet.y=hero.y-70


pgzrun.go()



 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页