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)