Python游戏利器pygame,疯狂赛车(34)

Python游戏利器pygame,疯狂赛车(34)

首页休闲益智疯狂赛车更新时间:2024-04-11

小朋友们好,大朋友们好!

我是猫妹,一名爱上Python编程的小学生。

欢迎和猫妹一起,趣味学Python。

今日主题

你玩过游戏吗?

你喜欢玩游戏吗?

手机游戏,电脑游戏,还是游戏机游戏?

Pygame是一个跨平台Python库,pygame 作者是 Pete Shinners, 协议为 GNU Lesser General Public License。

pygame官网上有许多游戏,如下图:

咱们今天简单学习下pygame,了解用它做游戏的简单流程。

使用前,需要用pip安装下pygame库。

一、绘制窗口

import sys import pygame from pygame.locals import * BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) pygame.init() clock = pygame.time.Clock() clock.tick(60) pygame.display.set_caption("疯狂赛车") screen = pygame.display.set_mode((500, 800)) screen.fill(WHITE) pygame.display.update() while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()

2行:导入pygame库

3行:导入pygame库中的局部变量

5~7行:定义单个颜色RGB变量

9行:该函数在使用pygame时进行初始化,只有引用了该函数才能使用pygame提供的所用功能。

10行:实例化Clock对象

11行:设置每秒60帧,每秒更新不超过60次。

帧速率是指程序每秒在屏幕上绘制图像的数目,我们可以用FPS来表示它。

一般的计算机都能达到每秒60帧的速度。如果我们把帧速率讲得比较低,游戏会看上去较为卡顿。

pygame.time.Clock对象帮助我们确定程序要以多少最大的帧速率运行。

13行:设置窗口的名称

14行:设置游戏窗口大小

15行:设置窗口填充颜色

16行:刷新屏幕内容显示

18~23行:当玩家点击退出时才会退出,所以是一个循环结构。

19行:获取事件,比如鼠标、键盘等

20行:当用户点击了窗口的关闭,产生事件QUIT

21~22行:退出程序,pygame.quit()是pygame.init()函数的一种相反的函数,它运行的代码会使得Pygame库停止工作。在调用sys.exit()终止程序之前,总是应该先调用pygame.quit()。

23行:刷新屏幕

二、加载游戏界面

import sys, os, random import pygame from pygame.locals import * BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GAME_ROOT_FOLDER=os.path.dirname(__file__) IMAGE_FOLDER=os.path.join(GAME_ROOT_FOLDER, "Images") pygame.init() clock = pygame.time.Clock() clock.tick(60) pygame.display.set_caption("疯狂赛车") IMG_ROAD = pygame.image.load(os.path.join(IMAGE_FOLDER, "Road.png")) IMG_PLAYER = pygame.image.load(os.path.join(IMAGE_FOLDER, "Player.png")) IMG_ENEMY = pygame.image.load(os.path.join(IMAGE_FOLDER, "Enemy.png")) screen = pygame.display.set_mode(IMG_ROAD.get_size()) h=IMG_ROAD.get_width()//2 v=IMG_ROAD.get_height() - (IMG_PLAYER.get_height()//2) player = pygame.sprite.Sprite() player.image = IMG_PLAYER player.surf = pygame.Surface(IMG_PLAYER.get_size()) player.rect = player.surf.get_rect(center = (h, v)) hl=IMG_ENEMY.get_width()//2 hr=IMG_ROAD.get_width()-(IMG_ENEMY.get_width()//2) h=random.randrange(hl, hr) v=0 enemy = pygame.sprite.Sprite() enemy.image = IMG_ENEMY enemy.surf = pygame.Surface(IMG_ENEMY.get_size()) enemy.rect = enemy.surf.get_rect(center = (h, v)) while True: screen.blit(IMG_ROAD, (0,0)) screen.blit(player.image, player.rect) screen.blit(enemy.image, enemy.rect) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()

1行:导入sys、os、random

9行:获取当前py文件所在路径

10行:当前py文件夹路径下的Images,此路径存放一些图片

16~18行:加载三张图像数据

(1).使用pygame.image.load()加载图像的数据

(2).使用游戏屏幕对象,调用blit方法将图像绘制到指定位置

(3).调用pygame.display.update()方法更新整个屏幕的显示

要在创建的窗口中按照指定要求显示图片,需要三个步骤:

19行:设置窗口大小

21~26行:设置player信息

精灵(sprite),一种能放在surface上的图片对象,可以进行移动、旋转、删除等操作。

创建sprite对象player,分别设置图像、大小、位置等。

31~34行:设置enemy信息

创建sprite对象enemy,分别设置图像、大小、位置等。

36~38行:使用游戏屏幕对象,调用blit方法将图像绘制到指定位置

三、动起来

import sys, os, random import pygame from pygame.locals import * from datetime import datetime BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) moveSpeed = 5 GAME_ROOT_FOLDER=os.path.dirname(__file__) IMAGE_FOLDER=os.path.join(GAME_ROOT_FOLDER, "Images") pygame.init() clock = pygame.time.Clock() clock.tick(60) pygame.display.set_caption("疯狂赛车") IMG_ROAD = pygame.image.load(os.path.join(IMAGE_FOLDER, "Road.png")) IMG_PLAYER = pygame.image.load(os.path.join(IMAGE_FOLDER, "Player.png")) IMG_ENEMY = pygame.image.load(os.path.join(IMAGE_FOLDER, "Enemy.png")) screen = pygame.display.set_mode(IMG_ROAD.get_size()) h=IMG_ROAD.get_width()//2 v=IMG_ROAD.get_height() - (IMG_PLAYER.get_height()//2) player = pygame.sprite.Sprite() player.image = IMG_PLAYER player.surf = pygame.Surface(IMG_PLAYER.get_size()) player.rect = player.surf.get_rect(center = (h, v)) hl=IMG_ENEMY.get_width()//2 hr=IMG_ROAD.get_width()-(IMG_ENEMY.get_width()//2) h=random.randrange(hl, hr) v=0 enemy = pygame.sprite.Sprite() enemy.image = IMG_ENEMY enemy.surf = pygame.Surface(IMG_ENEMY.get_size()) enemy.rect = enemy.surf.get_rect(center = (h, v)) while True: screen.blit(IMG_ROAD, (0,0)) screen.blit(player.image, player.rect) keys = pygame.key.get_pressed() if keys[K_LEFT] and player.rect.left > 0: player.rect.move_ip(-moveSpeed, 0) if player.rect.left < 0: player.rect.left = 0 if keys[K_RIGHT] and player.rect.right < IMG_ROAD.get_width(): player.rect.move_ip(moveSpeed, 0) if player.rect.right > IMG_ROAD.get_width(): player.rect.right = IMG_ROAD.get_width() screen.blit(enemy.image, enemy.rect) enemy.rect.move_ip(0, moveSpeed) if (enemy.rect.bottom > IMG_ROAD.get_height()): hl=IMG_ENEMY.get_width()//2 hr=IMG_ROAD.get_width()-(IMG_ENEMY.get_width()//2) h=random.randrange(hl, hr) v=0 enemy.rect.center = (h, v) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() dt_obj = datetime.now() millisec = dt_obj.timestamp() print(millisec)

怎么让游戏动起来呢?

只需要修改两张汽车的照片位置即可,可以用键盘上的左右键控制汽车player的左右移动,另一辆汽车可以自动从上向下移动。

10行:变量moveSpeed用于定义汽车移动速度

41~45行:如果按下了键盘方向键左键,且坐标没有靠近最左边,使其向左移动,否则在窗口最左边不改变。

47~50行:如果按下了键盘方向键右键,且坐标没有靠近最右边,使其向右移动,否则在窗口最右边不改变。

53~59行:汽车enemy从上向下移动,如果移出窗口,则重新生成一个,位置在顶部,横坐标随机。

66~68行:打印时间信息,调试使用

四、碰撞检测

import sys, os, random import pygame from pygame.locals import * from datetime import datetime BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) moveSpeed = 5 maxSpeed = 10 score = 0 GAME_ROOT_FOLDER=os.path.dirname(__file__) IMAGE_FOLDER=os.path.join(GAME_ROOT_FOLDER, "Images") def GameOver(): pygame.quit() sys.exit() pygame.init() clock = pygame.time.Clock() clock.tick(60) pygame.display.set_caption("疯狂赛车") IMG_ROAD = pygame.image.load(os.path.join(IMAGE_FOLDER, "Road.png")) IMG_PLAYER = pygame.image.load(os.path.join(IMAGE_FOLDER, "Player.png")) IMG_ENEMY = pygame.image.load(os.path.join(IMAGE_FOLDER, "Enemy.png")) screen = pygame.display.set_mode(IMG_ROAD.get_size()) h=IMG_ROAD.get_width()//2 v=IMG_ROAD.get_height() - (IMG_PLAYER.get_height()//2) player = pygame.sprite.Sprite() player.image = IMG_PLAYER player.surf = pygame.Surface(IMG_PLAYER.get_size()) player.rect = player.surf.get_rect(center = (h, v)) hl=IMG_ENEMY.get_width()//2 hr=IMG_ROAD.get_width()-(IMG_ENEMY.get_width()//2) h=random.randrange(hl, hr) v=0 enemy = pygame.sprite.Sprite() enemy.image = IMG_ENEMY enemy.surf = pygame.Surface(IMG_ENEMY.get_size()) enemy.rect = enemy.surf.get_rect(center = (h, v)) while True: pygame.display.set_caption("疯狂赛车-得分 " str(score)) screen.blit(IMG_ROAD, (0,0)) screen.blit(player.image, player.rect) keys = pygame.key.get_pressed() if keys[K_LEFT] and player.rect.left > 0: player.rect.move_ip(-moveSpeed, 0) if player.rect.left < 0: player.rect.left = 0 if keys[K_RIGHT] and player.rect.right < IMG_ROAD.get_width(): player.rect.move_ip(moveSpeed, 0) if player.rect.right > IMG_ROAD.get_width(): player.rect.right = IMG_ROAD.get_width() screen.blit(enemy.image, enemy.rect) enemy.rect.move_ip(0, moveSpeed) if (enemy.rect.bottom > IMG_ROAD.get_height()): hl=IMG_ENEMY.get_width()//2 hr=IMG_ROAD.get_width()-(IMG_ENEMY.get_width()//2) h=random.randrange(hl, hr) v=0 enemy.rect.center = (h, v) score = 1 if moveSpeed < maxSpeed: moveSpeed = 1 if pygame.sprite.collide_rect(player, enemy): GameOver() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() # dt_obj = datetime.now() # millisec = dt_obj.timestamp() # print(millisec)

11行:对面汽车的最大速度

12行:定义变量用于保存玩家分数

15行:定义一个函数,当玩家汽车和对面汽车接触时退出游戏

43行:窗口标题栏会显示玩家当前的分数

66行:每躲过一个对面的小汽车时,分数加1

67~68行:每躲过一个对面的小汽车时,对面小汽车速度加1,直到最大值

69~70:检测两个精灵对象是否接触,如果接触,退出游戏

五、Game Over

import sys, os, random, time import pygame from pygame.locals import * BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) startSpeed = 5 moveSpeed = startSpeed maxSpeed = 10 score = 0 eNum = -1 paused = False GAME_ROOT_FOLDER=os.path.dirname(__file__) IMAGE_FOLDER=os.path.join(GAME_ROOT_FOLDER, "Images") def GameOver(): textFonts = ['comicsansms','arial'] textSize = 48 fontGameOver = pygame.font.SysFont(textFonts, textSize) textGameOver = fontGameOver.render("Game Over!", True, RED) rectGameOver = textGameOver.get_rect() rectGameOver.center = (IMG_ROAD.get_width()//2, IMG_ROAD.get_height()//2) screen.fill(BLACK) screen.blit(textGameOver, rectGameOver) pygame.display.update() player.kill() enemy.kill() time.sleep(5) pygame.quit() sys.exit() pygame.init() clock = pygame.time.Clock() clock.tick(60) pygame.display.set_caption("疯狂赛车") IMG_ROAD = pygame.image.load(os.path.join(IMAGE_FOLDER, "Road.png")) IMG_PLAYER = pygame.image.load(os.path.join(IMAGE_FOLDER, "Player.png")) IMG_ENEMIES = [] IMG_ENEMIES.append(pygame.image.load(os.path.join(IMAGE_FOLDER, "Enemy.png"))) IMG_ENEMIES.append(pygame.image.load(os.path.join(IMAGE_FOLDER, "Enemy2.png"))) IMG_ENEMIES.append(pygame.image.load(os.path.join(IMAGE_FOLDER, "Enemy3.png"))) IMG_ENEMIES.append(pygame.image.load(os.path.join(IMAGE_FOLDER, "IceCube.png"))) screen = pygame.display.set_mode(IMG_ROAD.get_size()) h=IMG_ROAD.get_width()//2 v=IMG_ROAD.get_height() - (IMG_PLAYER.get_height()//2) player = pygame.sprite.Sprite() player.image = IMG_PLAYER player.surf = pygame.Surface(IMG_PLAYER.get_size()) player.rect = player.surf.get_rect(center = (h, v)) while True: pygame.display.set_caption("疯狂赛车-得分 " str(score)) screen.blit(IMG_ROAD, (0,0)) screen.blit(player.image, player.rect) if eNum == -1: eNum = random.randrange(0, len(IMG_ENEMIES)) hl=IMG_ENEMIES[eNum].get_width()//2 hr=IMG_ROAD.get_width()-(IMG_ENEMIES[eNum].get_width()//2) h=random.randrange(hl, hr) v=0 enemy = pygame.sprite.Sprite() enemy.image = IMG_ENEMIES[eNum] enemy.surf = pygame.Surface(IMG_ENEMIES[eNum].get_size()) enemy.rect = enemy.surf.get_rect(center = (h, v)) keys = pygame.key.get_pressed() if paused: if not keys[K_SPACE]: moveSpeed=tempSpeed paused=False else: if keys[K_LEFT] and player.rect.left > 0: player.rect.move_ip(-moveSpeed, 0) if player.rect.left < 0: player.rect.left = 0 if keys[K_RIGHT] and player.rect.right < IMG_ROAD.get_width(): player.rect.move_ip(moveSpeed, 0) if player.rect.right > IMG_ROAD.get_width(): player.rect.right = IMG_ROAD.get_width() if keys[K_SPACE]: tempSpeed=moveSpeed moveSpeed=0 paused=True screen.blit(enemy.image, enemy.rect) enemy.rect.move_ip(0, moveSpeed) if (enemy.rect.bottom > IMG_ROAD.get_height()): enemy.kill() eNum = -1 score = 1 moveSpeed = 1 if moveSpeed < maxSpeed: moveSpeed = 1 if eNum >= 0 and pygame.sprite.collide_rect(player, enemy): if eNum == 3: moveSpeed = startSpeed else: GameOver() for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()

5行:开始速度

22~40行:将游戏结束时,显示Game Over

16行:对面汽车索引

17行:是否暂停

49~53行:对面障碍物,是四个图片中的一个,随机出现

68~78行:随机产生一个障碍物

81~84行:空格键松开时,恢复对面车的运动速度,游戏暂停结束

94~97行:空格键按下时,保存当前对面车的速度,游戏开始暂停

102~108行:当对面车移除游戏窗口时,销毁它

111~112行:当遇到特殊的车,速度降低到原始速度

源码

有朋友需要图片、源码等资源的话,可以点击阅读原文,然后输入6666

好了,我们今天就学到这里吧!

如果遇到什么问题,咱们多多交流,共同解决。

我是猫妹,咱们下次见!

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved