Python菜鸟快乐游戏编程_pygame(4)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清)
https://study.163.com/course/courseMain.htm?courseId=1006188025&share=2&shareId=400000000398149
为了熟悉键盘,鼠标,颜色参数,屏幕参数,我为大家准备了一个最简单的游戏sprite and sounds
import pygame, sys, time, random
from pygame.locals import * # Set up pygame.
pygame.init()
mainClock = pygame.time.Clock() # Set up the window.
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Sprites and Sounds') # Set up the colors.
WHITE = (255, 255, 255) # Set up the block data structure.
player = pygame.Rect(300, 100, 40, 40)
playerImage = pygame.image.load('player.png')
playerStretchedImage = pygame.transform.scale(playerImage, (40, 40))
foodImage = pygame.image.load('cherry.png')
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - 20), random.randint(0, WINDOWHEIGHT - 20), 20, 20)) foodCounter = 0
NEWFOOD = 40 # Set up keyboard variables.
moveLeft = False
moveRight = False
moveUp = False
moveDown = False MOVESPEED = 6 # Set up the music.
pickUpSound = pygame.mixer.Sound('pickup.wav')
pygame.mixer.music.load('background.mid')
pygame.mixer.music.play(-1, 0.0)
musicPlaying = True # Run the game loop.
while True:
# Check for the QUIT event.
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# Change the keyboard variables.
if event.key == K_LEFT or event.key == K_a:
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == K_d:
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == K_w:
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == K_s:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == K_a:
moveLeft = False
if event.key == K_RIGHT or event.key == K_d:
moveRight = False
if event.key == K_UP or event.key == K_w:
moveUp = False
if event.key == K_DOWN or event.key == K_s:
moveDown = False
if event.key == K_x:
player.top = random.randint(0, WINDOWHEIGHT - player.height)
player.left = random.randint(0, WINDOWWIDTH - player.width)
if event.key == K_m:
if musicPlaying:
pygame.mixer.music.stop()
else:
pygame.mixer.music.play(-1, 0.0)
musicPlaying = not musicPlaying if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0] - 10, event.pos[1] - 10, 20, 20)) foodCounter += 1
if foodCounter >= NEWFOOD:
# Add new food.
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - 20), random.randint(0, WINDOWHEIGHT - 20), 20, 20)) # Draw the white background onto the surface.
windowSurface.fill(WHITE) # Move the player.
if moveDown and player.bottom < WINDOWHEIGHT:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < WINDOWWIDTH:
player.right += MOVESPEED # Draw the block onto the surface.
windowSurface.blit(playerStretchedImage, player) # Check whether the block has intersected with any food squares.
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
player = pygame.Rect(player.left, player.top, player.width + 2, player.height + 2)
playerStretchedImage = pygame.transform.scale(playerImage, (player.width, player.height))
if musicPlaying:
pickUpSound.play() # Draw the food.
for food in foods:
windowSurface.blit(foodImage, food) # Draw the window onto the screen.
pygame.display.update()
mainClock.tick(40 )
如下图,生成一个游戏,小怪物吃樱桃。游戏随机生成若干樱桃,玩家通过操作键盘移动小怪物,吃的樱桃越多,小怪物就会越大。
https://study.163.com/provider/400000000398149/index.htm?share=2&shareId=400000000398149(博主视频教学主页)
Python菜鸟快乐游戏编程_pygame(4)的更多相关文章
- Python菜鸟快乐游戏编程_pygame(6)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Python菜鸟快乐游戏编程_pygame(5)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Python菜鸟快乐游戏编程_pygame(3)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Python菜鸟快乐游戏编程_pygame(2)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- Python菜鸟快乐游戏编程_pygame(1)
Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...
- python游戏编程——跟13岁儿童学编程
python爬虫基本告一段落,琢磨搞点其他的,正好在网上看到一个帖子,一个外国13岁小朋友用python写的下棋程序,内容详细,也有意思,拿来练手. 13岁啊.. 我这年纪还在敲 dir啥的吧 想到原 ...
- Python游戏编程入门
<Python游戏编程入门>这些文章负责整理在这本书中的知识点.注意事项和课后习题的尝试实现.并且对每一个章节给出的最终实例进行分析和注释. 初识pygame:pie游戏pygame游戏库 ...
- 分享《Python 游戏编程快速上手(第3版)》高清中文版PDF+高清英文版PDF+源代码
通过编写一个个小巧.有趣的游戏来学习Python,通过实例来解释编程的原理的方式.14个游戏程序和示例,介绍了Python基础知识.数据类型.函数.流程控制.程序调试.流程图设计.字符串操作.列表和字 ...
- 《Python游戏编程快速上手》——1.3 如何使用本书
本节书摘来自异步社区<Python游戏编程快速上手>一书中的第1章,第1.3节,作者[美] Al Sweigart(斯维加特),李强 译,更多章节内容可以访问云栖社区"异步社区& ...
随机推荐
- 如何搭建基于C#和 Appium 的 Android自动测试环境
本文由葡萄城技术团队于博客园原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 如果想做手机端的自动化测试,Appium是首选的测试框架,因为网上使 ...
- Docker 使用 Dockerfile 构建自己的镜像
可以使用Dockerfile的配置文件方式进行构建自己的镜像 下面利用docker构建一个Caddy web服务器 构建脚本 Dockerfile有自己的命令,下面使用了一些比较常用的命令,更多的Do ...
- 【English】十六、时间相关
〇.其他 date: I have a date with her tomarrow. n.约会;日期,日子;时代,年代; vt.过时;使…显老;显示出…时代(或年龄);鉴定…的年代 vt.& ...
- Android Studio导包无效,全部报红
今天在转移项目到新的机器上的时候,出现了所有的导入失效,import不起作用,但是原有代码又能正常运行,在大佬的帮助下找到了这篇博客: https://blog.csdn.net/mr_chenxu/ ...
- SQL MIN() 函数
MIN() 函数 MIN 函数返回一列中的最小值.NULL 值不包括在计算中. SQL MIN() 语法 SELECT MIN(column_name) FROM table_name 注释:MIN ...
- 好的RESTful API的设计原则
转载自一位大佬 英文原版 Principles of good RESTful API Design Good API design is hard! An API represents a cont ...
- tensorflow 训练之tensorboard使用
1.add saclar and histogram tf.summary.scalar('mean', mean) tf.summary.histogram('histogram', var) 2. ...
- js实现分段上传文件
使用js实现分段上传文件,本文使用了FileReader对象,可参考:https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader 1)获取文 ...
- C#中的out 参数,ref参数,值参数
大家可能在编码中或多或少的使用过out的ref,但是是否注意过他两的详细用法以及区别? 本文想介绍下详细介绍下out参数,ref参数以及一般值参数. 值参数 在使用参数时,把一个值传递给函数使用的一个 ...
- 浅谈css3有意思的属性pointer-events: none;
pointer-events: none: 可以让某个元素实现类似于海市蜃楼的效果,具体理解为,你可以看的到某个元素,但是你无法摸的着. 而display:none; 是你摸不着,但是你也看不见. ...