1. 程式人生 > 其它 >python 小球碰撞遊戲

python 小球碰撞遊戲

#小球和擋板要自己找照片,放在一個單獨資料夾,音樂也是一樣的
import pygame pygame.init()#遊戲資源載入 a
= 700#x軸為700 b = 800#y抽為800 sceeen = pygame.display.set_mode((a,b))#建立遊戲視窗 image_ball = pygame.image.load("../image/ball.gif")#載入小球 image_ball_1 = pygame.image.load("../image/123.png")#新增擋板 font = pygame.font.SysFont("kaiti", 30)#新增字型 image_ball_rect_2
= image_ball_1.get_rect() image_ball_rect = image_ball.get_rect()#獲取矩形框 yyue= pygame.mixer.music.load("../image/Saiakoup - Crilwa.mp3")#新增背景音樂 pygame.mixer.music.play(-1)#播放音樂 still = False key = True#如果key 為假 就結束迴圈 x=1 y=1 number=0 while key: # 事件處理 for event in pygame.event.get(): if
event.type == pygame.QUIT: key = False break if event.type == pygame.QUIT: pygame.quit() break # 控制小球方向鍵盤控制 if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: if x > 0: x
-= 1 if x < 0: x += 1 if event.key == pygame.K_RIGHT: x = x+1 if x >= 0 else x-1 if event.key == pygame.K_UP: y = y + 1 if y >= 0 else y-1 if event.key == pygame.K_DOWN: if y > 0: y -= 1 if y < 0: y += 1 if event.type == pygame.MOUSEMOTION: if event.buttons[0] == 1: image_ball_rect = image_ball_rect.move(event.pos[0]-image_ball_rect.x, event.pos[1]-image_ball_rect.y) image_ball_rect_2[0] = event.pos[0] image_ball_rect_2[1] = b -90#設定擋板位置高度 sceeen.fill((255,182,193))#背景顏色 #內部邏輯 image_ball_rect = image_ball_rect.move(x, y) if image_ball_rect.left < 0 or image_ball_rect.right > a: x = -x if image_ball_rect.top < 0 or image_ball_rect.bottom > b: y = -y if not still: image_ball_rect = image_ball_rect.move(x, y) if pygame.Rect.colliderect(image_ball_rect_2, image_ball_rect) and image_ball_rect.bottom - image_ball_rect_2.top <= 1: y = -y number+=1 sceeen.blit(image_ball,image_ball_rect)#更新小球 sceeen.blit(image_ball_1, image_ball_rect_2) image_font = font.render("分為:%s" % number, True, (0, 0, 0)) sceeen.blit(image_font, (10, 10))#計分器的位置 pygame.display.update()#更新畫面 #退出遊戲 pygame.quit()