**热狗大战 **
最近看了王思聪吃热狗的图,感觉很有趣,所以就做了一个小游戏,来给大家分享一下!
先让大家看一下效果
游戏界面
结束界面
怎么样?不错吧?
今天就来做一下这个游戏
1.基础部分
1.1导入模块
这个游戏中有4个模块,分别是pygame,os,random,time,sys。
导入都可以用pip方法。
#windows + R 输入cmd按下回车
pip install pygame
pip installos
pip installrandom
…………(其它自己打吧^_^)
1.2游戏窗口及图片加载
#初始化pygame环境
pygame.init ()
#创建一个长宽分别为1300/700窗口
os.environ[ 'SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 25)
canvas = pygame.display.set_mode((1000,625))
canvas.fill((255,255,255))
#设置窗口标题
pygame.display.set_caption("热狗大战")
#加载图片
bg = pygame.image.load("images/bg.jpg")
h = pygame.image.load("images/hotdog.png")
player = pygame.image.load("images/hero.png")
end=pygame.image.load("images/end.jpg")
bullet_tip=pygame.image.load("images/bullet_tip.png")
time_tip=pygame.image.load("images/time_tip.png")
1.3键盘反应
其实这个是挺重要的,没键盘基本 啥都做不成。
看代码:
def handleEvent():
for event in pygame.event.get():
if event.type == QUIT :
pygame.quit()
sys.exit()
if states == 'RUNNING':
if event.type == KEYDOWN and event.key == K_LEFT:
hero.left()
if event.type == KEYDOWN and event.key == K_RIGHT:
hero.right()
2.运行部分
2.1类的使用
#英雄类
class Hero():
def __init__(self,x,y,img):
self.width=80
self.height=133
self.x=x
self.y=y
self.img=img
self.score=0
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def hit(self,c): #代表热狗
return c.x > self.x - c.width and \
c.x < self.x + self.width and \
c.y > self.y - c.height and \
c.y < self.y + self.height
def left(self):
self.x = self.x - 50
def right(self):
self.x = self.x + 50
def outOfBounds(self):
if self.x < 0:
self.x = self.x + 50
if self.x > 1000 - self.width:
self.x = self.x - 50
#热狗 类
class Hotdog():
def __init__(self,x,y,img):
self.width=30
self.height=55
self.x=random.randint(0,(1000-self.width))
self.y=-self.height
self.img=img
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def step(self):
self.y=self.y+10
2.2后台工具
主要是用于加载绘制!
def isActionTime(lastTime,interval):
if lastTime==0:
return True
currentTime=time.time()
return currentTime-lastTime>=interval
def conEnter():
global lastTime
if isActionTime(lastTime,interval):
lastTime=time.time()
hotdogs.append(Hotdog(0,-55,h))
else:
return
def conPaint():
canvas.blit(bg,(0,0))
canvas.blit(bullet_tip,(800,5))
fillText(str(hero.score),(880,40))
hero.paint()
for d in hotdogs:
d.paint()
def conStep():
for d in hotdogs:
d.step()
hero=Hero(450, 450, player)
hotdogs=[]
lastTime=-100
interval=0.5
t=60
n=0
states = 'RUNNING' #设置游戏默认状态为运行状态
文字及检测部分
检测是检测热狗与人发生的碰撞,及写出分数并加1。
def gameTime():
global t,n
canvas.blit(time_tip,(20,20))
fillText(str(t),(35,35))
if n%50==0:
t=t-1
n=n+1
def checkHit(): #检测热狗是否与人物发生碰撞
for d in hotdogs:
if hero.hit(d):
hero.score = hero.score + 1
hotdogs.remove(d)
def control():
global states
if states == 'RUNNING':
conEnter()
conPaint()
conStep()
gameTime()
checkHit()
hero.outOfBounds()
if t <= 0:
states = 'OVER'
if states == 'OVER':
canvas.blit(end,(0,0))
if hero.score < 10:
fillTextOver(str(hero.score),(485,390))
else:
fillTextOver(str(hero.score),(470,390))
# 工具方法-写文字方法
def fillText(text, position, view=canvas):
# 设置字体样式和大小
my_font = pygame.font.Font("my_font/font1.ttf", 30)
# 渲染文字
text = my_font.render(text, True, (255, 255, 255))
view.blit(text, position)
def fillTextOver(text, position, view=canvas):
my_font = pygame.font.Font("my_font/font1.ttf", 50)
# 渲染文字
text = my_font.render(text, True, (255, 255, 255))
view.blit(text, position)
3.最后一部分,游戏运行部分
加上这一段,可以帮助前面代码持续运行并保持至GAME OVER。
while True:
control()
# 监听有没有按下退出按钮
handleEvent()
# 更新屏幕内容
pygame.display.update()
#延时10毫秒
pygame.time.delay(10)
4.全部代码
全部代码如下:
#coding:utf-8
import pygame
from pygame.locals import *
import time
import random
import sys
import os
#初始化pygame环境
pygame.init ()
#创建一个长宽分别为1300/700窗口
os.environ[ 'SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 25)
canvas = pygame.display.set_mode((1000,625))
canvas.fill((255,255,255))
#设置窗口标题
pygame.display.set_caption("热狗大战")
#加载图片
bg = pygame.image.load("images/bg.jpg")
h = pygame.image.load("images/hotdog.png")
player = pygame.image.load("images/hero.png")
end=pygame.image.load("images/end.jpg")
bullet_tip=pygame.image.load("images/bullet_tip.png")
time_tip=pygame.image.load("images/time_tip.png")
def handleEvent():
for event in pygame.event.get():
if event.type == QUIT :
pygame.quit()
sys.exit()
if states == 'RUNNING':
if event.type == KEYDOWN and event.key == K_LEFT:
hero.left()
if event.type == KEYDOWN and event.key == K_RIGHT:
hero.right()
class Hero():
def __init__(self,x,y,img):
self.width=80
self.height=133
self.x=x
self.y=y
self.img=img
self.score=0
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def hit(self,c): #代表热狗
return c.x > self.x - c.width and \
c.x < self.x + self.width and \
c.y > self.y - c.height and \
c.y < self.y + self.height
def left(self):
self.x = self.x - 50
def right(self):
self.x = self.x + 50
def outOfBounds(self):
if self.x < 0:
self.x = self.x + 50
if self.x > 1000 - self.width:
self.x = self.x - 50
class Hotdog():
def __init__(self,x,y,img):
self.width=30
self.height=55
self.x=random.randint(0,(1000-self.width))
self.y=-self.height
self.img=img
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def step(self):
self.y=self.y+10
def isActionTime(lastTime,interval):
if lastTime==0:
return True
currentTime=time.time()
return currentTime-lastTime>=interval
def conEnter():
global lastTime
if isActionTime(lastTime,interval):
lastTime=time.time()
hotdogs.append(Hotdog(0,-55,h))
else:
return
def conPaint():
canvas.blit(bg,(0,0))
canvas.blit(bullet_tip,(800,5))
fillText(str(hero.score),(880,40))
hero.paint()
for d in hotdogs:
d.paint()
def conStep():
for d in hotdogs:
d.step()
hero=Hero(450, 450, player)
hotdogs=[]
lastTime=-100
interval=0.5
t=60
n=0
states = 'RUNNING' #设置游戏默认状态为运行状态
def gameTime():
global t,n
canvas.blit(time_tip,(20,20))
fillText(str(t),(35,35))
if n%50==0:
t=t-1
n=n+1
def checkHit(): #检测热狗是否与人物发生碰撞
for d in hotdogs:
if hero.hit(d):
hero.score = hero.score + 1
hotdogs.remove(d)
def control():
global states
if states == 'RUNNING':
conEnter()
conPaint()
conStep()
gameTime()
checkHit()
hero.outOfBounds()
if t <= 0:
states = 'OVER'
if states == 'OVER':
canvas.blit(end,(0,0))
if hero.score < 10:
fillTextOver(str(hero.score),(485,390))
else:
fillTextOver(str(hero.score),(470,390))
# 工具方法-写文字方法
def fillText(text, position, view=canvas):
# 设置字体样式和大小
my_font = pygame.font.Font("my_font/font1.ttf", 30)
# 渲染文字
text = my_font.render(text, True, (255, 255, 255))
view.blit(text, position)
def fillTextOver(text, position, view=canvas):
my_font = pygame.font.Font("my_font/font1.ttf", 50)
# 渲染文字
text = my_font.render(text, True, (255, 255, 255))
view.blit(text, position)
while True:
control()
# 监听有没有按下退出按钮
handleEvent()
# 更新屏幕内容
pygame.display.update()
#延时10毫秒
pygame.time.delay(10)
全文总共174行,打起来并不难,希望大家多打代码,多练手。想要素材请发邮箱注明“素材“二字,想要全部代码及素材请发邮箱并注明”代码”二字,发在评论区或私信都可以!
另外,本人第一次写博客,代码或解释有何不对可以指出!
记得点赞哦!!!