关于pygame跑酷游戏的示例
立即下载
资源介绍:
关于pygame跑酷游戏的示例
import pygame
import sys
import json
from player import Player
WIDTH = 800
HEIGHT = 600
BG = (0, 221, 225)
FPS=60
class Index:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.screenrect = self.screen.get_rect()
pygame.display.set_caption("PolandBall Attack Beta-1.0")
self.map = self.map_open("index_map")
self.run_game()
def run_game(self):
self.center_x, self.center_y = 0, 0
self.player = Player("RUSSIA", self)
self.fpsclock=pygame.time.Clock()
while True:
self.screen.fill(BG)
self.check_events()
self.update()
self.draw()
pygame.display.flip()
self.fpsclock.tick(FPS)
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
def quit(self):
pygame.quit()
sys.exit()
def update(self):
self.player.update()
# 更新中心点的位置,可以根据具体情况调整
self.center_x = self.player.rect.x - WIDTH // 2
self.center_y = self.player.rect.y - HEIGHT // 2
def draw(self):
self.draw_map()
self.player.draw()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.player.move(-5, 0)
if keys[pygame.K_RIGHT]:
self.player.move(5, 0)
if keys[pygame.K_UP]:
self.player.jump()
def draw_map(self):
for item in self.map:
self.draw_in_center_map(item, (0, 0, 0))
def draw_in_center_map(self, rect, color):
rect = rect.copy()
rect.x -= self.center_x
rect.y -= self.center_y
pygame.draw.rect(self.screen, color, rect)
def map_open(self, mapname):
with open(f"maps/{mapname}.map") as file:
mapo = json.load(file)
maps = []
for item in mapo:
maps.append(pygame.Rect(item[0], item[1], item[2], item[3]))
return maps
if __name__ == "__main__":
Index()