- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在制作一个基本的 pygame,我在游戏 map 上有几个“障碍”和“门户”。前者是玩家无法触及的点,后者是改变关卡的点。
我试图让它们不可见,所以只在 map 上有一个碰撞检测会注意到的不可见矩形,但现在,当我将它 blit 到 map 时,我有丑陋的黑色 Blob 。
我尝试使用脏矩形,但效果似乎不太好。
我的代码(或者至少是处理障碍和入口的代码):
class Barrier(pygame.sprite.DirtySprite):
'''class that builds up the invisible barriers in the game'''
#constructor function
def __init__(self, posX, posY, width, height): #create a self variable to refer to the object
#call up the parent's constructor
pygame.sprite.DirtySprite.__init__(self)
self.dirty = 1
self.visible = 0
#Make the barrier.
self.image = pygame.Surface([width, height])
#debug code that makes sure that the barrier is in the right place
#self.image.fill(white)
# place the top left corner of the barrier at the given location
self.rect = self.image.get_rect()
self.rect.y = posY
self.rect.x = posX
barriers = pygame.sprite.Group() #global barrier list used in all maps
portals = pygame.sprite.Group() #global portal list used in all maps
class Portal(pygame.sprite.DirtySprite):
'''class that builds up the portals in the game'''
#constructor function
def __init__(self, posX, posY, width, height): #create a self variable to refer to the object
#call up the parent's constructor
pygame.sprite.DirtySprite.__init__(self)
self.dirty = 1
self.visible = 0
#Make the barrier.
self.image = pygame.Surface([width, height])
#debug code that makes sure that the barrier is in the right place
self.image.fill(black)
# place the top left corner of the barrier at the given location
self.rect = self.image.get_rect()
self.rect.y = posY
self.rect.x = posX
def LoadInside():
barriers.empty()
portals.empty()
#Load up the level image
whichLevel = 1
background_image = pygame.image.load("House.png").convert()
#a list of all the barriers in the room
room_barrier_list = pygame.sprite.Group()
#make a barrier out of all of the objects in the room
barrierTopWall = Barrier(0,125,661,7)
barrierLeftWall= Barrier(5, 130,5, 300)
barrierBottomWallLeft= Barrier(10,414,292,7)
barrierBottomWallRight= Barrier(364,412,298,7)
barrierRightWall= Barrier(649,126,5, 294)
bed = Barrier(19,199,62,93)
smallTableChairs = Barrier(273,220,97,39)
pot = Barrier(300,288,34,31)
table = Barrier(493,242,151,42)
chair1 = Barrier(459,255,28,35)
chair2 = Barrier(490,296,31,28)
chair3 = Barrier(553,293,31,28)
chair4 = Barrier(621,292,31,28)
#make a portal to get out
door = Portal(300,413, 64,10)
#add the barriers to the lists
room_barrier_list.add(barrierTopWall, barrierLeftWall, barrierBottomWallLeft, barrierRightWall)
barriers.add(room_barrier_list)
all_sprites_list.add(barriers)
room_barrier_list.add(smallTableChairs,pot,table,chair1,chair2,chair3,chair4, barrierBottomWallRight, bed)
barriers.add(room_barrier_list)
all_sprites_list.add(barriers)
#add the portal to the list
all_sprites_list.add(door)
mainScreen.blit(background_image, [0,0])
#
# The above code handles the room barriers and portals
#
while done==False:
for event in pygame.event.get(): #user did something
if event.type == pygame.QUIT: #if the user hit the close button
done=True
# Move the player if an arrow key is pressed
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
faceWhatDirection = 'left' # set the faceWhatDirection variable
player.updateLeft() # call up the animation function
player.move(-10, 0)
if key[pygame.K_RIGHT]:
faceWhatDirection = 'right' # set the faceWhatDirection variable
player.updateRight() # call up the animation function
player.move(10, 0)
if key[pygame.K_UP]:
faceWhatDirection = 'up' # set the faceWhatDirection variable
player.updateUp() # call up the animation function
player.move(0, -10)
if key[pygame.K_DOWN]:
faceWhatDirection = 'down' # set the faceWhatDirection variable
player.updateDown() # call up the animation function
player.move(0, 10)
#if the user presses the space bar, the attack button
if key[pygame.K_SPACE]:
bullet = Bullet()
bullet_list.append(bullet) #adds bullet to the bullet list
all_sprites_list.add(bullet) #adds the bullet to the sprite list to be drawn
#puts the bullet in the same location as player (this needs to be changed to what direction player faces)
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y + 15 # the + 15 places it at a location that is not on the player's face!
bullet.bulletDirection = faceWhatDirection
#checks to see what direction to move the bullet in
for bullet in bullet_list:
bullet.move_bullet()
#see if the bullet hit anything
barrier_hit_list = pygame.sprite.spritecollide(bullet, barriers, False)
for barrier in barrier_hit_list:
#remove the bullet if it hit something
bullet_list.remove(bullet)
all_sprites_list.remove(bullet)
mainScreen.fill(black)#makes the background white, and thus, the white part of the images will be invisible
if whichLevel == 1:
LoadInside()
else:
LoadOutside()
#draw the sprites
all_sprites_list.draw(mainScreen)
#limit the game to 20 fps
clock.tick(20)
#update the screen on the regular
pygame.display.flip()
pygame.quit()
最佳答案
很简单,
Sprite 是用 Group.update() 方法 blit 的。
all_sprites_list.draw(mainScreen)
不要将障碍添加到 all_sprites_list。然后他们永远不会在屏幕上被点亮。
但是 barrier_hit_list
仍然可以用于碰撞。
关于python - 无法使 Sprite 在pygame中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635896/
Surface.blit在 1.8 中有一个新参数:混合。定义了以下值: BLEND_ADD BLEND_SUB BLEND_MULT BLEND_MIN BLEND_MAX BLEND_RGBA_A
import sys import pygame import pygame.locals as pgl class Test: def __init__(self): pyg
我对 PyGame 比较陌生。我正在尝试制作一个简单的程序来显示表示鼠标在屏幕上的位置的字符串。 import pygame, sys from pygame.locals import * pyga
我有总是在后台运行的音乐和一些在触发时会播放声音的事件。音乐效果很好。 pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.
我有这些代码 FONT = pygame.font.Font("font/calibri.ttf", 50) FONT.size = 25 但是编译器说 AttributeError: 'pygame
有我正在导入的图像: look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha() 我试图减少它的大小是这样的: pygame.
我正在为我的 pygame 制作一个帮助屏幕,每当我运行它时,我都会收到此错误消息: > self.surface.blit(self.helpscreen) TypeError: argument
在 pyGame 中应用程序,我想渲染 SVG 中描述的无分辨率 GUI 小部件。 我怎样才能做到这一点? (我喜欢 OCEMP GUI 工具包,但它的渲染似乎依赖于位图) 最佳答案 这是一个完整的例
有没有办法将多首歌曲加载到 Pygame 中?我不是在谈论这样的音效; crash_sound = pygame.mixer.Sound("crash.ogg") #and pygame.mixer.
我还有一个问题。当我尝试运行我的代码时,pygame 启动然后立即停止。 这是我的代码: import pygame import os import time import random pygam
我正在使用 pymunk 和 pygame 开发一个项目。我正在使用 PivotJoint 约束将我的 body 连接在一起。如果可能的话,我想让关节不可见 - 有什么办法可以做到这一点吗?现在关节在
我使用 fedora 20、Python 2.7 和 virtualenv 1.10.1。我想在 virtualenv 中安装 pygame,我得到了 You are installing a pot
尝试将文本添加到矩形中并使用箭头键在屏幕上移动矩形。我想让文字不会超出边缘。到目前为止,我已经在没有将其放入 Rect 中的情况下工作了,但我想让 Rect 函数工作。现在文本只是反弹回来,我不知道要
我是第一次玩 pygame(总的来说我是 python 的新手),想知道是否有人可以帮助我... 我正在制作一款小型射击游戏,希望能够为坏人创建一个类。我的想法是类应该继承自 pygame.Surfa
我在 Windows 10 机器上运行了 python 3.9.1。我通过 pip 在我的机器上安装了 pygame 2.0.1 (python -m pip install https://gith
错误: File "/home/alien/cncell/core/animator.py", line 413, in create_animation_from_data pygame
这是代码。 5000 个弹跳旋转的红色方块。 (16x16 png) 在 pygame 版本上,我获得 30 fps,但使用 pyglet 获得 10 fps。对于这种事情,OpenGl 不应该更快吗
我以为 pygame.font.Font 是用来加载 .ttf 字体的,如果没有 .ttf 文件在同一个目录下就无法加载字体,但我看过一个视频,有人在没有 .ttf 文件的情况下加载字体。 ttf 字
我正在尝试使用 Travis CI 设置一个项目。项目也使用 pygame。我曾多次尝试设置它 - 但它似乎失败了。 我得到的最接近的是以下内容: .travis.yml : language: py
这个问题已经有答案了: Python error "ImportError: No module named" (38 个回答) 已关闭 3 年前。 我正在使用 Mac 并输入 pip install
我是一名优秀的程序员,十分优秀!