pygame 相关问题

pygame是一组用于编写视频游戏的Python模块。只有在使用pygame库时才使用此标记,如果您对python中的游戏编程有疑问,请不要使用此标记。

如何圆化多边形的边缘?

我想在 pygame 中圆化多边形的边缘,希望没有任何额外的模块。 这是我的一些代码: 导入pygame pg.init() 屏幕=pg.set_mode((200,200)) 屏幕.填充(255,255,25...

回答 1 投票 0

有什么方法可以使 Pygame 代码(通常打包为 .exe)可修改吗?

我有一个用 Pygame 制作的飞行模拟器游戏,我想知道是否有办法让它可模组化。例如,一个模组添加了一个可以在空中自毁飞机的钥匙,或者一个......

回答 1 投票 0

PYGAME - 创建时钟并定期执行操作

我正在制作一款游戏,屏幕顶部的移动立方体将以一定的间隔向屏幕下方发射立方体。我该怎么做呢。例如,我希望每 1 秒移动一次...

回答 2 投票 0

pygame 中的定位和物理问题。角色碰撞后立即向相反方向跳跃

我的角色在与瓷砖碰撞后会直接跳跃。它的水平和垂直定位都有缺陷。请帮忙 实体.py: 导入pygame 物理实体类: def __init__(自我, ...

回答 1 投票 0

Python中进程如何获取全局变量的当前值?

我的代码是这样的: 导入pygame 从多处理导入进程、队列 #做一点事 #做一点事 默认键盘(): key_pressed = q.get() 如果 key_pressed[pygame.K_a]: #dosome...

回答 2 投票 0

如何在pygame中引用和操作精灵组中的单个实体?

我有几个组来对我的变量进行排序(即-enemy_group = pygame.sprite.Group())。然而,我不知道如何“捕获”单个敌人并移动它(并且,理想情况下,攻击解放军......

回答 2 投票 0

如何在 Pygame 中绘制右对齐的文本?

我正在制作一个数学东西,需要将数字绘制到屏幕右对齐。 我正在使用这个函数来绘制文本: def drawText(文本,字体,text_col,x,y): img = font.render(文本, ...

回答 1 投票 0

Pygame绘制抗锯齿填充多边形

文档说“对于aapolygon,请使用aalines和‘闭合’参数。”,但是pygame.draw.aalines不允许我指定宽度(0 =填充),使其不填充表面。这看起来很...

回答 3 投票 0

无法让 pygame 键盘输入工作[重复]

我刚开始学python做一个项目,需要键盘输入。据我所知,pygame 是做到这一点的最佳方法。不幸的是我一直无法让 KEYDOWN 工作。哦...

回答 2 投票 0

Python 矩形以某种方式被覆盖[重复]

这不是一个关于如何在PyGame中绘制矩形的问题。但不知何故,我的代码覆盖了我正在绘制的内容,因此当我尝试在地图上绘制一个大的绿色矩形标记时,我只会...

回答 1 投票 0

Python 矩形无法正确绘制[重复]

我正在尝试用 pygame 绘制一个矩形。到目前为止,draw.rect 命令一切顺利,但当我尝试重复使用相同的命令时,它停止工作。 这是我是你的命令...

回答 1 投票 0

检测鼠标滚动

如何使用pygame在python中检测鼠标上下滚动? 我已经创建了一种检测它的方法,但它没有给我任何关于我滚动鼠标的方式以及被拒绝的信息......

回答 2 投票 0

点与列表中包含的线的距离

我正在为一个项目制作一个Python动画,在该项目中,我采用了一个星形边界,其中有两个球在该星形内部移动。如果球与其他球碰撞,则会产生新的......

回答 1 投票 0

创建一个函数来检查块是否仍然可以放置在网格中

我是一名初学者程序员,我只想制作一个简单的益智游戏,例如积木游戏等。但是我遇到了一个问题,我需要检查生成的块是否仍然可以放置在网格中......

回答 1 投票 0

如何根据音频文件的位置更改 tkinter 音阶?

在下面发布的代码中,我有一个 tkinter 音阶,我可以通过向右或向左滚动音阶来更改音乐的时间。它工作得很好,但不会改变以适应当前的位置...

回答 1 投票 0

为什么我会收到位置参数错误

因此,在此类中的绘制方法中,由于某种原因,我遇到了位置参数错误。 文件“/Users/wallera/PycharmProjects/MAJORPROJECT2/maintest.py”,第 21 行,位于

回答 1 投票 0

为什么不检查每个图块?

我正在为我正在制作的平台游戏编写生命逻辑游戏代码,但它并没有检查每个图块,只给我一个对象位置 ” 我正在为我正在制作的平台游戏编写生命逻辑游戏代码,但它不会检查每个图块,并且只给我一个对象位置 "<scripts.tilemap.Tilemap object at 0x000001FFBEE1DC40>" 这是我的游戏逻辑: def is_tile_alive(self, pos): tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size)) tile_key = f"{tile_loc[0]};{tile_loc[1]}" if tile_key in self.tilemap and self.tilemap[tile_key]['type'] in ALIVE_TILE_TYPES: print('tile is there') return True return False def logic(self, pos, offset): neighbors = 0 tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size)) for offset in NEIGHBOR_OFFSETS: neighbor_pos = (tile_loc[0] + offset[0], tile_loc[1] + offset[1]) if self.is_tile_alive(neighbor_pos): print('neighbor is alive') neighbors += 1 current_tile_alive = self.is_tile_alive(tile_loc) if current_tile_alive and (neighbors < 2 or neighbors > 3): load_image('tiles/ntile/ntile.png') elif not current_tile_alive and neighbors == 3: self.render(self.tile) elif current_tile_alive and neighbors == 3: pass 这很可能是我的问题 if tile_key in self.tilemap and self.tilemap[tile_key]['type'] in ALIVE_TILE_TYPES: 我尝试了不同的方法来更改它,它改变了位置,但仍然没有给我所有的图块位置 这是我的瓷砖地图脚本 import json import pygame from scripts.utils import load_image, load_images NEIGHBOR_OFFSETS = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] PHYSICS_TILE = {'tiles'} ALIVE_TILE_TYPES = {'tile.png'} class Tilemap: def __init__(self, game, tile_size=16): self.game = game self.tile_size = tile_size self.tilemap = {} self.offgrid_tiles = [] self.timer_event = pygame.USEREVENT + 1 pygame.time.set_timer(self.timer_event, 5000) def extract(self, id_pairs, keep=False): matches = [] for tile in self.offgrid_tiles.copy(): if (tile['type'], tile['variant']) in id_pairs: matches.append(tile.copy()) if not keep: self.offgrid_tiles.remove(tile) for loc in self.tilemap: tile = self.tilemap[loc] if (tile['type'], tile['variant']) in id_pairs: matches.append(tile.copy()) matches[-1]['pos'] = matches[-1]['pos'].copy() matches[-1]['pos'][0] *= self.tile_size matches[-1]['pos'][1] *= self.tile_size if not keep: del self.tilemap[loc] return matches def tiles_around(self, pos): tiles = [] tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size)) for offset in NEIGHBOR_OFFSETS: check_loc = str(tile_loc[0] + offset[0]) + ';' + str(tile_loc[1] + offset[1]) if check_loc in self.tilemap: tiles.append(self.tilemap[check_loc]) return tiles def save(self, path): f = open(path, 'w') json.dump({'tilemap': self.tilemap, 'tile_size': self.tile_size, 'offgrid': self.offgrid_tiles}, f) f.close() def load(self, path): f = open(path, 'r') map_data = json.load(f) f.close() self.tilemap = map_data['tilemap'] self.tile_size = map_data['tile_size'] self.offgrid_tiles = map_data['offgrid'] def physics_rects_around(self, pos): rects = [] for tile in self.tiles_around(pos): if tile['type'] in PHYSICS_TILE: rects.append(pygame.Rect(tile['pos'][0] * self.tile_size -8, tile['pos'][1] * self.tile_size, self.tile_size, self.tile_size)) return rects def render(self, surf, offset=(0, 0)): for tile in self.offgrid_tiles: surf.blit(self.game.assets[tile['type']], (tile['pos'][0] - offset[0], tile['pos'][1] - offset[1])) for x in range(offset[0] // self.tile_size, (offset[0] + surf.get_width()) // self.tile_size + 1): for y in range(offset[1] // self.tile_size, (offset[1] + surf.get_height()) // self.tile_size + 1): loc = str(x) + ';' + str(y) if loc in self.tilemap: tile = self.tilemap[loc] surf.blit(self.game.assets[tile['type']], (tile['pos'][0] * self.tile_size - offset[0], tile['pos'][1] * self.tile_size - offset[1])) def is_tile_alive(self, pos): tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size)) tile_key = f"{tile_loc[0]};{tile_loc[1]}" if tile_key in self.tilemap and self.tilemap[tile_key]['type'] in ALIVE_TILE_TYPES: print('tile is there') return True return False def logic(self, pos, offset): neighbors = 0 tile_loc = (int(pos[0] // self.tile_size), int(pos[1] // self.tile_size)) for offset in NEIGHBOR_OFFSETS: neighbor_pos = (tile_loc[0] + offset[0], tile_loc[1] + offset[1]) if self.is_tile_alive(neighbor_pos): print('neighbor is alive') neighbors += 1 current_tile_alive = self.is_tile_alive(tile_loc) if current_tile_alive and (neighbors < 2 or neighbors > 3): load_image('tiles/ntile/ntile.png') elif not current_tile_alive and neighbors == 3: self.render(self.tile) elif current_tile_alive and neighbors == 3: pass 使用numpy它可以工作 def is_alive(self, pos): return str(np.sum(grid[pos[0]-1:pos[0]+2,pos[1]-1:pos[1]+2])) - grid[pos[0]][pos[ 1]]

回答 1 投票 0

带有 PyGame 的 GUI

我用Python和PyGame制作了一个GUI,我想从现在开始制作更具未来感的GUI。在制作这个特定的 GUI 时,我发现 PyGame 可能不是最佳选择,因为用户事件的数量......

回答 3 投票 0

如何在pygame中使用其他字体?

我下载了一种名为 redline.ttf 的字体,我想在 pygame 中使用它。我只想在屏幕上打印文本。我在文件夹 pygame -> lib 中找到了一种名为“freesansbold.ttf”的基本字体。我已经放了...

回答 2 投票 0

Pygame 精灵碰撞后移动[重复]

我已经做到了,当玩家 1 和玩家 2 的精灵碰撞时,代码停止接受输入并且它们不会移动。如果碰撞后的输入,我在实现运动时遇到了麻烦......

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.