Python Pygame中的精灵健康
我正在做一个非常基本的游戏,我试图让鸟(玩家)躲开岩石,如果鸟被岩石击中,它就会死亡。但我不知道如何让游戏知道鸭子是否被石头击中 这是我的密码:Python Pygame中的精灵健康,python,pygame,Python,Pygame,我正在做一个非常基本的游戏,我试图让鸟(玩家)躲开岩石,如果鸟被岩石击中,它就会死亡。但我不知道如何让游戏知道鸭子是否被石头击中 这是我的密码: import os, sys import random import time img_path = os.path.join('C:\Python27', 'player.png') img_path2 = os.path.join('C:\Python27', 'rock.png') class Bird(object):
import os, sys
import random
import time
img_path = os.path.join('C:\Python27', 'player.png')
img_path2 = os.path.join('C:\Python27', 'rock.png')
class Bird(object):
def __init__(self):
self.image = pygame.image.load(img_path)
self.x = 0
self.y = 0
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 2
if key[pygame.K_DOWN]:
self.y += dist
elif key[pygame.K_UP]:
self.y -= dist
if key[pygame.K_RIGHT]:
self.x += dist
elif key[pygame.K_LEFT]:
self.x -= dist
def draw(self, surface):
surface.blit(self.image, (self.x, self.y))
def bird_health(self):
health = 1
if health ==0:
sys.exit()
def background(self, surface):
bg = os.path.join('C:\Python27', 'bg.png')
self.image2 = pygame.image.load(bg)
surface.blit(self.image2, (0,0))
class Rock(object):
def __init__(self, x=640, y=0,):
self.image = pygame.image.load(img_path2)
self.x = x
self.y = y
dist = 10
self.dist = dist
def rock(self):
dist = 10
self.x -=dist
def rock_draw(self, surface):
surface.blit(self.image, (self.x, self.y))
pygame.init()
screen = pygame.display.set_mode((640, 200))
bird = Bird() # create an instance
rock = Rock()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # quit the screen
running = False
if rock.x < 0:
y = random.randint(10, 190)
rock = Rock(640, y)
bird.handle_keys()
rock.rock()
screen.fill((255,255,255))
bird.background(screen)
bird.draw(screen)
rock.rock_draw(screen)
pygame.display.update()
clock.tick(40)
导入操作系统,系统
随机输入
导入时间
img_path=os.path.join('C:\Python27','player.png')
img_path2=os.path.join('C:\Python27','rock.png')
类鸟(对象):
定义初始化(自):
self.image=pygame.image.load(img\u路径)
self.x=0
self.y=0
def手柄_键(自):
key=pygame.key.get_pressed()
距离=2
如果键[pygame.K_DOWN]:
self.y+=dist
elif key[pygame.K_UP]:
self.y-=距离
如果键[pygame.K_RIGHT]:
self.x+=dist
elif键[pygame.K_左]:
self.x-=距离
def绘图(自、表面):
blit(self.image,(self.x,self.y))
def bird_健康(自我):
健康=1
如果运行状况==0:
sys.exit()
def背景(自身、表面):
bg=os.path.join('C:\Python27','bg.png')
self.image2=pygame.image.load(bg)
表面blit(自成像2,(0,0))
岩石类别(对象):
定义初始化(self,x=640,y=0,):
self.image=pygame.image.load(img_路径2)
self.x=x
self.y=y
距离=10
self.dist=dist
def rock(自我):
距离=10
self.x-=距离
def rock_draw(自身、表面):
blit(self.image,(self.x,self.y))
pygame.init()
screen=pygame.display.set_模式((640200))
bird=bird()#创建一个实例
岩石
clock=pygame.time.clock()
运行=真
运行时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()#退出屏幕
运行=错误
如果岩石x<0:
y=random.randint(10190)
岩石=岩石(640,y)
伯德,拿钥匙
摇滚乐
屏幕填充((255255))
鸟。背景(屏幕)
鸟。画(屏幕)
rock.rock_draw(屏幕)
pygame.display.update()
时钟滴答(40)
现在,我只想在鸟的健康值为0时退出它。我已经有一段时间没有做pygame了,但请参考以下内容: Pygame有一个内置的collide函数spritecollideany() 或者,您可以使用:
def checkCollision(bird, rock):
if bird.x == rock.x and bird.y == rock.y:
sys.exit()
在鸟类和岩石类中,确保x和y是可访问的。这只在鸟角与岩石角相等时有效,但您可以添加子句进行检查。例如:
if bird.x == rock.x and bird.y == rock.y or bird.x == rock.x + rock.length and...
这将根据鸟的中心位置展开 这是一个程序,我可以在其中检测球是否撞到桨,希望能有所帮助。所有在底部(在一个单独的代码框中)的代码就是我检测它们是否碰撞的代码
import pygame
# Constants
WIDTH = 700
HEIGHT = 500
SCREEN_AREA = pygame.Rect(0, 0, WIDTH, HEIGHT)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Initialization
pygame.init()
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.mouse.set_visible(0)
pygame.display.set_caption("Breakout Recreation WIP")
clock = pygame.time.Clock()
# Variables
paddle = pygame.Rect(350, 480, 50, 10)
ball = pygame.Rect(10, 250, 15, 15)
paddle_movement_x = 0
ball_direction = (1, 1)
balls = 3
done = False
while not done and balls > 0:
# Process events
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
paddle_movement_x = -2
elif keys[pygame.K_RIGHT]:
paddle_movement_x = 2
else:
paddle_movement_x = 0
# Move paddle
paddle.move_ip(paddle_movement_x, 0)
paddle.clamp_ip(SCREEN_AREA)
# Move ball
ball.move_ip(*ball_direction)
if ball.right > WIDTH or ball.left < 0:
ball_direction = -ball_direction[0], ball_direction[1]
elif ball.top < 0 or paddle.colliderect(ball):
ball_direction = ball_direction[0], -ball_direction[1]
elif ball.bottom > HEIGHT:
balls = balls - 1
ball_direction = (1, 1)
ball = pygame.Rect(10, 250, 15, 15)
ball.clamp_ip(SCREEN_AREA)
# Redraw screen
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, paddle)
pygame.draw.rect(screen, WHITE, ball)
pygame.display.flip()
clock.tick(100)
pygame.quit()
导入pygame
#常数
宽度=700
高度=500
屏幕面积=pygame.Rect(0,0,宽度,高度)
黑色=(0,0,0)
白色=(255,255,255)
#初始化
pygame.init()
screen=pygame.display.set_模式([宽度,高度])
pygame.mouse.set_可见(0)
pygame.display.set_标题(“突破娱乐WIP”)
clock=pygame.time.clock()
#变数
桨=pygame.Rect(350、480、50、10)
ball=pygame.Rect(10250,15,15)
桨叶移动量x=0
球的方向=(1,1)
球=3
完成=错误
未完成且球>0时:
#处理事件
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
keys=pygame.key.get_pressed()
如果键[pygame.K_左]:
桨叶运动量=2
elif keys[pygame.K_RIGHT]:
桨叶移动量x=2
其他:
桨叶移动量x=0
#动桨
桨叶移动ip(桨叶移动x,0)
挡板夹钳(屏幕区域)
#移动球
球。移动球ip(*球方向)
如果ball.right>宽度或ball.left<0:
球的方向=-球的方向[0],球的方向[1]
elif ball.top<0或桨叶碰撞矩形(ball):
球的方向=球的方向[0],-球的方向[1]
elif ball.bottom>高度:
球=球-1
球的方向=(1,1)
ball=pygame.Rect(10250,15,15)
球形夹具(屏蔽区)
#重绘屏幕
屏幕填充(黑色)
pygame.draw.rect(屏幕、白色、挡板)
pygame.draw.rect(屏幕、白色、球)
pygame.display.flip()
时钟滴答(100)
pygame.quit()
下面是我基本上改变球方向的代码:
# Move ball
ball.move_ip(*ball_direction)
if ball.right > WIDTH or ball.left < 0:
ball_direction = -ball_direction[0], ball_direction[1]
elif ball.top < 0 or paddle.colliderect(ball):
ball_direction = ball_direction[0], -ball_direction[1]
elif ball.bottom > HEIGHT:
balls = balls - 1
ball_direction = (1, 1)
ball = pygame.Rect(10, 250, 15, 15)
#移动球
球。移动球ip(*球方向)
如果ball.right>宽度或ball.left<0:
球的方向=-球的方向[0],球的方向[1]
elif ball.top<0或桨叶碰撞矩形(ball):
球的方向=球的方向[0],-球的方向[1]
elif ball.bottom>高度:
球=球-1
球的方向=(1,1)
ball=pygame.Rect(10250,15,15)
SpriteClimiteAny可以工作,但我想我必须创建矩形。最简单的方法是什么对不起,自从我使用pygame以来,它就一直存在。这个引用可能会有帮助:您需要做的是给它一个属性;也许把图像加载到rect上?我没有安装pygame,因此无法尝试检查——很抱歉,谢谢您的帮助!仅供参考。。。你忘了申报import pygame
没有,我只是复制并粘贴了它,我一定是漏掉了