如题,目前正在学习pygame,自己试着在做一个小球移动碰到敌人后结束的小游戏,但是想试下使用面向过程来写,现在面临了不知道该如何实现碰撞检测的尴尬局面
# coding=utf-8
import pygame
import sys
import time
def main():
# 初始化
pygame.init()
# 变量
# 屏幕尺寸
SIZE = WIDTH, HEIGHT = 800, 600
# 颜色
BLACK = 0, 0, 0
WHITE = 255, 255, 255
# 初始旋转速度
angle = 1
# 设置屏幕窗口大小
screen = pygame.display.set_mode(SIZE)
# 加载图片
# 加载齿轮图片
gear = pygame.image.load("./images/chilun.png")
ball = pygame.image.load("./images/ball.png")
award = pygame.image.load("./images/award.png")
# 加载背景音乐
pygame.mixer.init()
maoxiandao = pygame.mixer.Sound("./bgm/maoxiandao.mp3")
# 设置精灵位置
# 设置齿轮位置
gear_rect = gear.get_rect()
gear_rect = gear_rect.move((WIDTH - gear_rect.width) / 2, (HEIGHT - gear_rect.height) / 2)
gear_rect1 = gear_rect.move((WIDTH - gear_rect.width) / 3, (HEIGHT - gear_rect.height) / 3)
gear_rect2 = gear_rect.move(130, -150)
# 设置球初始位置
ball_rect = ball.get_rect()
ball_rect = ball_rect.move(100, 500)
# 设置终点的位置
award_rect = award.get_rect()
award_rect = award_rect.move(700, 50)
# 设置时钟
clock = pygame.time.Clock()
# 设置速度
speed = 2
while True:
# pygame开始惯例设置关闭
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
maoxiandao.play()
# 设置图片大小
# 设置齿轮1的大小
new_gear1 = pygame.transform.scale(gear, (150, 150))
# 设置齿轮2的大小
new_gear2 = pygame.transform.scale(gear, (100, 100))
# 设置小球的大小
new_ball = pygame.transform.scale(ball, (50, 50))
# 设置小球中心点
new_ball_rect = new_ball.get_rect(center=ball_rect.center)
# 判断小球是否碰到边界
if new_ball_rect.left <= 0:
new_ball_rect.left = 0
if new_ball_rect.right >= WIDTH:
new_ball_rect.right = WIDTH
if new_ball_rect.top <= 0:
new_ball_rect.top = 0
if new_ball_rect.bottom >= HEIGHT:
new_ball_rect.bottom = HEIGHT
# 设置旋转
# 齿轮旋转
new_gear = pygame.transform.rotate(gear, angle)
new_rect = new_gear.get_rect(center=gear_rect.center)
# 齿轮旋转1
new_gear1 = pygame.transform.rotate(new_gear1, angle)
new_rect1 = new_gear1.get_rect(center=gear_rect1.center)
# 齿轮旋转2
new_gear2 = pygame.transform.rotate(new_gear2, angle)
new_rect2 = new_gear2.get_rect(center=gear_rect2.center)
# 终点旋转
new_award = pygame.transform.rotate(award, angle)
new_award_rect = new_award.get_rect(center=award_rect.center)
# 设置旋转速度
angle += 1
# 控制小球移动
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
# print('右')
ball_rect.x += speed
# print(new_ball_rect.center)
if keys[pygame.K_LEFT]:
# print('左')
ball_rect.x -= speed
if keys[pygame.K_UP]:
# print('上')
ball_rect.y -= speed
if keys[pygame.K_DOWN]:
# print('下')
ball_rect.y += speed
# 设置背景颜色
screen.fill(WHITE)
# 将齿轮放在屏幕上
screen.blit(new_gear, new_rect)
screen.blit(new_gear1 , new_rect1)
screen.blit(new_gear2, new_rect2)
screen.blit(new_ball, new_ball_rect)
screen.blit(new_award, new_award_rect)
# 辅助线
pygame.draw.rect(screen, (255, 0, 0), new_rect, 1)
pygame.draw.rect(screen, (0, 255, 0), new_rect1, 1)
pygame.draw.rect(screen, (0, 0, 0), new_rect2, 1)
pygame.draw.rect(screen, (0, 0, 255), ball_rect, 1)
pygame.draw.rect(screen, (173, 0, 0), award_rect, 1)
# 刷新屏幕
pygame.display.flip()
# 设置屏幕的刷新频率
clock.tick(60)
if __name__ == '__main__':
main()
碰撞的实现思路,是判断两个矩形是否相交,也就是说小球所在的矩形和敌人所在的矩形是否相交。
我没学过python,但是C#有个方法,你可以参考下:
//
// 摘要:
// 确定此矩形是否与 rect 相交。
//
// 参数:
// rect:
// 要测试的矩形。
//
// 返回结果:
// 如果有重叠,此方法将返回 true;否则将返回 false。
public bool IntersectsWith(Rectangle rect);
//小球矩形
smallBallRect = new Rectangle(this.X, this.Y, width, height);
//敌人矩形
enemyRect = new Rectangle(this.X, this.Y, width, height);
//调用方式
bool bIsHit = smallBallRect.IntersectsWith(enemyRect);
博问回答支持 markdown 代码高亮
好的谢谢 我参考试下