CQPEED 2024-08-05 17:43 采纳率: 0%
浏览 6
已结题

pygame按键不响应



```python
import pygame
import sys
import json

WIDTH = 800
HEIGHT = 600
BG = (0, 221, 225)

class Index:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        self.screenrect = self.screen.get_rect()
        self.map = self.map_open("index_map")
        print(self.map)
        self.run_game()

    def run_game(self):
        self.center_x, self.center_y = 0, 0
        self.player = Player("index", self)
        
        while True:
            self.screen.fill(BG)
            self.check_events()
            self.update()
            self.draw()
            pygame.display.flip()

    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(-1, 0)
        if keys[pygame.K_RIGHT]:
            self.player.move(1, 0)
        if keys[pygame.K_SPACE]:
            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

class Player:
    def __init__(self, country, arg):
        self.arg = arg
        self.img = pygame.image.load(f"countries/{country}.png")
        self.rect = self.img.get_rect()
        self.rect.x = 400
        self.rect.y = 200
        self.downspeed=0

    def draw(self):
        # 计算实际绘制位置
        draw_x = self.rect.x - self.arg.center_x
        draw_y = self.rect.y - self.arg.center_y
        self.arg.screen.blit(self.img, (draw_x, draw_y))

    def is_collide(self):
        for item in self.arg.map:
            if self.rect.colliderect(item):
                return True
        return False

    def move(self, xadd=0, yadd=0):
        self.rect.x += xadd
        self.rect.y += yadd
        if self.is_collide():
            self.rect.x -= xadd
            self.rect.y -= yadd
    def down(self):
        self.rect.y+=self.downspeed
        self.nowy=self.rect.y
        if self.is_collide():
            self.rect.y=self.nowy-1
        else:
            self.downspeed+=0.01
    def update(self):
        self.down()
    def jump(self):
        if self.is_collide():
            self.downspeed=3
if __name__ == "__main__":
    Index()


这按空格和按AD都不起作用,请问怎么回事?

  • 写回答

1条回答 默认 最新

  • CQPEED 2024-08-06 07:07
    关注

    找到了,原来那个角色一直浮在在地图上

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月14日
  • 已采纳回答 8月6日
  • 创建了问题 8月5日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见