Pygame小游戏:无敌斗牛士
哔哩哔哩视频网址:https://www.bilibili.com/video/BV1ZP4y167cZ
众所周知,马上就春节了,那就是兔年了,今天我就教大家怎么把牛年的牛给抓回来……(逃
1.图片素材
做个游戏,没有素材肯定是不行的
但是小编已经给大家准备好了,给大家放在最后了。
2.直接开干
首先,留下最基本的框架(没有配置好pygame的点这里配置)
#coding:utf-8
import pygame
from pygame.locals import * # 导入pygame包
pygame.init() #初始化pygame
canvas = pygame.display.set_mode((1000,700))
pygame.display.set_caption("无敌斗牛士")
while True:
pygame.display.update() # 更新屏幕内容
接着,导入素材
bg = pygame.image.load("images/bg.png")
bull_img = pygame.image.load("images/bull.png")
rope_img = pygame.image.load("images/rope.png")
win = pygame.image.load("images/win.png")
lose = pygame.image.load("images/lose.png")
bow = pygame.image.load("images/bow.png")
设置事件
state = "RUNNING"
on_off = 0
def handleEvent():
global on_off,state
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
on_off = 1
创建绳索类
class Rope():
def __init__(self,x,y,width,height,img):
self.x = x
self.y = y
self.width = width
self.height = height
self.img = img
self.speedY = -1
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def move(self):
global on_off
if on_off == 1:
self.y += self.speedY * 10
if self.y < 400:
self.speedY = 1
elif self.y > 600:
self.speedY = -1
on_off = 0
def hit(self,b):
return self.y == b.y and self.x + 40 > b.x and self.x < b.x + 40
rope = Rope(385,600,39,52,rope_img)
创建牛类
class Bull():
def __init__(self,x,y,width,height,img):
self.x = x
self.y = y
self.width = width
self.height = height
self.img = img
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def move(self):
self.x = self.x + 10
if self.x > 1000:
self.x = 0
bull = Bull(0,400,192,141,bull_img)
设置侦测事件
def comPaint():
canvas.blit(bg,(0,0))
bull.paint()
rope.paint()
def comMove():
bull.move()
rope.move()
def checkHit():
global on_off,state
if rope.hit(bull):
on_off = 0
state = "SUCCESS"
最后留下循环
while True:
if state == "RUNNING":
comPaint()
comMove()
checkHit()
elif state == "SUCCESS":
comPaint()
canvas.blit(win,(0,0))
handleEvent()
pygame.display.update()
pygame.time.delay(10)
完整代码如下(我中间加了一些输出的调试,对游戏运行没什么太大的影响):
import pygame,sys
from pygame.locals import *
pygame.init()
canvas = pygame.display.set_mode((1000,700))
pygame.display.set_caption("无敌斗牛士")
bg = pygame.image.load("images/bg.png")
bull_img = pygame.image.load("images/bull.png")
rope_img = pygame.image.load("images/rope.png")
win = pygame.image.load("images/win.png")
lose = pygame.image.load("images/lose.png")
bow = pygame.image.load("images/bow.png")
print("测试Test")
print("[测试Test:1]碰撞检测:False")
state = "RUNNING"
print("[测试Test:4]state更改为RUNNING")
on_off = 0
def handleEvent():
global on_off,state
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
print("[测试Test:2]结束测试Test")
print("结束测试Test")
if state == "RUNNING":
raise UserWarning("[Error:1]中途退出")
pygame.quit()
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
on_off = 1
print("[测试Test:3]on_off更改为1")
class Rope():
def __init__(self,x,y,width,height,img):
self.x = x
self.y = y
self.width = width
self.height = height
self.img = img
self.speedY = -1
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def move(self):
global on_off
if on_off == 1:
self.y += self.speedY * 10
if self.y < 400:
self.speedY = 1
elif self.y > 600:
self.speedY = -1
on_off = 0
print("[测试Test:3]on_off更改为0")
def hit(self,b):
return self.y == b.y and self.x + 40 > b.x and self.x < b.x + 40
class Bull():
def __init__(self,x,y,width,height,img):
self.x = x
self.y = y
self.width = width
self.height = height
self.img = img
def paint(self):
canvas.blit(self.img,(self.x,self.y))
def move(self):
self.x = self.x + 10
if self.x > 1000:
self.x = 0
def comPaint():
canvas.blit(bg,(0,0))
bull.paint()
rope.paint()
def comMove():
bull.move()
rope.move()
def checkHit():
global on_off,state
if rope.hit(bull):
print("[测试Test:1]碰撞检测:True")
on_off = 0
state = "SUCCESS"
print("[测试Test:4]state更改为SUCCESS")
print("[测试Test:5]停止运动")
rope = Rope(385,600,39,52,rope_img)
print("[测试Test:6]rope创建")
bull = Bull(0,400,192,141,bull_img)
print("[测试Test:6]bull创建")
while True:
if state == "RUNNING":
comPaint()
comMove()
checkHit()
elif state == "SUCCESS":
comPaint()
canvas.blit(win,(0,0))
handleEvent()
pygame.display.update()
pygame.time.delay(10)
给大家看一下游戏效果
3.总结
总体来说,这还是一个很简单的游戏,
游戏源码我放这里了,点击跳转页面下载
喜欢的话点个赞,收藏关注,在分享给你的朋友们吧~