单词炮弹.py (操作大炮打英文单词)

"""本版本新增Sprite类,它定义可旋转360度的角色类,继承自海龟类,使用pillow模块让它旋转360次"""

import os
import sys 
import math
import time
import tempfile
from PIL import Image
from turtle import Turtle,Screen
from bullet import *               # 导入子弹类
from sprite import *               # 导入角色类,建大炮
from word import *                 # 导入单词类
from explosion import *            # 导入爆炸类

def get_mouse_xy():
    """获取鼠标指针的坐标"""
    mousex = Sprite.mousex
    mousey = Sprite.mousey
    return mousex,mousey

def shoot(x,y):
    speed = 10
    position = (x,y)
    image = "bullet.png"
    angle =  sprite.heading()
    Bullet(position,speed,angle,image,bullet_group)

def check_collide(word_group,bullet_group):
    """碰撞检测"""
    global score
    removed_word = set()
    removed_bullet = set()
    wg = word_group.copy()       # 由于在迭代的过程中可能会改变集合所以用副本
    bg = bullet_group.copy()
    for word in wg:
        for bullet in bg:
            if bullet.distance(word) < 100 and bullet.isvisible() and word.isvisible():
                pos = word.position()
                explosion(pos,explosion_images)   # 爆炸效果
                bullet.ht()                       # 隐藏弹药
                word.ht()                         # 隐藏单词
                if word.isgood == True:            # 翻译正确的加10分
                    score +=10
                else:                             # 否则减10分
                    score -=10
                screen.title("得分:" + str(score))
                removed_word.add(word)            # 放到待移除集合
                removed_bullet.add(bullet)        # 放到待移除集合
    removed_word.clear()
    removed_bullet.clear()
       
if __name__ == "__main__":

    score = 0                       # 得分
    explosion_images = glob("explosion/*.gif")
    sprite_image = "大炮.png"       # 大炮图
    word_group = set()
    bullet_group = set()
    
    screen = Screen()
    screen.delay(0)
    screen.setup(800,600)
    screen.colormode(255)
    screen.bgcolor((4,21,104))
    [screen.addshape(image) for image in explosion_images]
    
    sprite = Sprite(sprite_image)   # 新建角色
    sprite.penup()
    screen.onclick(shoot)           # 单击发射子弹
    from wordict import *           # 导入wordict字典
    begin_time = time.time()        # 起始时间
    while True:
       check_collide(word_group,bullet_group)
       if time.time() - begin_time > 2:
           string = list(wordict.keys())
           string = choice(string)        
           Word((0,0),string,wordict[string],word_group) # 生成单词
           begin_time = time.time()
            
       sprite.facemouse()
       x ,y = get_mouse_xy()
       if x ==None and y == None :screen.update();continue
       if sprite.distance(get_mouse_xy())>50:
          sprite.fd(1)
    
    screen.listen()
    screen.mainloop() 


    

单词炮弹作者李兴球操作大炮打英文单词

以下是Sprite类 ,用pillow模块突破海龟画图像块里当海龟造型为图片时不能旋转的“缺憾”:

You must log in to read the rest of this article. Please log in or register as a user.