赞
踩
这里主要使用turtle来开发,pygame来添加音效
参考:跟麦叔学Python - 1小时开发太空侵略者游戏
pip install turtle
pip install pygame
在末尾处写上这两句
- turtle.done()
- game.mainloop()
设置700*700大小的背景
- import turtle
-
- game = turtle.Screen()
- game.setup(700.700)
- game.title('飞机大战')
- game.bgpic('bg.png')
-
- turtle.done()
注意:这里的图片都要用gif格式的
- # 创建玩家
- turtle.register_shape('player2.gif')
- player = turtle.Turtle()
- player.ht()
- player.speed(0)
- player.up()
- player.shape('player2.gif')
- player.setpos(0,-230)
- player.st()
- # 设置步长
- player_step = 20
- # 玩家左移和右移函数
- def go_left():
- x = player.xcor()
- x = x - player_step
- if x < -300:
- x = -300
- player.setx(x)
-
- def go_right():
- x = player.xcor()
- x = x + player_step
- if x > 300:
- x = 300
- player.setx(x)
-
- turtle.listen()
- # 左移和右移
- turtle.onkey(go_left,'Left')
- turtle.onkey(go_right,'Right')
这里创建6个敌人,每个敌人的初始位置随机
- # 添加敌人
- num = 6
- inv_list = []
- turtle.register_shape('enemy.gif')
- for i in range(0,6):
- inv = turtle.Turtle()
- inv_list.append(inv)
- inv.ht()
- inv.speed(0)
- inv.up()
- inv.shape('enemy.gif')
- x = random.randint(-200,200)
- y = random.randint(100,220)
- inv.setpos(x,y)
- inv.st()
这个while循环是死循环,要写在后面
- # 敌人动起来
- inv_step = 5
- go_back = False
- while True:
- for inv in inv_list:
- x = inv.xcor()
- x += inv_step
- inv.setx(x)
- if inv.xcor() > 300 or inv.xcor() < -300:
- go_back = True
-
- # 掉头
- if go_back:
- inv_step *= -1
- go_back = False
- for inv in inv_list:
- y = inv.ycor()
- y -= 40
- inv.sety(y)
- # 添加子弹
- bomb = turtle.Turtle()
- bomb.ht()
- bomb.speed(0)
- bomb.up()
- bomb.shape('triangle')
- bomb.color('yellow')
- bomb.shapesize(0.5,0.5)
- bomb.seth(90)
- bomb.setpos(-350,-350)
发射子弹
- # 发射子弹
- is_fired = False
- def fire():
- global is_fired
- if is_fired == False:
- bomb.setpos(player.xcor(), player.ycor() + 30)
- bomb.st()
- is_fired = True
-
- turtle.onkey(fire,'space')
子弹射出:
- # 敌人动起来
- inv_step = 5
- go_back = False
- bomb_step = 50
- while True:
- for inv in inv_list:
- x = inv.xcor()
- x += inv_step
- inv.setx(x)
- if inv.xcor() > 300 or inv.xcor() < -300:
- go_back = True
-
- # 掉头
- if go_back:
- inv_step *= -1
- go_back = False
- for inv in inv_list:
- y = inv.ycor()
- y -= 40
- inv.sety(y)
-
- # 子弹被发射
- if is_fired:
- y = bomb.ycor()
- y += bomb_step
- bomb.sety(y)
- if y > 250:
- is_fired = False
- bomb.setpos(-350, -350)
- bomb.ht()
打中后的敌人再次随机生成位置
- # 敌人动起来
- inv_step = 5
- go_back = False
- bomb_step = 50
- while True:
- for inv in inv_list:
- x = inv.xcor()
- x += inv_step
- inv.setx(x)
- if inv.xcor() > 300 or inv.xcor() < -300:
- go_back = True
-
- # 打中敌人,并重新生成敌人位置
- if inv.distance(bomb) < 30:
- x = random.randint(-200, 200)
- y = random.randint(-50, 220)
- inv.setpos(x,y)
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
-
- # 掉头
- if go_back:
- inv_step *= -1
- go_back = False
- for inv in inv_list:
- y = inv.ycor()
- y -= 40
- inv.sety(y)
- # 子弹被发射
- if is_fired:
- y = bomb.ycor()
- y += bomb_step
- bomb.sety(y)
- if y > 250:
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
- # 添加分数
- score = 0
- pen = turtle.Turtle()
- pen.color('white')
- pen.speed(0)
- pen.up()
- pen.ht()
- pen.setpos(-340,265)
- score_string = "分数:%s" %score
- pen.write(score_string,align="left",font=('Arial',12,'normal'))
- # 敌人动起来
- inv_step = 5
- go_back = False
- bomb_step = 50
- while True:
- for inv in inv_list:
- x = inv.xcor()
- x += inv_step
- inv.setx(x)
- if inv.xcor() > 300 or inv.xcor() < -300:
- go_back = True
-
- # 打中敌人,并重新生成敌人位置
- if inv.distance(bomb) < 30:
- x = random.randint(-200, 200)
- y = random.randint(-50, 220)
- inv.setpos(x,y)
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
-
- score += 10
- score_string = "分数:%s" % score
- pen.clear()
- pen.write(score_string, align="left", font=('Arial', 12, 'normal'))
-
- # 掉头
- if go_back:
- inv_step *= -1
- go_back = False
- for inv in inv_list:
- y = inv.ycor()
- y -= 40
- inv.sety(y)
- # 子弹被发射
- if is_fired:
- y = bomb.ycor()
- y += bomb_step
- bomb.sety(y)
- if y > 250:
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
添加分界线
- # 画一条线,敌人越过这条线游戏结束
- t = turtle.Turtle()
- t.pencolor("white")
-
- t.penup()
- t.goto(-350, -200)
- t.pendown()
-
- for i in range(35):
- t.forward(10)
- t.penup()
- t.forward(10)
- t.pendown()
敌人越过分界线,游戏结束
- # 敌人动起来
- inv_step = 5
- go_back = False
- bomb_step = 50
- game_over = False
- while True:
- # 游戏结束
- if game_over:
- pen2 = turtle.Turtle()
- pen2.color('red')
- pen2.ht()
- pen2.write('游戏结束',align='center',font=('Arial',18,'normal'))
- explosion.play()
- break
-
- for inv in inv_list:
- x = inv.xcor()
- x += inv_step
- inv.setx(x)
- if inv.xcor() > 300 or inv.xcor() < -300:
- go_back = True
-
- # 打中敌人,并重新生成敌人位置
- if inv.distance(bomb) < 30:
- x = random.randint(-200, 200)
- y = random.randint(-50, 220)
- inv.setpos(x,y)
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
-
- score += 10
- score_string = "分数:%s" % score
- pen.clear()
- pen.write(score_string, align="left", font=('Arial', 12, 'normal'))
-
- # 判定游戏结束
- if inv.ycor() < -180:
- game_over = True
-
- # 掉头
- if go_back:
- inv_step *= -1
- go_back = False
- for inv in inv_list:
- y = inv.ycor()
- y -= 40
- inv.sety(y)
- # 子弹被发射
- if is_fired:
- y = bomb.ycor()
- y += bomb_step
- bomb.sety(y)
- if y > 250:
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
- import pygame
-
- # 音乐
- pygame.init()
- laser = pygame.mixer.Sound('laser.wav')
- explosion = pygame.mixer.Sound('exp.wav')
- bg = pygame.mixer.Sound('bg.wav')
- # 使用bg音乐
- bg.play()
- import turtle
- import random
- import pygame
-
- # 音乐
- pygame.init()
- laser = pygame.mixer.Sound('laser.wav')
- explosion = pygame.mixer.Sound('exp.wav')
- bg = pygame.mixer.Sound('bg.wav')
-
- bg.play()
-
- # 设置背景
- game = turtle.Screen()
- game.setup(700.700)
- game.title('飞机大战')
- game.bgpic('bg.png')
-
- # 画一条线,敌人越过这条线游戏结束
- t = turtle.Turtle()
- t.pencolor("white")
-
- t.penup()
- t.goto(-350, -200)
- t.pendown()
-
- for i in range(35):
- t.forward(10)
- t.penup()
- t.forward(10)
- t.pendown()
-
- # 创建玩家
- turtle.register_shape('player2.gif')
- player = turtle.Turtle()
- player.ht()
- player.speed(0)
- player.up()
- player.shape('player2.gif')
- player.setpos(0,-230)
- player.st()
-
- # 设置步长
- player_step = 20
- # 玩家左移和右移函数
- def go_left():
- x = player.xcor()
- x = x - player_step
- if x < -300:
- x = -300
- player.setx(x)
-
- def go_right():
- x = player.xcor()
- x = x + player_step
- if x > 300:
- x = 300
- player.setx(x)
-
- turtle.listen()
- # 左移和右移
- turtle.onkey(go_left,'Left')
- turtle.onkey(go_right,'Right')
-
-
- # 添加敌人
- num = 6
- inv_list = []
- turtle.register_shape('enemy.gif')
- for i in range(0,6):
- inv = turtle.Turtle()
- inv_list.append(inv)
- inv.ht()
- inv.speed(0)
- inv.up()
- inv.shape('enemy.gif')
- x = random.randint(-200,200)
- y = random.randint(100,220)
- inv.setpos(x,y)
- inv.st()
-
- # 添加子弹
- bomb = turtle.Turtle()
- bomb.ht()
- bomb.speed(0)
- bomb.up()
- bomb.shape('triangle')
- bomb.color('yellow')
- bomb.shapesize(0.5,0.5)
- bomb.seth(90)
- bomb.setpos(-350,-350)
-
- # 发射子弹
- is_fired = False
- def fire():
- global is_fired
- if is_fired == False:
- bomb.setpos(player.xcor(), player.ycor() + 30)
- bomb.st()
- is_fired = True
- laser.play()
-
- turtle.onkey(fire,'space')
-
- # 添加分数
- score = 0
- pen = turtle.Turtle()
- pen.color('white')
- pen.speed(0)
- pen.up()
- pen.ht()
- pen.setpos(-340,265)
- score_string = "分数:%s" %score
- pen.write(score_string,align="left",font=('Arial',12,'normal'))
-
- # 敌人动起来
- inv_step = 5
- go_back = False
- bomb_step = 50
- game_over = False
- while True:
- # 游戏结束
- if game_over:
- pen2 = turtle.Turtle()
- pen2.color('red')
- pen2.ht()
- pen2.write('游戏结束',align='center',font=('Arial',18,'normal'))
- explosion.play()
- break
-
- for inv in inv_list:
- x = inv.xcor()
- x += inv_step
- inv.setx(x)
- if inv.xcor() > 300 or inv.xcor() < -300:
- go_back = True
-
- # 打中敌人,并重新生成敌人位置
- if inv.distance(bomb) < 30:
- x = random.randint(-200, 200)
- y = random.randint(-50, 220)
- inv.setpos(x,y)
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
-
- score += 10
- score_string = "分数:%s" % score
- pen.clear()
- pen.write(score_string, align="left", font=('Arial', 12, 'normal'))
- explosion.play()
-
- # 判定游戏结束
- if inv.ycor() < -180:
- game_over = True
-
- # 掉头
- if go_back:
- inv_step *= -1
- go_back = False
- for inv in inv_list:
- y = inv.ycor()
- y -= 40
- inv.sety(y)
- # 子弹被发射
- if is_fired:
- y = bomb.ycor()
- y += bomb_step
- bomb.sety(y)
- if y > 250:
- is_fired = False
- bomb.setpos(-350,-350)
- bomb.ht()
-
- turtle.done()
- game.mainloop()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。