Python_Pygame

简介: Python_Pygame
1. 
#01、基本圆的绘制
2. #'white'白色  'black'黑色  'red'红色  'yellow'黄色  'green'绿色  
3. #'orange'橙色  'blue'蓝色  'purple'紫色  'pink'粉色
4. import pgzrun #导入游戏开发库
5. def draw(): #定义绘图函数
6.     screen.fill('white')  # 屏幕.填充(颜色)
7.     screen.draw.circle((400, 300), 100, 'black')# 屏幕.绘制.圆((x坐标,y坐标),半径,颜色)
8.     screen.draw.filled_circle((400, 300), 80, 'red')#屏幕.绘制.填充圆((x坐标,y坐标),半径,颜色)
9. pgzrun.go()  # 游戏开始运行
1. #02、弹跳的小球
2. import pgzrun #导入游戏开发库
3. HEIGHT = 600  # 设置窗口高度
4. WIDTH = 800  # 设置窗口宽度
5. y = HEIGHT/2
6. x = WIDTH/2
7. speed_x = 5
8. speed_y = 10
9. r = 5
10. def draw():  # 定义绘图函数 每帧重复操作
11. #screen.fill('white')  # 屏幕.填充(颜色)
12.     screen.draw.filled_circle((x, y), r, 'red') #屏幕.绘制.填充圆((x坐标,y坐标),半径,颜色)
13. def update(): #定义一个更新函数 每帧重复操作
14. global x,y,speed_y,speed_x # global定义为全局变量
15.     y = y+speed_y
16.     x = x+speed_x
17. if x >= WIDTH-2*r or x <= r:
18.         speed_x = -speed_x  # 控制x坐标的增加和减少
19. if y >= HEIGHT-2*r or y <= r:
20.         speed_y = -speed_y  # 控制y坐标的增加和减少
21. pgzrun.go()  # 游戏开始运行
1. #03、缤纷的圆圈铺满屏
2. import pgzrun  # 导入游戏开发库
3. import random  # 导入随机库
4. WIDTH = 1200  # 设置窗口宽度
5. HEIGHT = 800  # 设置窗口高度
6. R = 100
7. def draw():  # 绘制模块 每帧重复执行
8.     screen.fill((255, 255, 255))  # 用RGB来填充白色背景
9. for x in range(R, WIDTH, 2*R):  # 枚举x坐标位置
10. for y in range(R, HEIGHT, 2*R):  # 枚举y坐标位置
11. for r in range(1, R, 10):  # 绘制每一层圆 从大到小 颜色随机
12.                 screen.draw.filled_circle((x, y), R-r,
13.                 (random.randint(0, 255), random.randint(0, 255), \
14.                 random.randint(0, 255)))
15. def on_mouse_down(): #当按下鼠标健时
16.     draw()
17. pgzrun.go()  # 游戏开始运行
1. #04、按下并移动鼠标随机生成随机移动的同心圆
2. import pgzrun   # 导入游戏开发库
3. import random   # 导入随机库
4. WIDTH =800      # 设置窗口宽度
5. HEIGHT = 600    # 设置窗口高度
6. balls=[]        # 建立空的列表
7. def draw():     # 绘制模块 每帧重复执行
8.     screen.fill('white')    # 填充白色背景
9. for ball in balls:      #遍历列表的所有项
10. #绘制一个填充圆
11.         screen.draw.filled_circle((ball[0],ball[1]),ball[4],(ball[5],ball[6],ball[7]))  
12. for x in range(1,ball[4],5):    #用同心圆填充 
13.             screen.draw.filled_circle((ball[0], ball[1]), ball[4]-x, \  
14.             (random.randint(ball[5], 255),\
15.             random.randint(ball[6], 255), random.randint(ball[7], 255)))
16. def update():           # 更新模块 每帧重复执行
17. for ball in balls:  #遍历列表的所有项
18.         ball[0] = ball[0]+ball[2]   #更新x坐标
19.         ball[1] = ball[1]+ball[3]   #更新y坐标
20. if ball[0] > WIDTH-ball[4] or ball[0] < ball[4]:
21.             ball[2] = -ball[2]      #改变x方向
22. if ball[1] > HEIGHT-ball[4] or ball[1] < ball[4]:
23.             ball[3] = -ball[3]      #改变y方向
24. def on_mouse_move(pos,rel,buttons): #当鼠标移动时
25. if mouse.LEFT in buttons:       #当按下鼠标左键时
26.         x=pos[0]    #获取鼠标x坐标
27.         y=pos[1]    #获取鼠标y坐标
28.         speedx=random.randint(1,5)      #生成x方向随机速度
29.         speedy=random.randint(1,5)      #生成y方向随机速度
30.         r=random.randint(5,50)          #生成随机半径
31.         colorR=random.randint(10,255)   #生成随机红
32.         colorG=random.randint(10,255)   #生成随机绿
33.         colorB=random.randint(10,255)   #生成随机蓝
34.         ball=[x,y,speedx,speedy,r,colorR,colorG,colorB] #存储小球信息
35.         balls.append(ball)  #将小球信息存入列表
36. pgzrun.go()    # 游戏开始运行
37.
1. #05、飞翔的小鸟 点击鼠标能够调整小鸟的飞翔高度 躲避障碍物
2. import pgzrun   # 导入游戏开发库
3. import random   # 导入随机库
4. 
5. WIDTH=670   # 设置窗口宽度
6. HEIGHT=415  # 设置窗口高度
7. background = Actor('background') #导入背景图片
8. bird=Actor('bird')  #导入小鸟图片
9. bird.x=150          #小鸟坐标x
10. bird.y=HEIGHT/2     #小鸟坐标y
11. bar_up = Actor('bar_up')    #导入障碍物上图片
12. bar_up.x=WIDTH              #障碍物上坐标x
13. bar_up.y=-20                #障碍物上坐标y
14. bar_down=Actor('bar_down')  #导入障碍物下图片
15. bar_down.x=WIDTH            #障碍物下坐标x
16. bar_down.y=HEIGHT+20        #障碍物下坐标y
17. score=0     #初始成绩
18. speed=1     #初始速度
19. 
20. def draw(): # 绘制模块 每帧重复执行
21.     background.draw()
22.     bird.draw()
23.     bar_up.draw()
24.     bar_down.draw()
25. #屏幕.绘制.文字(数字变量转换为字符串,(x,y坐标),字体大小,字体颜色)
26.     screen.draw.text(str(score),(30,30),fontsize=50,color='green')
27. 
28. def update():           # 更新模块 每帧重复执行
29. global score,speed  # global定义为全局变量
30.     bird.y+=2
31.     bar_up.x-=speed
32.     bar_down.x-=speed    
33. if bar_up.x<0:
34.         score+=1            #分数增加
35.         bar_up.x=WIDTH      #重置障碍物x坐标
36.         bar_down.x=WIDTH    #重置障碍物x坐标
37.         bar_up.y=random.randint(-150,150)   #随机障碍物高度
38.         bar_down.y=bar_up.y+HEIGHT+40       #保持障碍物之间的空隙高度
39. if score%3==0 :     #每增加三分速度提升1
40.             speed+=1
41. #碰到障碍物或屏幕上下边框游戏失败  数据重置
42. if bird.colliderect(bar_up) or bird.colliderect(bar_down) or bird.y<0 or bird.y>HEIGHT :
43. print("游戏失败!")
44.         bird.x=150
45.         bird.y=HEIGHT/2
46.         bar_up.x=WIDTH
47.         bar_up.y=-20
48.         bar_down.x=WIDTH
49.         bar_down.y=HEIGHT+20
50.         score=0
51.         speed=1
52. 
53. def on_mouse_down():    #当点击鼠标时
54.     bird.y-=50          #调整小鸟高度
55. 
56. pgzrun.go()
1. #06、见缝插针 按任意键发针
2. import pgzrun      # 导入游戏开发库
3. TITLE="见缝插针"    # 设置屏幕标题
4. 
5. HEIGHT = 600       # 设置窗口高度
6. WIDTH = 800        # 设置窗口宽度
7. needles = []       # 储存所有针的列表
8. # 导入初始针 设置锚点位置 并初始化它的坐标 
9. startneedle = Actor('needle', anchor=(170+50, 1.5))
10. startneedle.x=220
11. startneedle.y=300
12. rotateSpeed=1   # 设置旋转速度
13. score=0         # 初始化分数
14. 
15. def draw():     # 定义绘图函数 每帧重复操作
16.     screen.fill('white')    #背景
17.     screen.draw.filled_circle((400, 300), 80, 'red')    # 同心圆
18.     screen.draw.filled_circle((400, 300), 70, 'white')  # 同心圆
19.     startneedle.draw()      # 绘制初始针
20.     screen.draw.text(str(score), (50, 250), fontsize=50, color='orange')  # 文本分数
21. if rotateSpeed == 0:    # 转速为0 文本游戏结束
22.         screen.draw.text('Game over!', (20, 320), fontsize=35, color='red')
23. for needle in needles:  # 绘制列表中的所有针
24.         needle.draw()
25. 
26. def update():               # 定义一个更新函数 每帧重复操作
27. for needle in needles:  # 列表中的所有针进行旋转
28.         needle.angle += rotateSpeed
29. 
30. def on_key_down():              # 当按下任意键时
31. global rotateSpeed, score   # 定义全局变量
32. # 初始化新健针
33.     newneedle = Actor('needle', anchor=(170+50, 1.5))
34.     newneedle.x = 400
35.     newneedle.y = 300
36. for needle in needles:  # 遍历列表中的所有针
37. if newneedle.colliderect(needle):  # 碰撞检测
38.             music.play_once('失误')         # 失误声音播放
39. print('游戏失败!')
40.             rotateSpeed = 0     # 转速置零
41. if rotateSpeed > 0:         # 旋转速度非0
42.         music.play_once('光')   # 声音播放
43.         score += 1              # 成绩增加
44.     needles.append(newneedle)   # 将新针加入列表
45. 
46. pgzrun.go()
1. #07、飞机大战
2. import pgzrun  # 导入游戏库
3. import random  # 导入随机库
4. TITLE = 'Python飞机大战'  # 屏幕头
5. WIDTH = 480  # 屏幕宽
6. HEIGHT = 700  # 屏幕高
7. score = 0  # 成绩设置
8. isLoose = False  # bool标记游戏是否结束
9. sounds.game_music.play(-1)  # 背景音乐循环播放
10. 
11. background1 = Actor('background1')  # 加载循环背景1
12. background1.x = WIDTH/2  # 背景1设置初始化x坐标
13. background1.y = 852/2  # 背景1设置初始化y坐标
14. background2 = Actor('background2')  # 加载循环背景2
15. background2.x = WIDTH/2  # 背景2设置初始化x坐标
16. background2.y = -852/2  # 背景2设置初始化y坐标
17. 
18. bullet = Actor('bullet')  # 加载子弹角色
19. bullet.x = WIDTH/2  # 初始子弹角色x坐标
20. bullet.y = -HEIGHT  # 初始子弹y坐标
21. 
22. hero = Actor('hero')  # 加载我方飞机
23. hero.x = WIDTH/2  # 初始化我方飞机x坐标
24. hero.y = HEIGHT*2/3  # 初始化我方飞机y坐标
25. 
26. enemy = Actor('enemy')  # 加载敌机
27. enemy.x = WIDTH/2  # 初始化敌机x坐标
28. enemy.y = 0  # 初始化敌机y坐标
29. 
30. 
31. def draw():  # 绘制模块 每帧重复播放
32.     background1.draw()  # 绘制游戏背景1
33.     background2.draw()  # 绘制游戏背景2
34.     hero.draw()  # 绘制子弹
35.     bullet.draw()  # 绘制我机
36.     enemy.draw()  # 绘制敌机
37.     screen.draw.text("Score:"+str(score), (200, HEIGHT-50),
38.                      fontsize=30, color='green')  # 绘制文本成绩
39. if isLoose:  # 如果失败 绘制失败文本
40.         screen.draw.text("GAME ONER!", (80, HEIGHT/2), fontsize=50,
41.                          fontname='s', color='red')
42. 
43. def update():  # 更新模块 每帧重复操作
44. global score, isLoose  # 设置全局变量
45. if isLoose:
46. return  # 失败即退出
47.     background1.y += 1  # 背景下移
48.     background2.y += 1  # 背景下移
49. # 背景下移达到极限 y坐标重新初始化
50. if background1.y > 852/2+852:
51.         background1.y = -852/2
52. if background2.y > 852/2+852:
53.         background2.y = -852/2
54. if bullet.y > -HEIGHT:  # 子弹移动
55.         bullet.y = bullet.y-10
56. 
57.     enemy.y += 3  # 敌机移动
58. if enemy.y > HEIGHT:
59.         enemy.y = 0  # 重新初始化y坐标
60.         enemy.x = random.randint(80, 400)  # x坐标随机
61. #如果子弹碰撞到敌机
62. if bullet.colliderect(enemy):
63.         sounds.got_enemy.play()  # 播放击中音
64.         enemy.y = 0  # 敌机重新初始化
65.         enemy.x = random.randint(80, 400)
66.         score = score+1  # 成绩增加
67.         bullet.y = -HEIGHT  # 子弹初始化
68. #如果我方碰到敌机
69. if hero.colliderect(enemy):
70.         sounds.explode.play()  # 播放爆炸音
71.         isLoose = True  # 标记游戏失败
72.         hero.image = 'hero_blowup'  # 我方造型切换为爆炸图片
73. 
74. 
75. def on_mouse_move(pos, rel, buttons):  # 当鼠标移动时
76. if isLoose:
77. return  # 失败即退出
78.     hero.x = pos[0]  # 我机x坐标跟随鼠标坐标
79.     hero.y = pos[1]  # 我机y坐标跟随鼠标y坐标
80. 
81. 
82. def on_mouse_down():  # 当按下鼠标键
83. if isLoose:
84. return  # 失败即退出
85.     sounds.gun.play()  # 播放子弹发出的声音
86.     bullet.x = hero.x  # 子弹x坐标为我机x坐标
87.     bullet.y = hero.y-70  # 子弹y坐标为我机y坐标
88. 
89. pgzrun.go()  # 游戏开始
1. #08、永闯百层
2. import pgzrun #导入游戏库
3. import random #导入随机库
4. WIDTH = 600 #设置窗口宽度
5. HEIGHT = 800  # 设置窗口高度
6. playerSpeed = 5 #设置角色水平移动速度
7. breakSpeed = 1 #设置砖块自动上移速度
8. 
9. alien = Actor('alien') #导入角色图片
10. alien.x = WIDTH/2 #设置角色的x坐标
11. alien.y = HEIGHT/2 #设置角色的y坐标
12. lastAlienY=alien.y #记录角色上一帧的y坐标
13. 
14. isLoose = False #游戏是否失败
15. score=0 #游戏得分初始化
16. 
17. bricks = []  # 存储所有砖块的列表
18. for i in range(5):
19.     brick = Actor('brick') #导入砖块图片
20.     brick.pos = (100*(i+1), 150*(i+1)) #设置砖块的坐标
21.     bricks.append(brick) #把当前砖块加入列表中
22. 
23. def draw():#绘制模块 每帧重复执行
24.     screen.clear() #清空游戏画面
25.     alien.draw() #绘制角色
26. #绘制文字提示信息
27.     screen.draw.text("你已经坚持了"+str(score)+"层", (400, 20),
28.                      fontsize=25, fontname='s', color='white')
29. for brick in bricks:  # 绘制列表中的所有砖块
30.         brick.draw()
31. if isLoose:  # 如果游戏失败 显示相关文字
32.         screen.draw.text("游戏失败!", (80, HEIGHT/2-100),
33.                          fontsize=100, fontname='s', color='red')
34. 
35. def update():#更新模块,每一帧重复的动作
36. global isLoose, playerSpeed, breakSpeed, lastAlienY,score  # 变量全局化
37.     isPlayerOnGround = False  # 假设角色没有站在砖块上
38. for brick in bricks:#对列表中所有砖块遍历
39. #如果角色正好站在砖块上面,在砖块左右边界之间,可以左右移动
40. if abs(alien.bottom-brick.top) < 5 and\
41.                 brick.left-alien.left < alien.width*2/3 and\
42.                 alien.right-brick.right < alien.width*2/3:
43.             alien.image='alien' #设定砖块上站立的造型
44.             isPlayerOnGround = True #在砖块上
45.             alien.bottom = brick.top #角色跟着砖块一直向上移动
46. if lastAlienY < alien.y:  # 之前不在砖上
47.                 score += 1  # 现在跳到砖上 成绩加一
48. if keyboard.left: #如果按下键盘<--左移键
49.                 alien.x -= playerSpeed
50. if keyboard.right:  # 如果按下键盘-->右移键
51.                 alien.x += playerSpeed
52. 
53.     lastAlienY = alien.y  # 对所有砖块遍历后更新lastAlienY的值
54. if not isPlayerOnGround: #不在砖块上
55.         alien.image='alien_falling' #切换造型
56.         alien.y += 5 #下落
57. 
58. for brick in bricks: #所有砖块上升
59.         brick.y -= breakSpeed
60. 
61. if bricks[0].top < 10:#当最上面的砖块到达顶部时
62. del bricks[0] #删除最上面的砖块
63.         brick = Actor('brick') #新增一个砖块
64.         brick.x = random.randint(100, 500) #初始化新砖块的坐标 x坐标随机
65.         brick.y = HEIGHT #y坐标出现在最下面
66.         bricks.append(brick)  #将新砖块加入列表 
67. #角色超出上下范围 游戏失败
68. if alien.top < 0 or alien.bottom > HEIGHT:
69.         playerSpeed = 0
70.         breakSpeed = 0
71.         isLoose = True
72. 
73. pgzrun.go()
1. //09. 行走动画
2. import pgzrun #导入游戏库
3. WIDTH=600 #设置窗口宽度
4. HEIGHT = 800  # 设置窗口高度
5. 
6. #导入所有的动作分解图片 存储在列表中
7. Anims = [Actor('1'), Actor('2'), Actor('3'), 
8.          Actor('4'), Actor('5'), Actor('6')]
9. numAnims=len(Anims) #分解动作图片的张数
10. animIndex=0 #表示需要显示的动作图片的序号
11. animSpeed=0 #用于控制行走动画速度
12. 
13. player_x=WIDTH/2 #设置角色的x坐标
14. player_y = HEIGHT/2  # 设置角色的y坐标
15. for i in range(numAnims):
16.
相关文章
|
8天前
|
存储 数据处理 Python
Python比大小
Python比大小
26 0
|
8天前
|
安全 网络安全 数据安全/隐私保护
python telnetlib详解
`telnetlib`模块允许你通过Telnet协议与远程设备进行交互,执行命令并获取响应。这在自动化网络设备配置、远程服务器管理等场景中非常有用。需要注意的是,由于Telnet协议不安全,推荐在安全网络环境下使用,或者考虑替代协议如SSH。
23 0
|
12月前
|
机器学习/深度学习 并行计算 数据挖掘
【python是什么】
【python是什么】
|
Python
Python满天星
用Python画场小星星。
85 0
|
测试技术 Python
python分享-pprint
python分享-pprint
|
存储 算法 Python
|
Python
Python:使用2to3将Python2转Python3
Python:使用2to3将Python2转Python3
87 0
|
算法 Unix 数据库
Python 特点
Python 特点
112 0
|
SQL Java 关系型数据库
数据持久化技术(Python)的使用
- 传统数据库连接方式:mysql(PyMySQL) - ORM 模型:SQLAlchemy MyBatis、 Hibernate ## PyMySQL 安装: ``` pip install pymysql ``` ## 简单使用 利用 pymysql.connect 建立数据库连接并执行 SQL 命令(需要提前搭建好数据库): ``` import pymysql db =
|
Python
Python求梅森尼数
Python求梅森尼数
334 0
Python求梅森尼数

热门文章

最新文章