Python单击球游戏turtle创意编程

Python单击球游戏turtle创意编程

Python击球游戏turtle创意编程

Python击球游戏turtle创意编程

"""本程序会自动生成一些小球,单击它们会消失,请设计策略让游戏结束"""

import os,sys
from turtle import *
from random import randint,choice
from time import sleep

class Ball(Turtle):
    clicks = 0
    containers = []
    def __init__(self,image,sound):
        Turtle.__init__(self,visible=False)         # 调用Turtle的初始化方法
        self.shape(image)                           # 设定形状
        self.sound = sound                          # 音效
        self.penup()                                # 抬笔
        self.speed(0)                               # 速度为最快
        self.dead = False                           # 标志死亡的逻辑变量
        self.screen_width = self.screen.window_width()
        self.screen_height = self.screen.window_height()
        x = randint(-100,100)
        y = randint(-100,100)

   def move(self):
       """ 让小球移动"""       
       self.fd(2)

    def die(self,x,y):
        self.dead = True
        self.hideturtle()

if __name__ == "__main__":

   game_start = True
   gametitle = "单击球小游戏"
   pygame_exist = False         # 标识pygame存不存在的逻辑变量
   width,height = 480,360
   try:
      import pygame
      pygame_exist = True   
   except:
      print("pygame模块没有正确安装。\n请在命令提示符下输入:'pip install -U pygame --user'进行安装。")

   if pygame_exist:
      pygame.mixer.init()
      #音乐文件= "My Musicfmusic1.wav"
      #pygame.mixer.music.load(音乐文件)
      #pygame.mixer.music.play(-1,0) 
      pop =pygame.mixer.Sound("pop.wav")

   screen =Screen()
   screen.setup(width,height)
   screen.bgpic("slopes.gif")
   screen.title(gametitle)
   
   balls = ["ball-a.gif","ball-b.gif","ball-c.gif","ball-d.gif","ball-e.gif"]
   ...................    
   screen.mainloop()

下载完整源代码与素材,请

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

李兴球

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

评论已关闭。