Python,以其简洁明了且功能全面的编程语言特性,正日益吸引着众多编程初学者的目光。将学习过程融入游戏元素,能够迅速引领同学们领略到编程学习的无穷乐趣与魅力~
- 下面给大家分享30个Python小游戏及源码,希望能更好的帮助大家上手掌握!
- 由于篇幅限制在此就发布了2个,剩下的源码已经给大家打包好了—在文末,需要的可以自取嗷!
一、植物大战僵尸游戏概述:
1. 核心游戏逻辑:
- 玩家操作:玩家需精心挑选并部署植物,以构筑坚实的防线。
- 僵尸入侵:僵尸们会从屏幕的一端悄然现身,坚定不移地向另一端进发。
- 游戏目标:玩家的终极任务是确保无一僵尸能突破防线,成功抵达屏幕的另一侧。
3. 技术实现所用库:
- pygame库:此库负责构建生动的图形用户界面,并高效处理玩家的各类输入指令。
- random库:借助此库,游戏能够随机设定僵尸的起始位置及行进速度,增添游戏的不确定性和挑战性。
4.简化版游戏设定:
- 植物种类:为便于初学者上手,游戏中仅设有一种植物,该植物具备向僵尸发射攻击的能力。
- 僵尸类型:同样地,游戏中仅包含一种僵尸,但其行进速度会根据random库的随机生成而有所差异。
- 用户界面:游戏界面采用简洁直观的命令行形式,旨在让玩家能够迅速沉浸于游戏世界,享受编程与策略结合的乐趣。
5.代码示例
二、飞机大战
让我们用python来做一个飞机大战小游戏,要放置玩家和敌机还有子弹,设置玩家控制方向:WASD,攻击:J。
import random
import tkinter
root = tkinter.Tk()
root.title('飞机大战')
root.geometry('800x650+400+100')
canvas = tkinter.Canvas(root, width=550, height=520, bg='white')
canvas.pack()
lb = tkinter.Label(root, width=20, height=1, bg='white')
lb.place(x=290, y=540)``count = 0``message = "消灭敌机数量:"
lb['text'] = message + str(count)
lb2 = tkinter.Label(root, width=20, height=1, bg='white')
lb2.place(x=290, y=570)
lb2['text'] = '---'
class PlayerPlane:
wings_x1, wings_y1 = 240, 470
wings_x2, wings_y2 = 290, 470
tail_x1, tail_y1 = 250, 500
tail_x2, tail_y2 = 280, 500
fuselage_x1, fuselage_y1 = 265, 500
fuselage_x2, fuselage_y2 = 265, 440
bullet_x1, bullet_y1 = fuselage_x2, fuselage_y2
bullet_x2, bullet_y2 = bullet_x1, bullet_y1 - 120
def __init__(self):
self.wings = canvas.create_line(self.wings_x1, self.wings_y1, self.wings_x2, self.wings_y2)
self.tail = canvas.create_line(self.tail_x1, self.tail_y1, self.tail_x2, self.tail_y2)
self.fuselage = canvas.create_line(self.fuselage_x1, self.fuselage_y1, self.fuselage_x2, self.fuselage_y2)
self.bullet = None
self.move_distance = 5
def attack(self, event):
self.bullet_x1, self.bullet_y1 = self.fuselage_x2, self.fuselage_y2
self.bullet_x2, self.bullet_y2 = self.bullet_x1, self.bullet_y1 - 120
self.bullet = canvas.create_line(self.bullet_x1, self.bullet_y1, self.bullet_x2, self.bullet_y2, fill='red',
dash=(1, 1))
canvas.after(80, self.delete_bullet)
def delete_bullet(self):
canvas.delete(self.bullet)
def move_left(self, event):
if self.wings_x1 == 0:
return
canvas.move(self.wings, -self.move_distance, 0)
self.wings_x1 -= self.move_distance
self.wings_x2 -= self.move_distance
canvas.move(self.tail, -self.move_distance, 0)
self.tail_x1 -= self.move_distance
self.tail_x2 -= self.move_distance
canvas.move(self.fuselage, -self.move_distance, 0)
self.fuselage_x1 -= self.move_distance
self.fuselage_x2 -= self.move_distance
def move_right(self, event):
if self.wings_x2 == 550:
return
canvas.move(self.wings, self.move_distance, 0)
self.wings_x1 += self.move_distance
self.wings_x2 += self.move_distance
canvas.move(self.tail, self.move_distance, 0)
self.tail_x1 += self.move_distance
self.tail_x2 += self.move_distance
canvas.move(self.fuselage, self.move_distance, 0)
self.fuselage_x1 += self.move_distance
self.fuselage_x2 += self.move_distance
def move_up(self, event):
if self.fuselage_y2 == 0:
return
canvas.move(self.wings, 0, -self.move_distance)
Self.wings_y1 -= self.move_distance
self.wings_y2 -= self.move_distance
canvas.move(self.tail, 0, -self.move_distance)
self.tail_y1 -= self.move_distance
self.tail_y2 -= self.move_distance
canvas.move(self.fuselage, 0, -self.move_distance)
self.fuselage_y1 -= self.move_distance
self.fuselage_y2 -= self.move_distance
def move_down(self, event):
if self.fuselage_y1 == 530:
return
canvas.move(self.wings, 0, self.move_distance)
self.wings_y1 += self.move_distance
self.wings_y2 += self.move_distance
anvas.move(self.tail, 0, self.move_distance)
self.tail_y1 += self.move_distance
self.tail_y2 += self.move_distance
canvas.move(self.fuselage, 0, self.move_distance)
self.fuselage_y1 += self.move_distance
self.fuselage_y2 += self.move_distance
class EnemyPlane:
wings_x1 = 0
wings_y1 = 30
wings_x2 = 40
wings_y2 = 30
tail_x1 = 10
tail_y1 = 0
tail_x2 = 30
tail_y2 = 0
fuselage_x1 = 20
fuselage_y1 = 0
fuselage_x2 = 20
fuselage_y2 = 60
bullet_x1, bullet_y1 = fuselage_x2, fuselage_y2
bullet_x2, bullet_y2 = bullet_x1, bullet_y1 + 120
def __init__(self):
self.bullet = None
self.move_distance = 10
def create(self):
lt = [0, 40, 80, 120]
x_change = random.choice(lt)
self.wings_x1 += x_change
self.wings_x2 += x_change
self.tail_x1 += x_change
self.tail_x2 += x_change
self.fuselage_x1 += x_change
self.fuselage_x2 += x_change
self.wings = canvas.create_line(self.wings_x1, self.wings_y1, self.wings_x2, self.wings_y2)
self.tail = canvas.create_line(self.tail_x1, self.tail_y1, self.tail_x2, self.tail_y2)
self.fuselage = canvas.create_line(self.fuselage_x1, self.fuselage_y1, self.fuselage_x2, self.fuselage_y2)
self.move()
def attack(self):
self.bullet_x1, self.bullet_y1 = self.fuselage_x2, self.fuselage_y2
self.bullet_x2, self.bullet_y2 = self.bullet_x1, self.bullet_y1 + 120
self.bullet = canvas.create_line(self.bullet_x1, self.bullet_y1, self.bullet_x2, self.bullet_y2, fill='red',
dash=(1, 1))
canvas.after(100, self.delete_bullet)
def delete_bullet(self):
canvas.delete(self.bullet)
def delete(self):
canvas.delete(self.wings)
canvas.delete(self.tail)
canvas.delete(self.fuselage)
def move(self):
global countself.attack()
canvas.move(self.wings, 0, self.move_distance)
self.wings_y1 += self.move_distanceself.wings_y2 += self.move_distance`
canvas.move(self.tail, 0, self.move_distance)
self.tail_y1 += self.move_distance
self.tail_y2 += self.move_distance
canvas.move(self.fuselage, 0, self.move_distance)
self.fuselage_y1 += self.move_distance
self.fuselage_y2 += self.move_distance
time_id2 = root.after(2000, self.move)
if self.fuselage_y2 == 530:
root.after_cancel(time_id2)
self.delete()
if (self.wings_x1 <= p.bullet_x2 <= self.wings_x2 and self.wings_y2 >= p.bullet_y2) or (
p.bullet_x2 == self.fuselage_x2 and p.bullet_y2 <= self.fuselage_y2):
root.after_cancel(time_id2)
self.delete()
count += 1
lb['text'] = message + str(count)
if count == n:
lb2['text'] = '敌机已全部消灭!
p = PlayerPlane()
lt = []
n = 3
for i in range(n):
lt.append(EnemyPlane())
i = 0
def cre():
global i, n, count
time_id = root.after(6000, cre)
if i < n:
lt[i].create()
i += 1
else:
root.after_cancel(time_id)
cre()
canvas.bind('<KeyPress-j>', p.attack)
canvas.bind('<KeyPress-a>', p.move_left)
canvas.bind('<KeyPress-d>', p.move_right)
canvas.bind('<KeyPress-w>', p.move_up)
canvas.bind('<KeyPress-s>', p.move_down)
canvas.focus_set()
root.mainloop()
1. 剩下的Python小游戏游戏
总结
- 最后希望你编程学习上不急不躁,按照计划有条不紊推进,把任何一件事做到极致,都是不容易的,加油,努力!相信自己!
文末福利
- 最后这里免费分享给大家一份Python全套学习资料,希望能帮到那些不满现状,想提升自己却又没有方向的朋友,也可以和我一起来学习交流呀。
包含编程资料、学习路线图、源代码、软件安装包等!【[点击这里]】领取!
- ① Python所有方向的学习路线图,清楚各个方向要学什么东西
- ② 100多节Python课程视频,涵盖必备基础、爬虫和数据分析
- ③ 100多个Python实战案例,学习不再是只会理论
- ④ 华为出品独家Python漫画教程,手机也能学习
可以扫描下方二维码领取【保证100%免费】