把小游戏又修改了一下,练习减法

Words
599
Reading
3 min
5y
Listen to this post
~4 min

动画.gif

之前用pygame做了一个小游戏给小宝贝练习加法。现在进入减法的学习阶段,减法需要逆向思维,对小孩来说比加法更难理解一些。也需要更多的练习来熟悉,就在原来程序的基础上修改了一下。现在可以练习减法。

一、编程目标

  • 1、将原来的加法练习修改为减法。
  • 2、增加奖惩机制,左上方有三个头像,打错一题会掉一个头像,三个头像掉完,游戏结束。右上方显示得分,答对一题奖励一分。

二、编程思路

  • 1、生成题目
    先随机生成3个10以内的整数作为备选答案,然后从这三个数中随机挑选一个作为答案也就是两个数的差。再随机生成一个整数作为减数,差和减数相加得到被减数,在出题的地方列出算式。

    为了限制难度,用while循环检查被减数,如果大于10就重新生成一遍被减数、减数和差。不过实际运行的时候发现,这有时会引起一个问题,就是,选项可能会出现两个相同的答案。正在想办法解决。
    代码:

def update_question(pre_idx=0):
    answers=random.sample(range(1,11),3)
    brick1.answer_number=answers[0]
    brick2.answer_number=answers[1]
    brick3.answer_number=answers[2]
    brick_list=[brick1,brick2,brick3]
    idx=random.sample(range(0,3),1)[0]
    while idx == pre_idx:
        idx=random.sample(range(0,3),1)[0]
    sub_num[1]=random.randint(0,answers[idx])
    sub_num[0]=answers[idx]+sub_num[1]
    while sub_num[0] > 10:
        answers[idx]=random.sample(range(1,11),1)[0]
        brick_list[idx].answer_number=answers[idx]
        sub_num[0]=answers[idx]+sub_num[1]
    QuestionBanner.image.fill((0,0,255))
    QuestionBanner.text = str(sub_num[0])+'-'+str(sub_num[1])+'=?'  

2、生命计数
建立一个生命计数的类(life_count),类的功能就是载入头像图片,然后创立三个实例,加入all_sprites组里面。如果答错,就将一个实例移出all_sprites组。这样对应的头像就会从屏幕上消失。
设置了一个变量叫“可以掉命”(can_lose_life),如果这个参数为真并且跳上了错误的数字平台,就会有一个头像消失。如果不用这个参数,由于程序主体没秒钟执行30次。在下落过程中,三个头像就全部掉完了。所以掉一个头像之后,要把这个变量设置为假,这样一次就只掉一个头像。另外经过多次试验,当人物落到答案数字平台下方之后,应该把can_lose_life变量再次设置为真。
========================
全部源代码:

# -*- coding: utf-8 -*-

import pygame
import random

gravity = 1.2
support = 0

pygame.init()
screen = pygame.display.set_mode((400,800))#,pygame.FULLSCREEN|pygame.SCALED)


background = pygame.image.load('image\Background.jpg')
background = pygame.transform.smoothscale(background,(400,800))


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('image\player.jpg').convert()
        pygame.Surface.set_colorkey(self.image,(0,0,0))
        self.image = pygame.transform.smoothscale(self.image,(80,90))
        self.rect=self.image.get_rect()
        self.rect.center =(200,700)
        self.vel_x = 0
        self.vel_y = 0
        self.jumping = False
        self.last_update = 0

    def update(self):
        global gravity
        global support
        self.vel_y += gravity+support
        self.rect=self.rect.move(self.vel_x,self.vel_y)
        if self.rect.right < 0:
            self.rect.left = 400

        if self.rect.left > 400:
            self.rect.right = 0

        if self.rect.bottom >= 725:
            self.rect.bottom =725
            support = -1*gravity
            self.jumping = False

        else:
            support = 0

        if support == 0:
            self.jumping = True
class life(pygame.sprite.Sprite):
    def __init__(self,path2img,center_x,center_y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(path2img).convert()
        pygame.Surface.set_colorkey(self.image,(0,0,0))
        self.image = pygame.transform.smoothscale(self.image,(80,80))
        self.rect=self.image.get_rect()
        self.score=0
        self.rect.center=(center_x,center_y)

    def update(self):
        pass


class brick(pygame.sprite.Sprite):
    def __init__(self,center_x,center_y,answer_number,idx):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50,50))
        self.image.fill((255,0,0))
        self.rect=self.image.get_rect()
        self.rect.center =(center_x,center_y)
        self.font =  pygame.font.Font('segoeprb.ttf',32)
        self.answer_number = answer_number
        self.idx = idx

    def update(self):
        self.image.fill((255,0,0))
        choice_number = self.font.render(str(self.answer_number),True,(0,255,0))
        self.image.blit(choice_number,(5,-10))

class question_banner(pygame.sprite.Sprite):
    def __init__(self,center_x,center_y,question):
        pygame.sprite.Sprite.__init__(self)
        self.text = question
        self.font =  pygame.font.Font('segoeprb.ttf',64)
        self.image = pygame.Surface((300,80))
        self.image.fill((0,0,255))
        self.rect=self.image.get_rect()
        self.rect.center =(center_x,center_y)

    def update(self):
        question_img = self.font.render(self.text,True,(255,0,0))
        self.image.blit(question_img,(10,-10))

class Score(pygame.sprite.Sprite):
    def __init__(self,center_x,center_y,score):
        pygame.sprite.Sprite.__init__(self)
        self.text = score
        self.font =  pygame.font.Font('segoeprb.ttf',64)
        self.image = pygame.Surface((80,80))
        self.image.fill((0,0,255))
        self.rect=self.image.get_rect()
        self.rect.center =(center_x,center_y)
    
    def update(self):
        score_img = self.font.render(self.text,True,(255,0,0))
        self.image.blit(score_img,)

    def update(self):
        question_img = self.font.render(self.text,True,(255,0,0))
        self.image.blit(question_img,(10,-10))



class contral(pygame.sprite.Sprite):
    def __init__(self,image,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(image)
        self.image = pygame.transform.smoothscale(self.image,(60,40))
        self.rect = self.image.get_rect()
        self.rect.center = (x,y)
        
    def update(self):
        pass




sub_num=[2,1]
all_sprites= pygame.sprite.Group()
player = Player()
life1= life('image\Avatar.png',50,200)
life2= life('image\Avatar.png',110,200)
life3= life('image\Avatar.png',170,200)
life_count=[life1,life2,life3]
brick1  = brick(80,600,1,0)
brick2  = brick(200,600,2,1)
brick3  = brick(320,600,3,2)
QuestionBanner = question_banner(200,100,str(sub_num[0])+'-'+str(sub_num[1])+'=?') 
can_lose_life = True
score = 0
score_board = Score(300,200,str(score))
right_arrow = contral('image\Right.png',350,750)
left_arrow = contral('image\Left.png',50,750)
all_sprites.add(player,brick1,brick2,brick3,QuestionBanner,right_arrow,left_arrow,life1,life2,life3,score_board)

bricks = pygame.sprite.Group()
bricks.add(brick1,brick2,brick3)
clock = pygame.time.Clock()


def update_question(pre_idx=0):
    answers=random.sample(range(1,11),3)
    brick1.answer_number=answers[0]
    brick2.answer_number=answers[1]
    brick3.answer_number=answers[2]
    brick_list=[brick1,brick2,brick3]
    idx=random.sample(range(0,3),1)[0]
    while idx == pre_idx:
        idx=random.sample(range(0,3),1)[0]
    sub_num[1]=random.randint(0,answers[idx])
    sub_num[0]=answers[idx]+sub_num[1]
    while sub_num[0] > 10:
        answers[idx]=random.sample(range(1,11),1)[0]
        brick_list[idx].answer_number=answers[idx]
        sub_num[0]=answers[idx]+sub_num[1]
    QuestionBanner.image.fill((0,0,255))
    QuestionBanner.text = str(sub_num[0])+'-'+str(sub_num[1])+'=?'  

running = True

while running:
    clock.tick(30)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
        if event.type == pygame.MOUSEBUTTONDOWN and event.button ==1:
            if left_arrow.rect.collidepoint(event.pos):
                player.vel_x -= 5
            elif right_arrow.rect.collidepoint(event.pos):
                player.vel_x = 5

            if player.rect.collidepoint(event.pos):
                player.vel_y = -20
        elif event.type == pygame.MOUSEBUTTONUP:
            player.vel_x = 0

    #update
    all_sprites.update()
    hits = pygame.sprite.spritecollide(player,bricks,False)
    if not hits:
        ready2change_question = True
    if hits and player.vel_y > 0:
        player.last_update = pygame.time.get_ticks()
    if not hits and player.vel_y>0 and player.rect.top>625:
        can_lose_life = True

    if hits and player.vel_y >= 0: 
        if sub_num[0]-sub_num[1]==hits[0].answer_number:
            player.vel_y = 0
            player.rect.bottom = hits[0].rect.top+1
            support = -1*gravity
            player.jumping = False
            now = pygame.time.get_ticks()
            if ready2change_question and now - player.last_update>1500:
                update_question(hits[0].idx)
                ready2change_question = False
                score += 1
                score_board.image.fill((0,0,255))
                score_board.text = str(score)
                print(score)
                can_lose_life = False
        elif can_lose_life:
            all_sprites.remove(life_count[-1])
            life_count.pop()
            can_lose_life = False

    #render
    screen.blit(background,(0,0))
    all_sprites.draw(screen)


    pygame.display.update()
把小游戏又修改了一下,练习减法 | Ecency