python奥特曼打怪兽gameturtle版

python奥特曼打怪兽gameturtle版

python奥特曼打怪兽gameturtle版

"""
  奥特曼打怪兽_按键.py
  本程序设计了Background类,
  还设计了继承自Sprite类的Actor类及Monster类。
  为了让奥特曼能发射子弹,增加了按f键发红色小方块的功能。  
"""
import glob
import time
from tkinter import *
from PIL import Image
from random import randint
from gameturtle import Sprite,group

class Background:
     def __init__(self,cv,image):
         """image是磁盘上的一张图片"""
         self.w = int(cv.cget('width'))
         self.h = int(cv.cget('height'))
         self.cv = cv
         image = Image.open(image)
         im = image.convert("RGBA")
         self.pic1 = Sprite(cv,im)
         self.pic2 = Sprite(cv,im,pos=(1.5*self.w,self.h/2))            
    
class Actor(Sprite):
    def __init__(self,canvas,frames,pos=None,
                 visible=True,heading=0,tag='sprite'):
        Sprite.__init__(self,canvas=canvas,frames=frames,pos=pos,
                        visible=visible,heading=heading,tag=tag)
        self.dx = 0                  # 水平速度
        self.dy = 0                  # 垂直速度
        self._canvas.bind("",self.moveback)
        self._canvas.bind("",self.moveup)
        self._canvas.bind("",self.movedown)
        self._canvas.bind("",self.movefd)
        self._canvas.bind("",self.movexstop)
        self._canvas.bind("",self.movexstop)        
        self._canvas.bind("",self.moveystop)
        self._canvas.bind("",self.moveystop)
       
    def movexstop(self,event):         
         self.dx = 0
         
    def moveystop(self,event):
         self.dy = 0
         
    def moveback(self,event):
          self.dx = -4
          
    def movefd(self,event):
          self.dx = 4
          
    def moveup(self,event):
          self.dy = -4
          
    def movedown(self,event):
          self.dy = 4          

         
class Monster(Sprite):     
    def __init__(self,canvas,frames,pos=None,
                 visible=False,heading=0,tag='monster'):
        Sprite.__init__(self,canvas=canvas,frames=frames,pos=pos,
                        visible=visible,heading=heading,tag=tag)
        y = randint(0,360)
        self.goto(self._cv_width+100,y)
        self.randomshape()               # 随机造型
        self.setrotmode(1)               # 旋转模式为左右翻转  
        self.right(180)                  # 向右转180度   
        self.show()                      # 显示
        
if __name__ == '__main__':
    
    root = Tk()
    root.title('奥特曼的诞生')

    cv = Canvas(width=480,height=360)
    cv.pack()                            # 放置画布
    cv.focus_force()                     # 设置画布焦点

    bg = Background(cv,'1.png')

    manpic = Image.open('奥特曼.png')    # 奥特曼的造型图
    sp = ultraman = Actor(cv,manpic)     # 实例化奥特曼

    monster_pics = glob.glob('res/*.png')# 怪兽造型图列表
    monster_pics = [Image.open(im) for im in monster_pics]
    c = 0
    zd_pic = Image.new("RGBA",(10,10),color='red')   # 子弹图
    cv.bind("",lambda event:Sprite(cv,zd_pic,pos=sp.pos(),tag='bullet'))
    while True:
        bg.pic1.addx(-10)             # 背景图片1的x坐标减小 
        if bg.pic1.xcor() <= -bg.w/2:
           bg.pic1.setx(1.5*bg.w) 
        bg.pic2.addx(-10)             # 背景图片2的x坐标减小  
        if bg.pic2.xcor() <= -bg.w/2:
           bg.pic2.setx(1.5*bg.w)
        c = c + 1
        if c%100==0:Monster(cv,monster_pics)             
        ms = group(cv,'monster')
        [m.fd(2) for m in ms]
        [m.kill() for m in ms if m.xcor()<0 or m.collide_tag('bullet')]

        ultraman.addx(sp.dx)
        ultraman.addy(sp.dy)         
        if ultraman.collide_tag('monster'):sp.dy=10

        bullets = group(cv,'bullet')       
        [b.fd(10) for b in bullets]
        [b.kill() for b in bullets if b.xcor()>480]
        
        cv.update()                   # 刷新画布显示
        time.sleep(0.01)              # 等待0.01秒


需要全部源代码和素材请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

李兴球的博客是Python创意编程原创博客