pygame - 2djump - 项目备份2022-4-16
import pygame
import os
from pygame.locals import *
from sys import exit
# 声明全局变量 SCREEN_WIDTH = 1600 # 窗口分辨率 SCREEN_HEIGHT = 900 keylist_ad = [pygame.K_a, pygame.K_d] # 按键列表 FPS = 60 # 刷新率
# 初始化游戏 pygame.init() # 初始化pygame screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) # 初始化窗口 clock = pygame.time.Clock() # 初始化时钟对象 pygame.display.set_caption('This is my first pygame-program') # 设置窗口标题
# 载入背景图 background = pygame.image.load('assets/background - 1600x900.png') # 载入精灵图 player_img = pygame.image.load('assets/sprite - player - 1.png') rock_img = pygame.image.load('assets/sprite - rock - 1.png')
# 初始化对象属性: player player_rect = pygame.Rect(0, 64, 0, 64) # player_mask = [0, 0, 64, 64] # 碰撞检测用的遮罩(x1,y1,x2,y2) player_pos = [800, 450] # 初始位置 player_hspeed = 10 # 横向速度绝对值 player_vspeed = 0 # 纵向速度初始化 player_gravity = 1.5 # 重力系数 # 初始化键盘时刻 (ticks) ticks_K_a = 0 ticks_K_d = 0 # 初始化横向移动系数 [1,-1] sign_direction_horizonal = 0
# 初始化对象属性: rock rock_rect = pygame.Rect(0, 64, 0, 64) rock_mask = [0, 0, 64, 64] rock_pos = [800, 600]
# 主事件循环 while True: # 设置刷新频率 clock.tick(FPS)
# 检测键盘按住的键位 累加键盘时刻 list_key_pressed = pygame.key.get_pressed() if list_key_pressed[pygame.K_a]: ticks_K_a += 1 else: ticks_K_a = 0 if list_key_pressed[pygame.K_d]: ticks_K_d += 1 else: ticks_K_d = 0
# 当a和d的键盘时刻都不为0时 取两键盘时刻的较小者 作为有效按键 修改横向速度方向系数 if ticks_K_a != 0 and ticks_K_d != 0: if ticks_K_a <= ticks_K_d: sign_direction_horizonal = -1 else: sign_direction_horizonal = 1
# 当a或d两个键其中一个键的键盘时刻不为0时 若另一个为0 则取不为0者作为有效按键 # 修改横向速度方向系数 if ticks_K_a > ticks_K_d and ticks_K_d == 0: sign_direction_horizonal = -1 if ticks_K_a < ticks_K_d and ticks_K_a == 0: sign_direction_horizonal = 1
# 当两个键的键盘时刻都为0时 修改横向速度方向系数为0 if ticks_K_a == 0 and ticks_K_d == 0: sign_direction_horizonal = 0
# 绘制背景 screen.blit(background, (0, 0))
# 遍历游戏事件 for event in pygame.event.get():
# 处理游戏退出 if event.type == pygame.QUIT: pygame.quit() exit()
# 计算对象属性 player_hspeed = sign_direction_horizonal * 10 player_vspeed += player_gravity
# 计算精灵位置 player_pos[0] += player_hspeed player_pos[1] += player_vspeed
# 绘制精灵 screen.blit(player_img, player_pos) screen.blit(rock_img, rock_pos)
# 更新屏幕 pygame.display.update()
# 刷新控制台 # os.system('cls')
# 声明全局变量 SCREEN_WIDTH = 1600 # 窗口分辨率 SCREEN_HEIGHT = 900 keylist_ad = [pygame.K_a, pygame.K_d] # 按键列表 FPS = 60 # 刷新率
# 初始化游戏 pygame.init() # 初始化pygame screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) # 初始化窗口 clock = pygame.time.Clock() # 初始化时钟对象 pygame.display.set_caption('This is my first pygame-program') # 设置窗口标题
# 载入背景图 background = pygame.image.load('assets/background - 1600x900.png') # 载入精灵图 player_img = pygame.image.load('assets/sprite - player - 1.png') rock_img = pygame.image.load('assets/sprite - rock - 1.png')
# 初始化对象属性: player player_rect = pygame.Rect(0, 64, 0, 64) # player_mask = [0, 0, 64, 64] # 碰撞检测用的遮罩(x1,y1,x2,y2) player_pos = [800, 450] # 初始位置 player_hspeed = 10 # 横向速度绝对值 player_vspeed = 0 # 纵向速度初始化 player_gravity = 1.5 # 重力系数 # 初始化键盘时刻 (ticks) ticks_K_a = 0 ticks_K_d = 0 # 初始化横向移动系数 [1,-1] sign_direction_horizonal = 0
# 初始化对象属性: rock rock_rect = pygame.Rect(0, 64, 0, 64) rock_mask = [0, 0, 64, 64] rock_pos = [800, 600]
# 主事件循环 while True: # 设置刷新频率 clock.tick(FPS)
# 检测键盘按住的键位 累加键盘时刻 list_key_pressed = pygame.key.get_pressed() if list_key_pressed[pygame.K_a]: ticks_K_a += 1 else: ticks_K_a = 0 if list_key_pressed[pygame.K_d]: ticks_K_d += 1 else: ticks_K_d = 0
# 当a和d的键盘时刻都不为0时 取两键盘时刻的较小者 作为有效按键 修改横向速度方向系数 if ticks_K_a != 0 and ticks_K_d != 0: if ticks_K_a <= ticks_K_d: sign_direction_horizonal = -1 else: sign_direction_horizonal = 1
# 当a或d两个键其中一个键的键盘时刻不为0时 若另一个为0 则取不为0者作为有效按键 # 修改横向速度方向系数 if ticks_K_a > ticks_K_d and ticks_K_d == 0: sign_direction_horizonal = -1 if ticks_K_a < ticks_K_d and ticks_K_a == 0: sign_direction_horizonal = 1
# 当两个键的键盘时刻都为0时 修改横向速度方向系数为0 if ticks_K_a == 0 and ticks_K_d == 0: sign_direction_horizonal = 0
# 绘制背景 screen.blit(background, (0, 0))
# 遍历游戏事件 for event in pygame.event.get():
# 处理游戏退出 if event.type == pygame.QUIT: pygame.quit() exit()
# 计算对象属性 player_hspeed = sign_direction_horizonal * 10 player_vspeed += player_gravity
# 计算精灵位置 player_pos[0] += player_hspeed player_pos[1] += player_vspeed
# 绘制精灵 screen.blit(player_img, player_pos) screen.blit(rock_img, rock_pos)
# 更新屏幕 pygame.display.update()
# 刷新控制台 # os.system('cls')