Python中文
首页
教程
问答
标签
搜索
登录
注册
用pygame和python跟踪玩家的摄像机
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有50个外星精灵,我想跟踪玩家,但首先我要一个摄像头跟踪玩家去哪里。在</p> <p>我在一个旧游戏中使用了这个功能,但是在我的旧游戏中,我没有使用类或精灵组,所以效率低下。在</p> <p>所以在这个游戏中,我把玩家设置在屏幕中央,其他的东西都会移动,所以我有一个变量,比如<code>CameraX</code>和{<cd2>},当玩家移动时,两个摄像机变量会上下移动。但是,在我的剧本里外星人没有更新。总之,这是我的脚本:</p> <pre><code>import pygame, random, sys from pygame.locals import * pygame.init() black = ( 0, 0, 0) white = ( 255, 255, 255) red = ( 255, 0, 0) screen_width = 1080 screen_height = 720 screen = pygame.display.set_mode([screen_width,screen_height]) alien_list = pygame.sprite.Group() all_sprites = pygame.sprite.Group() Alien = "graphics\sprites\Alien.png" Player = "graphics\sprites\Player.png" CameraX = 0 CameraY = 0 def main(): class Enemy(pygame.sprite.Sprite): def __init__(self, image): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(image).convert_alpha() self.rect = self.image.get_rect() def Create(): for i in range(50): alien = Enemy(Alien) alien.rect.x = random.randrange(screen_width - 50 - CameraX) alien.rect.y = random.randrange(screen_height - 50 - CameraY) alien_list.add(alien) all_sprites.add(alien) player = Enemy(Player) all_sprites.add(player) done = False clock = pygame.time.Clock() score = 0 moveCameraX = 0 moveCameraY = 0 player.rect.x = 476 player.rect.y = 296 Enemy.Create() while done == False: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == KEYDOWN: if event.key == K_w: moveCameraY = -10 if event.key == K_s: moveCameraY = 10 if event.key == K_a: moveCameraX = -10 if event.key == K_d: moveCameraX = 10 if event.type == KEYUP: if event.key == K_w: moveCameraY = 0 if event.key == K_s: moveCameraY = 0 if event.key == K_a: moveCameraX = 0 if event.key == K_d: moveCameraX = 0 screen.fill(white) enemys_hit = pygame.sprite.spritecollide(player, alien_list, True) for enemy in enemys_hit: score += 1 print(score) all_sprites.draw(screen) clock.tick(40) pygame.display.flip() pygame.quit() </code></pre> <p>然后是整个过程的脚本:</p> ^{pr2}$ <p>谢谢你的时间和帮助</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您不需要使用<code>CameraX</code>和<code>CameraY</code>变量。相反,当你得到键盘输入时,只要让外星人都朝那个方向移动。你也没有任何代码可以移动玩家和外星人。您需要在类中添加一个update函数来更改源矩形的位置。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
文本导入时标题行中的特殊字符
3 回答
文本小部件:在没有输入时更新并在循环后保持空闲
4 回答
文本小部件tkin
2 回答
文本小部件tkinter中的标签更改或文本外观更改是否有撤消功能?
4 回答
文本小部件tkinter复制图像选项
2 回答
文本小部件上的Python Tkinter ttk滚动条未缩放
9 回答
文本小部件上的滚动条可能需要根据制表符ord显示前进行滚动
2 回答
文本小部件不显示lis中的内容
8 回答
文本小部件不显示Unicode字符
7 回答
文本小部件中写入的行间距
4 回答
文本小部件中的文本作为变量
7 回答
文本小部件中的滚动条仅显示在底部
6 回答
文本小部件中的选项卡键空间计数
5 回答
文本小部件作为Lis
4 回答
文本小部件在主框架中扩展列宽
7 回答
文本小部件未使用删除功能清除
7 回答
文本小部件滚动动画(Tkinter、Python)
10 回答
文本居中。格式正确吗?
1 回答
文本差分算法
9 回答
文本已知时音频文件中的单词索引
10 回答