python热狗大战
生活随笔
收集整理的这篇文章主要介绍了
python热狗大战
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
**热狗大战 **
最近看了王思聪吃热狗的图,感觉很有趣,所以就做了一个小游戏,来给大家分享一下!
先让大家看一下效果
游戏界面
结束界面
怎么样?不错吧?
今天就来做一下这个游戏
1.基础部分
1.1导入模块
这个游戏中有4个模块,分别是pygame,os,random,time,sys。
导入都可以用pip方法。
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键盘反应
其实这个是挺重要的,没键盘基本 啥都做不成。
看代码:
2.运行部分
2.1类的使用
#英雄类 class Hero():def __init__(self,x,y,img):self.width=80self.height=133self.x=xself.y=yself.img=imgself.score=0def 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.heightdef left(self):self.x = self.x - 50def right(self):self.x = self.x + 50def outOfBounds(self):if self.x < 0:self.x = self.x + 50if self.x > 1000 - self.width:self.x = self.x - 50#热狗 类 class Hotdog():def __init__(self,x,y,img):self.width=30self.height=55self.x=random.randint(0,(1000-self.width))self.y=-self.heightself.img=imgdef paint(self):canvas.blit(self.img,(self.x,self.y))def step(self):self.y=self.y+102.2后台工具
主要是用于加载绘制!
def isActionTime(lastTime,interval):if lastTime==0:return TruecurrentTime=time.time()return currentTime-lastTime>=intervaldef conEnter():global lastTimeif isActionTime(lastTime,interval):lastTime=time.time()hotdogs.append(Hotdog(0,-55,h))else:returndef 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,ncanvas.blit(time_tip,(20,20))fillText(str(t),(35,35))if n%50==0:t=t-1n=n+1def checkHit(): #检测热狗是否与人物发生碰撞for d in hotdogs:if hero.hit(d):hero.score = hero.score + 1hotdogs.remove(d)def control():global statesif 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=80self.height=133self.x=xself.y=yself.img=imgself.score=0def 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.heightdef left(self):self.x = self.x - 50def right(self):self.x = self.x + 50def outOfBounds(self):if self.x < 0:self.x = self.x + 50if self.x > 1000 - self.width:self.x = self.x - 50class Hotdog():def __init__(self,x,y,img):self.width=30self.height=55self.x=random.randint(0,(1000-self.width))self.y=-self.heightself.img=imgdef paint(self):canvas.blit(self.img,(self.x,self.y))def step(self):self.y=self.y+10def isActionTime(lastTime,interval):if lastTime==0:return TruecurrentTime=time.time()return currentTime-lastTime>=intervaldef conEnter():global lastTimeif isActionTime(lastTime,interval):lastTime=time.time()hotdogs.append(Hotdog(0,-55,h))else:returndef 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,ncanvas.blit(time_tip,(20,20))fillText(str(t),(35,35))if n%50==0:t=t-1n=n+1def checkHit(): #检测热狗是否与人物发生碰撞for d in hotdogs:if hero.hit(d):hero.score = hero.score + 1hotdogs.remove(d)def control():global statesif 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行,打起来并不难,希望大家多打代码,多练手。想要素材请发邮箱注明“素材“二字,想要全部代码及素材请发邮箱并注明”代码”二字,发在评论区或私信都可以!
另外,本人第一次写博客,代码或解释有何不对可以指出!
记得点赞哦!!!
总结
以上是生活随笔为你收集整理的python热狗大战的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: windows下安装linux环境
- 下一篇: python实现自适应中值滤波器