Python游戏利器pygame,疯狂赛车(34)
小伴侣们好,大伴侣们好!
我是猫妹,一名爱上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
好了,我们今天就学到那里吧!
假设碰着什么问题,咱们多多交换,配合处理。
我是猫妹,咱们下次见!