python热狗大战

**热狗大战 **
最近看了王思聪吃热狗的图,感觉很有趣,所以就做了一个小游戏,来给大家分享一下!
先让大家看一下效果
游戏界面
游戏界面
在这里插入图片描述
结束界面
怎么样?不错吧?
今天就来做一下这个游戏

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行,打起来并不难,希望大家多打代码,多练手。想要素材请发邮箱注明“素材“二字,想要全部代码及素材请发邮箱并注明”代码”二字,发在评论区或私信都可以!
另外,本人第一次写博客,代码或解释有何不对可以指出!
记得点赞哦!!!

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#import ... 使用库函数需要 库.函数 不会出现函数重名问题 #from .... import* 则可以直接使用函数名 #import .... as ....使代码量更少且不容易重名 import turtle #设置窗体大小和位置,4个参数后两个可选为左上角坐标,setup函数不是必须的 turtle.setup(650,350,200,200) #绝对坐标 turtle.goto(x,y)海龟一开始在画面的心(0,0),到达(x,y) #相对坐标 海龟当前运行的方向是前进方向 turtle.fd(d) 或turtle.forward(d) #后方是后退方向 turtle.bk(d) #左侧是左侧方向 turtle.circle(r,angle)以左侧的某个点为圆心向左侧 #右侧是右侧方向 # 画笔控制函数pen... # 一般成对出现: turtle.penup() 别名 turtle.pu() 不画 # turtle.pendown() turtle.pd() 画 # #画笔宽度设置后一直有效 turtle.pensize(width) 或 turtle.width(width) #画笔颜色 turtle.pencolor("purple")或 turtle.pencolor(0.63,0.13,0.94) 或 turtle.pencolor((0.63,0.13,0.94)) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) turtle.pencolor("purple") #绝对角度 turtle角度坐标体系,类似数学平面直角坐标系,turtle.seth(angle)改变海龟行进角度,但不行进 或turtle.setheading() #相对角度 turtle.left(angle)向左改变角度 turtle.right(angle)向右改变角度 turtle.seth(-40) #turtle.circle(r,extent=None)绘制弧 默认圆心是左侧r距离的位置,弧度为360 -r右侧 for i in range(4): turtle.circle(40,80) turtle.circle(-40,80) turtle.circle(40,80/2) turtle.fd(40) turtle.circle(16,180) turtle.fd(40*2/3) turtle.done()

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值