python代码收集

 

目录

五角星

五角星2

六星

 太阳花

画圈圈

画五角星

六行代码画玫瑰 

玫瑰

冰墩墩

飞机大战游戏


五角星

import turtle as t
t.goto(100,0)
for i in range(50):
    t.left(80)
    t.fd(100)
    t.left(135)
    t.fd(105)

五角星2

import turtle as t
t.goto(100,0)
for i in range(50):
        t.left(80)
        t.fd(100)
        t.left(135)
        t.fd(120)

六星

import turtle as t

# size传入最大的圆的半径
def draw_Star(size):
    t.setup(1200, 1000)
    t.speed(11)
    t.pensize(2)
    t.pencolor('grey')
    for r, pen_size in [(size//7, 3), (size*6//7, 4), (size, 4)]:
        t.penup()
        t.goto(0, -r)
        t.pendown()
        t.pensize(pen_size)
        t.circle(r)
        t.penup()
        t.goto(0, 0)
    r = size * 5.5 // 7
    for i in range(6):
        t.pendown()
        t.pensize(3)
        t.seth(i * 60 + 30)
        t.fd(r)
        t.goto(0, 0)
    t.goto(0, r)
    t.seth(-150)
    t.pensize(5)
    for i in range(6):
        t.fd(r)
        t.left(60)
    r = r - size // 50
    t.goto(0, r)
    t.pensize(10)
    t.seth(-120)
    t.pencolor('orange')
    for i in range(3):
        t.fd(r * (3**0.5))
        t.left(120)
    t.penup()
    t.goto(0, 0)
    t.seth(30)
    t.fd(r)
    t.seth(-180)
    t.pendown()
    for i in range(3):
        t.fd(r * (3 ** 0.5))
        t.left(120)
    t.hideturtle()
    t.done()

draw_Star(400)

 太阳花

from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

画圈圈

import turtle

def draw_spiral(t, n, length=3, a=0.1, b=0.0002):
   #Draws an Archimedian spiral starting at the origin

   theta = 0.0

   for i in range(n):
      t.fd(length)
      dtheta = 1 / (a + b * theta)

      t.lt(dtheta)
      theta += dtheta

#create the world and bob
bob = turtle.Turtle()
draw_spiral(bob, n=1000)

turtle.mainloop()

画五角星

from turtle import *

fillcolor("red")

begin_fill()

while True:

    forward(200)

    right(144)

    if abs(pos()) < 1:

            break

end_fill()

六行代码画玫瑰 

import turtle as tt.color('red ')
for i in range(270) :
        t.fd(i)
        t.left(70)
t.done()



#第一行是导入海龟库,同时设置画笔名称为t
#第二行是设置画笔颜色为红色
#第三行是循环,括号中是循环次数(循环接下来的两行代码,因为这两行缩进了)
#第五行是前进,括号中填前进距离
#第六行是左转,括号填入左转角度
#第七行,结束绘画

玫瑰

*_*coding:utf8 *_*
import turtle
 
#设置初始位置
turtle.penup()
turtle.1eft(90)
turtle.fd(200)
turtle.pendown()
turtle.right(90)
 
#花蕊
turtle.fillcolor("red")
turtle.begin_fili()
turtle.circle(10,180)
turtle.circle(25,110)
turtle.left(50)
turtle.circle(60,45)
turtle.circle(20,170)
turtle.right(24)
turt1e.fd(30)
turtle.left(10)
turtle.circie(30,110)
turtle.fd(20)
turtle.left(40)
turtle.circle(90,70)
turtle.circle(30,150)
turtle.right(30)
turtle.fd(15)
turtle.circle(80,90)
 
turtle.left(15)
turtle.fd(45)
turtle.right(165)
turt1e.fd(20)
turtle.left(155)
turtle.circie(150,80)
turt1e.left(50)
turtle.circie(150,90)
turtle.end_fill()
 
#花瓣1
turtle.left(150)
turtle.circie(-90,70)
turt1e.left(20)
turtle.circle(75,105)
turtle.setheading(60)
turtle.circle(80,98)
turt1e.circle(-90,40)
 
#花瓣2
turt1e.left(180)
turtle.circle(90,40)
turtle.circle(-80,98)
turtle.setheading(-83)
 
#叶子1
turtle.fd(30)
turtle.left(90)
turtle.fd(25)
turtle.left(45)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(-80,90)
turtle.right(90)
turtle.circle(-80,90)
turtle.end_fill()
 
turtle.right(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(85)
turtle.left(90)
turtle.fd(80)
 
#叶子2
turtle.right(90)
turtle.right(45)
turtle.fii1color("green")
turtle.begin_fill()
turtle.circle(80,90)
turt1e.left(90)
turtle.circle(80,90)
turtle.end_fill()
turtle.left(135)
turtle.fd(60)
turtle.left(180)
turtle.fd(60)
turtle.right(90)
turtle.circle(200,60)
 
turtle.pencolor("red")
turtle.write("亲爱的!生日快乐!",font=("迷你繁篆书" ,10, " normal'))
turtle.done()

冰墩墩

import turtle
#名字
turtle.title('python')
#画笔速度
turtle.speed(10)

#左手
turtle.penup()
turtle.goto(177,112)
turtle.pencolor("lightgray")
turtle.pensize(3)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(80)
turtle.circle(-45,200)
turtle.circle(-300,23)
turtle.end_fill()

#左手内
turtle.penup()
turtle.goto(182,95)
turtle.pencolor("black")
turtle.pensize(1)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.setheading(95)
turtle.pendown()
turtle.circle(-37,160)
turtle.circle(-20,50)
turtle.circle(-200,30)
turtle.end_fill()

#轮廓、头顶
turtle.penup()
turtle.goto(-73,230)
turtle.pencolor("lightgray")
turtle.pensize(3)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(20)
turtle.circle(-250,35)

#轮廓、左耳
turtle.setheading(50)
turtle.circle(-42, 180)

#轮廓、左侧
turtle.setheading(-50)
turtle.circle(-190, 30)
turtle.circle(-320, 45)

#轮廓、左腿
turtle.circle(120, 30)
turtle.circle(200, 12)
turtle.circle(-18, 85)
turtle.circle(-180, 23)
turtle.circle(-20, 110)
turtle.circle(15, 115)
turtle.circle(100,12)

#轮廓、右腿
turtle.circle(15, 120)
turtle.circle(-15, 110)
turtle.circle(-150, 30)
turtle.circle(-15, 70)
turtle.circle(-150, 10)
turtle.circle(200, 35)
turtle.circle(-150,20)

#轮廓、右手
turtle.setheading(-120)
turtle.circle(50, 30)
turtle.circle(-35, 200)
turtle.circle(-300, 23)

#轮廓、右侧
turtle.setheading(86)
turtle.circle(-300, 26)

#轮廓、右耳
turtle.setheading(122)
turtle.circle(-53, 160)
turtle.end_fill()

#右耳内
turtle.penup()
turtle.goto(-130,180)
turtle.pencolor("black")
turtle.pensize(1)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(120)
turtle.circle(-28,160)
turtle.setheading(210)
turtle.circle(150,20)
turtle.end_fill()

#左耳内
turtle.penup()
turtle.goto(90,230)
turtle.setheading(40)
turtle.begin_fill()
turtle.pendown()
turtle.circle(-30,170)
turtle.setheading(125)
turtle.circle(150,23)
turtle.end_fill()

#右手内
turtle.penup()
turtle.goto(-180,-55)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.setheading(-120)
turtle.pendown()
turtle.circle(50,30)
turtle.circle(-27,200)
turtle.circle(-300,20)
turtle.setheading(-90)
turtle.circle(300,14)
turtle.end_fill()

#左腿内
turtle.penup()
turtle.goto(108,-168)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(-115)
turtle.circle(110,15)
turtle.circle(200,10)
turtle.circle(-18,80)
turtle.circle(-180,13)
turtle.circle(-20,90)
turtle.circle(15,60)
turtle.setheading(42)
turtle.circle(-200,29)
turtle.end_fill()

#右腿内
turtle.penup()
turtle.goto(-38,-210)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(-155)
turtle.circle(15,100)
turtle.circle(-10,110)
turtle.circle(-100,30)
turtle.circle(-15,65)
turtle.circle(-100,10)
turtle.circle(200,15)
turtle.setheading(-14)
turtle.circle(-200,27)
turtle.end_fill()

#右眼眼圈
turtle.penup()
turtle.goto(-64,120)
turtle.begin_fill()
turtle.pendown()
turtle.setheading(40)
turtle.circle(-35,152)
turtle.circle(-100,50)
turtle.circle(-35,152)
turtle.circle(-100,50)
turtle.circle(-35,130)
turtle.circle(-100,50)
turtle.end_fill()

#右眼眼珠
turtle.penup()
turtle.goto(-47, 55)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(25, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(-45, 62)
turtle.pencolor("darkslategray")
turtle.fillcolor("darkslategray")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(19, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(-45, 68)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(10, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(-47, 86)
turtle.pencolor("white")
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(5, 360)
turtle.end_fill()

#左眼眼圈
turtle.penup()
turtle.goto(51,82)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(120)
turtle.circle(-32,152)
turtle.circle(-100,55)
turtle.circle(-25,120)
turtle.circle(-120,45)
turtle.end_fill()

#左眼眼珠
turtle.penup()
turtle.goto(79, 60)
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(24, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(79, 64)
turtle.pencolor("darkslategray")
turtle.fillcolor("darkslategray")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(19, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(79, 70)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(10, 360)
turtle.end_fill()
turtle.penup()
turtle.goto(79, 88)
turtle.pencolor("white")
turtle.fillcolor("white")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(0)
turtle.circle(5, 360)
turtle.end_fill()

#鼻子
turtle.penup()
turtle.goto(37,80)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.circle(-8,130)
turtle.circle(-22,100)
turtle.circle(-8,130)
turtle.end_fill()

#嘴
turtle.penup()
turtle.goto(-15,48)
turtle.setheading(-36)
turtle.begin_fill()
turtle.pendown()
turtle.circle(60,70)
turtle.setheading(-132)
turtle.circle(-45,100)
turtle.end_fill()

#彩虹圈
turtle.penup()
turtle.goto(-135, 120)
turtle.pensize(5)
turtle.pencolor("cyan")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-165, 150)
turtle.circle(-130, 78)
turtle.circle(-250, 30)
turtle.circle(-138, 105)
turtle.penup()
turtle.goto(-131, 116)
turtle.pencolor("slateblue")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-160, 144)
turtle.circle(-120, 78)
turtle.circle(-242, 30)
turtle.circle(-135, 105)
turtle.penup()
turtle.goto(-127, 112)
turtle.pencolor("orangered")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-155, 136)
turtle.circle(-116, 86)
turtle.circle(-220, 30)
turtle.circle(-134, 103)
turtle.penup()
turtle.goto(-123, 108)
turtle.pencolor("gold")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-150, 136)
turtle.circle(-104, 86)
turtle.circle(-220, 30)
turtle.circle(-126, 102)
turtle.penup()
turtle.goto(-120, 104)
turtle.pencolor("greenyellow")
turtle.pendown()
turtle.setheading(60)
turtle.circle(-145, 136)
turtle.circle(-90, 83)
turtle.circle(-220, 30)
turtle.circle(-120, 100)
turtle.penup()

#爱心
turtle.penup()
turtle.goto(220,115)
turtle.pencolor("brown")
turtle.pensize(1)
turtle.fillcolor("brown")
turtle.begin_fill()
turtle.pendown()
turtle.setheading(36)
turtle.circle(-8,180)
turtle.circle(-60,24)
turtle.setheading(110)
turtle.circle(-60,24)
turtle.circle(-8,180)
turtle.end_fill()

#最后一步,五环
turtle.penup()
turtle.goto(-5,-170)
turtle.pendown()
turtle.pencolor("blue")
turtle.circle(6)
turtle.penup()
turtle.goto(10,-170)
turtle.pendown()
turtle.fillcolor("black")
turtle.circle(6)
turtle.penup()
turtle.goto(25,-170)
turtle.pendown()
turtle.pencolor("brown")
turtle.circle(6)
turtle.penup()
turtle.goto(2,-175)
turtle.pendown()
turtle.pencolor("lightgoldenrod")
turtle.circle(6)
turtle.penup()
turtle.goto(16,-175)
turtle.pendown()
turtle.pencolor("green")
turtle.circle(6)
turtle.penup()

飞机大战游戏

  01-创建窗口

import pygame
import time
from pygame.locals import *

# 飞机类
class HeroPlan(object):
    def __init__(self,screen):
        self.x = 150
        self.y = 580
        self.screen = screen
        self.image = pygame.image.load('./feiji/hero1.png')

    # 显示我方飞机
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    # move_left
    def move_left(self):
        self.x -= 5
    # move_right
    def move_right(self):
        self.x += 5

# 键盘控制
def key_contrl(hero):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                hero.move_right()

# 主函数
def main():
    # 创建窗口
    screen = pygame.display.set_mode((394,700),0,32)
    # 创建背景图片
    background = pygame.image.load('./feiji/background.png')
    # 创建飞机
    hero = HeroPlan(screen)

    while True:
        # 设置背景图片
        screen.blit(background,(0,0))

        # 显示飞机
        hero.display()

        # 键盘控制
        key_contrl(hero)

        pygame.display.update()

        time.sleep(0.01)

if __name__ == '__main__':
    main()

02-面向对象修改

import pygame
import time
from pygame.locals import *

# 飞机类
class HeroPlan(object):
    def __init__(self,screen):
        self.x = 150
        self.y = 580
        self.screen = screen
        self.image = pygame.image.load('./feiji/hero1.png')

    # 显示飞机
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        # screen.blit(hero, (x, y))

    # 移动飞机
    def move_left(self):
        # x -= 5
        self.x -= 5
    def move_right(self):
        self.x += 5

# 键盘控制
def key_contro(hero):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                # x -= 5
                hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                # x += 5
                hero.move_right()
            

# 主函数
def main():
    # 创建窗口
    screen = pygame.display.set_mode((394,700),0,32)
    # 创建背景图片
    background = pygame.image.load('./feiji/background.png')
    # 创建飞机
    hero = HeroPlan(screen)
    while True:
        # 设置背景图片
        screen.blit(background,(0,0))
        # 显示飞机
        hero.display()
        # 键盘控制
        key_contro(hero)

        pygame.display.update()

        time.sleep(0.01)

if __name__ == '__main__':
    main()

03-发射子弹

import pygame
import time
from pygame.locals import *

# 飞机类
class HeroPlan(object):
    def __init__(self,screen):
        self.x = 150
        self.y = 580
        self.screen = screen
        self.image = pygame.image.load('./feiji/hero1.png')
        self.bullet_list = []
    # 显示飞机
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        # screen.blit(hero, (x, y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()

    # 移动飞机
    def move_left(self):
        # x -= 5
        self.x -= 5
    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))





# 子弹类
class Bullet(object):
    def __init__(self,screen,x,y):
        self.x = x+30
        self.y = y-20
        self.screen = screen
        self.image = pygame.image.load('./feiji/bullet.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5


# 键盘控制
def key_contro(hero):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                # x -= 5
                hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                # x += 5
                hero.move_right()
            elif event.key == K_SPACE:
                print("--space--")
                # 发射子弹
                hero.fire()
            

# 主函数
def main():
    # 创建窗口
    screen = pygame.display.set_mode((394,700),0,32)
    # 创建背景图片
    background = pygame.image.load('./feiji/background.png')
    # 创建飞机
    hero = HeroPlan(screen)
    while True:
        # 设置背景图片
        screen.blit(background,(0,0))
        # 显示飞机
        hero.display()
        # 键盘控制
        key_contro(hero)

        pygame.display.update()

        time.sleep(0.01)

if __name__ == '__main__':
    main()

04-显示敌机

import pygame
import time
from pygame.locals import *


# 飞机类
class HeroPlan(object):
    def __init__(self,screen):
        self.x = 150
        self.y = 580
        self.screen = screen
        self.image = pygame.image.load('./feiji/hero1.png')
        self.bullet_list = []
    # 显示飞机
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        # screen.blit(hero, (x, y))
        for bullet in self.bullet_list:
            # 让我方飞机子弹显示
            bullet.display()
            # 让我方飞机子弹移动
            bullet.move()



    # 移动飞机
    def move_left(self):
        # x -= 5
        self.x -= 5
    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))


# 敌机类
class EnemyPlan(object):
    def __init__(self,screen):
        self.x = 0
        self.y = 0
        self.screen = screen
        self.image = pygame.image.load('./feiji/enemy0.png')
        self.direction = "right"

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5

        if self.x > 340:
            self.direction = "left"
        elif self.x<0:
            self.direction ="right"



# 子弹类
class Bullet(object):
    def __init__(self,screen,x,y):
        self.x = x+30
        self.y = y-20
        self.screen = screen
        self.image = pygame.image.load('./feiji/bullet.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5


# 键盘控制
def key_contro(hero):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                # x -= 5
                hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                # x += 5
                hero.move_right()
            elif event.key == K_SPACE:
                print("--space--")
                # 发射子弹
                hero.fire()
            

# 主函数
def main():
    # 创建窗口
    screen = pygame.display.set_mode((394,700),0,32)
    # 创建背景图片
    background = pygame.image.load('./feiji/background.png')
    # 创建飞机
    hero = HeroPlan(screen)

    # 创建敌方飞机
    enemy = EnemyPlan(screen)

    while True:
        # 设置背景图片
        screen.blit(background,(0,0))
        # 显示我方飞机
        hero.display()
        # 显示敌方飞机
        enemy.display()
        # 移动敌机
        enemy.move()
        # 键盘控制
        key_contro(hero)

        pygame.display.update()

        time.sleep(0.01)

if __name__ == '__main__':
    main()

05-删除越界子弹

import pygame
import time
from pygame.locals import *


# 飞机类
class HeroPlan(object):
    def __init__(self,screen):
        self.x = 150
        self.y = 580
        self.screen = screen
        self.image = pygame.image.load('./feiji/hero1.png')
        self.bullet_list = []
    # 显示飞机
    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        # screen.blit(hero, (x, y))
        for bullet in self.bullet_list:
            # 让我方飞机子弹显示
            bullet.display()
            # 让我方飞机子弹移动
            bullet.move()
            if bullet.judge():
                # 让子弹列表里面的元素被删除
                self.bullet_list.remove(bullet)


    # 移动飞机
    def move_left(self):
        # x -= 5
        self.x -= 5
    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(self.screen,self.x,self.y))


# 敌机类
class EnemyPlan(object):
    def __init__(self,screen):
        self.x = 0
        self.y = 0
        self.screen = screen
        self.image = pygame.image.load('./feiji/enemy0.png')
        self.direction = "right"

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5

        if self.x > 340:
            self.direction = "left"
        elif self.x<0:
            self.direction ="right"



# 子弹类
class Bullet(object):
    def __init__(self,screen,x,y):
        self.x = x+30
        self.y = y-20
        self.screen = screen
        self.image = pygame.image.load('./feiji/bullet.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5

    def judge(self):
        if self.y < 0:
            return True
        else:
            return False



# 键盘控制
def key_contro(hero):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                # x -= 5
                hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                # x += 5
                hero.move_right()
            elif event.key == K_SPACE:
                print("--space--")
                # 发射子弹
                hero.fire()
            

# 主函数
def main():
    # 创建窗口
    screen = pygame.display.set_mode((394,700),0,32)
    # 创建背景图片
    background = pygame.image.load('./feiji/background.png')
    # 创建飞机
    hero = HeroPlan(screen)

    # 创建敌方飞机
    enemy = EnemyPlan(screen)

    while True:
        # 设置背景图片
        screen.blit(background,(0,0))
        # 显示我方飞机
        hero.display()
        # 显示敌方飞机
        enemy.display()
        # 移动敌机
        enemy.move()
        # 键盘控制
        key_contro(hero)

        pygame.display.update()

        time.sleep(0.01)

if __name__ == '__main__':
    main()

06-敌方飞机发射子弹

import pygame
import time
import random
from pygame.locals import *



# 飞机类
class HeroPlan(object):
    def __init__(self, screen):
        self.x = 150
        self.y = 580
        self.screen = screen
        self.image = pygame.image.load('./feiji/hero1.png')
        self.bullet_list = []

    # 显示飞机
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        # screen.blit(hero, (x, y))
        for bullet in self.bullet_list:
            # 让我方飞机子弹显示
            bullet.display()
            # 让我方飞机子弹移动
            bullet.move()
            if bullet.judge():
                # 让子弹列表里面的元素被删除
                self.bullet_list.remove(bullet)

    # 移动飞机
    def move_left(self):
        # x -= 5
        self.x -= 5

    def move_right(self):
        self.x += 5

    def fire(self):
            self.bullet_list.append(Bullet(self.screen, self.x, self.y))

# 我方飞机子弹类
class Bullet(object):
    def __init__(self, screen, x, y):
        self.x = x + 30
        self.y = y - 20
        self.screen = screen
        self.image = pygame.image.load('./feiji/bullet.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y -= 5

    def judge(self):
        if self.y < 0:
            return True
        else:
            return False


# 敌机类
class EnemyPlan(object):
    def __init__(self, screen):
        self.x = 0
        self.y = 0
        self.screen = screen
        self.image = pygame.image.load('./feiji/enemy0.png')
        self.direction = "right"
        self.bullet_list = []

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        for bullet in self.bullet_list:
            # 让敌方飞机子弹显示
            bullet.display()
            # 让敌方飞机子弹移动
            bullet.move()
            if bullet.judge():
                # 让子弹列表里面的元素被删除
                self.bullet_list.remove(bullet)


    def move(self):
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5

        if self.x > 340:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"

    def fire(self):
        random_num = random.randint(1,100)
        if random_num == 28 or random_num == 78:
            self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))

# 敌机子弹类
class EnemyBullet(object):
    def __init__(self,screen,x,y):
        self.x = x
        self.y = y
        self.screen = screen
        self.image = pygame.image.load('./feiji/bullet1.png')

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))

    def move(self):
        self.y += 5

    def judge(self):
        if self.y > 700:
            return True
        else:
            return False


# 键盘控制
def key_contro(hero):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                # x -= 5
                hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                # x += 5
                hero.move_right()
            elif event.key == K_SPACE:
                print("--space--")
                # 发射子弹
                hero.fire()

# 主函数
def main():
    # 创建窗口
    screen = pygame.display.set_mode((394, 700), 0, 32)
    # 创建背景图片
    background = pygame.image.load('./feiji/background.png')
    # 创建飞机
    hero = HeroPlan(screen)

    # 创建敌方飞机
    enemy = EnemyPlan(screen)

    while True:
        # 设置背景图片
        screen.blit(background, (0, 0))
        # 显示我方飞机
        hero.display()
        # 显示敌方飞机
        enemy.display()
        # 移动敌机
        enemy.move()
        # 敌方飞机发射子弹
        enemy.fire()
        # 键盘控制
        key_contro(hero)

        pygame.display.update()

        time.sleep(0.01)

if __name__ == '__main__':
    main()

07-代码优化

import pygame
import time
import random
from pygame.locals import *


class Base(object):
    pass


# 飞机基类
class BasePlan(object):
    def __init__(self, screen, x, y, image_name):
        self.x = x
        self.y = y
        self.screen = screen
        self.image = pygame.image.load(image_name)
        self.bullet_list = []

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        # screen.blit(hero, (x, y))
        for bullet in self.bullet_list:
            # 让我方飞机子弹显示
            bullet.display()
            # 让我方飞机子弹移动
            bullet.move()
            if bullet.judge():
                # 让子弹列表里面的元素被删除
                self.bullet_list.remove(bullet)


# 子弹基类
class BaseBullet(object):
    def __init__(self, screen, x, y, image_name):
        self.x = x
        self.y = y
        self.screen = screen
        self.image = pygame.image.load(image_name)

    def display(self):
        self.screen.blit(self.image, (self.x, self.y))


# 我方飞机类
class HeroPlan(BasePlan):
    def __init__(self, screen):
        BasePlan.__init__(self, screen, 150, 580, './feiji/hero1.png')

    # 移动飞机
    def move_left(self):
        # x -= 5
        self.x -= 5

    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(self.screen, self.x, self.y))


# 我方飞机子弹类
class Bullet(BaseBullet):
    def __init__(self, screen, x, y):
        BaseBullet.__init__(self, screen, x + 30, y - 20, './feiji/bullet.png')

    def move(self):
        self.y -= 5

    def judge(self):
        if self.y < 0:
            return True
        else:
            return False


# 敌机类
class EnemyPlan(BasePlan):
    def __init__(self, screen):
        BasePlan.__init__(self, screen, 0, 0, './feiji/enemy0.png')
        self.direction = "right"

    def move(self):
        if self.direction == "right":
            self.x += 5
        elif self.direction == "left":
            self.x -= 5

        if self.x > 340:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"

    def fire(self):
        random_num = random.randint(1, 100)
        if random_num == 28 or random_num == 78:
            self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y))


# 敌机子弹类
class EnemyBullet(BaseBullet):
    def __init__(self, screen, x, y):
        BaseBullet.__init__(self, screen, x, y, './feiji/bullet1.png')

    def move(self):
        self.y += 5

    def judge(self):
        if self.y > 700:
            return True
        else:
            return False


# 键盘控制
def key_contro(hero):
    for event in pygame.event.get():
        # 判断是否是点击了退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
        # 判断是否是按下了键
        elif event.type == KEYDOWN:
            # 检测按键是否是a或者left
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                # x -= 5
                hero.move_left()
            # 检测按键是否是d或者right
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                # x += 5
                hero.move_right()
            elif event.key == K_SPACE:
                print("--space--")
                # 发射子弹
                hero.fire()


# 主函数
def main():
    # 创建窗口
    screen = pygame.display.set_mode((394, 700), 0, 32)
    # 创建背景图片
    background = pygame.image.load('./feiji/background.png')
    # 创建飞机
    hero = HeroPlan(screen)

    # 创建敌方飞机
    enemy = EnemyPlan(screen)

    while True:
        # 设置背景图片
        screen.blit(background, (0, 0))
        # 显示我方飞机
        hero.display()
        # 显示敌方飞机
        enemy.display()
        # 移动敌机
        enemy.move()
        # 敌方飞机发射子弹
        enemy.fire()
        # 键盘控制
        key_contro(hero)

        pygame.display.update()

        time.sleep(0.01)


if __name__ == '__main__':
    main()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值