1. 程式人生 > >基於pygame做的小遊戲

基於pygame做的小遊戲

最近一邊學習pygame模組一邊做了個小遊戲,完成功能如下:

  1. 滑鼠右鍵控制移動
  2. 人物跟隨滑鼠方向轉動
  3. 滑鼠左鍵控制攻擊
  4. 動畫效果
  5. 血量計數效果
    這裡寫圖片描述
    畫面比較亂入,請自行過濾。直接上程式碼:
# coding: utf-8
import pygame
from pygame.locals import *     # 匯入pygame庫中的一些常量
from sys import exit            # 匯入sys庫中的exit函式
from gameobjects.vector2 import Vector2
from math import *

# 定義視窗的解析度
SCREEN_WIDTH = 603
SCREEN_HEIGHT = 603 # 初始化遊戲 pygame.init() screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT], 0, 32) # 初始化一個用於顯示的視窗 pygame.display.set_caption('LOL') # 設定視窗標題 #載入並轉換影象 background = pygame.image.load("background.jpg").convert() # 載入圖片資源 major_cursor = pygame.image.load("major.png"
).convert_alpha() mouse_cursor = pygame.image.load("cursor.png").convert_alpha() fire_cursor = pygame.image.load("fire.png").convert_alpha() boom_cursor = pygame.image.load("boom.png").convert_alpha() major_pos_new = major_pos_old = (20.0, 550.0) mouse_pos = (300.0, 300.0) sunflower_pos = (35, 420) bullets = [] def
load_image(file, width=None, number=None):
try: surface = pygame.image.load(file).convert_alpha() except pygame.error: raise SystemExit('Could not load image "%s" %s'%(file, pygame.get_error())) if width == None: return surface height = surface.get_height() return [surface.subsurface( Rect((i * width, 0), (width, height)) ) for i in range(number)] # 返回切片後的圖片列表,按幀控制顯示 class SunFlower(pygame.sprite.Sprite): _rate = 100 _width = 82 _height = 77 _number = 18 _life = 100 images = [] def __init__(self): self.order = 0 pygame.sprite.Sprite.__init__(self) if len(self.images) == 0: self.images = load_image("sunflower.png", self._width, self._number) self.image = self.images[self.order] self.rect = Rect(0, 0, self._width, self._height) self.life = self._life self.passed_time = 0 def update(self, passed_time): self.passed_time += passed_time self.order = ( self.passed_time // self._rate ) % self._number if self.order == 0 and self.passed_time > self._rate: self.passed_time = 0 return self.images[self.order] if __name__ == '__main__': sunflower = SunFlower() clock = pygame.time.Clock() screen.blit(major_cursor, major_pos_old) # 主角 #遊戲主迴圈 while True: screen.blit(background, (0,0)) # 背景 # major_pos_old = straight_move(major_pos_old, major_pos_new) for event in pygame.event.get(): #接收到退出事件後退出程式 if event.type == QUIT: exit() elif event.type == MOUSEBUTTONDOWN: pressed_array = pygame.mouse.get_pressed() for index in range(len(pressed_array)): if pressed_array[index]: if index == 0: major_pos_new = major_pos_old bullet_pos = (major_pos_old[0], major_pos_old[1]) vector_direction = Vector2.from_points(bullet_pos, pygame.mouse.get_pos()) vector_direction.normalize() bullets.append([bullet_pos, vector_direction]) # 新增子彈位置及方向 elif index == 1: pass elif index == 2: major_pos_new = pygame.mouse.get_pos() time_passed = clock.tick() for bullet in bullets: print(bullet[0][0], bullet[0][1]) if (int(bullet[0][0]) in range(20, 70)) and (int(bullet[0][1]) in range(410, 460)): sunflower.life -= 10 bullets.remove(bullet) elif (bullet[0][0] > 0 and bullet[0][0] < 603) and (bullet[0][1] > 0 or bullet[0][1] < 603): bullet[0] += bullet[1] * time_passed * 0.5 # 設定子彈按幀移動 screen.blit(fire_cursor, bullet[0]) else: bullets.remove(bullet) vector_to_new = Vector2.from_points(major_pos_old, major_pos_new) vector_to_new.normalize() major_pos_old += vector_to_new * time_passed * 0.1 vector_mouse = Vector2.from_points(major_pos_old, pygame.mouse.get_pos()) vector_mouse.normalize() # print(degrees(acos(vector_mouse[0]))) if vector_mouse[1] >= 0: derection = -1 else: derection = 1 if vector_mouse[0] >= 0: major_sufurce = major_cursor else: major_sufurce = pygame.transform.flip(major_cursor, False, True) # 根據Y軸翻轉 major_cursor_ratation = pygame.transform.rotate(major_sufurce, derection * degrees(acos(vector_mouse[0]))) # 設定偏移角度 screen.blit(major_cursor_ratation, (major_pos_old[0]-30, major_pos_old[1]-15)) if sunflower.life > 0: img = sunflower.update(time_passed) screen.blit(img, sunflower_pos) my_font = pygame.font.SysFont("arial", 15) # 設定血量 text_surface = my_font.render(str(sunflower.life), True, (255,0,0)) screen.blit(text_surface, (sunflower_pos[0]+25, sunflower_pos[1]-15)) else: screen.blit(boom_cursor, sunflower_pos) screen.blit(mouse_cursor, pygame.mouse.get_pos()) pygame.mouse.set_visible(False) # 設定滑鼠不可見 pygame.display.update() #重新整理畫面