|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我刚刚用pygame做了个超简单超简单的打砖块游戏(一共才40多行),可是这里面的球与砖块的碰撞检测我还是不是太清楚。目前暂时用了X,Y分别进行匹配的方法。可是有时候侦测的有点慢,显现出我的球是有白色背景的(无奈),甚至少时候会穿过球,减一分...求大神帮助。代码如下(test1是砖块,test2是球,自己找素材):
- """用pygame开发一款打砖块的游戏"""
- # 初始化
- import pygame, sys, random, pygame.freetype
- # 开始运行
- if __name__ == "__main__":
- pygame.init()
- screen = pygame.display.set_mode((600, 400))
- test1 = pygame.image.load("test1.png")
- test2 = pygame.image.load("test2.png")
- t1_location = (190, 280)
- t2_location = (240, 25)
- speed = speed = random.randint(0, 3) * 2 + -3
- clock = pygame.time.Clock()
- file = pygame.freetype.Font("C://windows//Fonts//msyh.ttc", 36)
- score = 0
- note = file.render_to(screen, (260, 30), "Your score is:" + str(score), fgcolor=pygame.Color("black"), size=36)
- while True:
- t1_location = (pygame.mouse.get_pos()[0], t1_location[1])
- if not t2_location[0] < 0 and not t2_location[0] > 580:
- t2_location = (t2_location[0]+speed, t2_location[1]+1)
- else:
- speed = -speed
- t2_location = (t2_location[0]+speed, t2_location[1]+1)
- if t2_location[1] >= 260:
- if t2_location[1] >= 390:
- t2_location = (t1_location[0], 25)
- speed = random.randint(0, 3) * 2 + -3
- score -= 1
- elif t1_location[0] < t2_location[0]+30 and t1_location[0] > t2_location[0]-30:
- score += 1
- t2_location = (t1_location[0], 25)
- speed = random.randint(0, 3) * 2 + -3
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- screen.fill(pygame.Color("white"))
- screen.blit(test1, t1_location)
- screen.blit(test2, t2_location)
- note = file.render_to(screen, (200, 30), "Your score is:" + str(score), fgcolor=pygame.Color("black"), size=36)
- pygame.display.update()
- clock.tick(160)
复制代码 |
评分
-
查看全部评分
|