- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在为学校计算机课做这个项目,但无法让玩家 Sprite 出现,有人可以帮忙吗?当我运行主游戏循环时,除了玩家 Sprite 外,一切都正确显示。它应该由于箭头输入而在屏幕上移动并受重力影响。当我删除图像并仅使用一个对象类和一个矩形时,该代码也有效。
import os
import random
import pygame
#start pygame
pygame.init()
#set up display
pygame.display.set_caption("Jump to escape!")
width = 720
height = 540
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
img = pygame.image.load('img.jpg')
#PLayer class
class Player(pygame.sprite.Sprite):
#My sprite is going to be a player
def __init__(self):
super().__init__()
self.image = pygame.image.load("player.png").convert_alpha()
self.image.set_colorkey([255,255,255])
self.rect = self.image.get_rect()
#We have added a class variable called self.currentWalls.
#This is blank initially but as soon as the game loop is called it will store the currently displayed walls.
#It's effectively a copy of the walls variable within the game loop itself.
self.currentWalls =[]
def move(self,dx,dy, walls):
#Every time the 'move' function is called, we update self.currentWalls with the walls passed to the function.
self.currentWalls = walls
if dx!=0:
self.move_single_axis(dx,0)
if dy!=0:
self.move_single_axis(0,dy)
def move_single_axis(self, dx, dy):
self.rect.x += dx
self.rect.y += dy
#This is where we actually use self.currentWalls.
#It is used for collision detection.
#The code remains pretty much the same, however it is now referencing self.currentWalls instead of just walls.
for wall in self.currentWalls:
if self.rect.colliderect(wall.rect):
if dx > 0:
self.rect.right = wall.rect.left
if dx < 0:
self.rect.left = wall.rect.right
if dy > 0:
self.rect.bottom = wall.rect.top
if dy < 0:
self.rect.top = wall.rect.bottom
#a new class for walls / blocks to jump onto
class Wall(object):
#Whenever this class is instantiated, an item with a property 'item.rect' is created.
#We append copies of this class to the walls array later in the program.
def __init__(self, wx, wy):
self.rect = pygame.Rect(wx,wy,30,30)
def reset_wall(self):
self.active = False
#Start of the menu implementation.
#Function that, when called will quit pygame.
def quitgame():
pygame.quit()
quit()
#Function that creates a button to the required parameters.
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(screen, ic,(x,y,w,h))
smallText = pygame.font.SysFont("arialBold",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
screen.blit(textSurf, textRect)
#Function that creates a text object to the specified parameters.
def text_objects(text, font):
textSurface = font.render(text, True, (0,0,0))
return textSurface, textSurface.get_rect()
#Function that, when called, makes a menu appear.
#It makes use of the above few functions.
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
screen.fill((255,255,255))
screen.blit(img,(0,0))
largeText = pygame.font.SysFont("Segoe UI Black",115)
TextSurf, TextRect = text_objects("Pixel Dash", largeText)
TextRect.center = ((width/2),(height/2))
screen.blit(TextSurf, TextRect)
button("GO!",150,450,100,50,(0,150,0),(0,255,0),game_loop)
button("Quit",450,450,100,50,(150,0,0),(255,0,0),False)
pygame.display.update()
clock.tick(60)
#Function that, when called, will run the main game.
#It uses the other functions that the menu system doesn't.
def game_loop():
#Create an empty list called walls.
#At any point during the game this list will store the walls that are currently being displayed.
walls = []
player = Player() #create a player object using the class above
colour = (0,128,255)
wall_colour = (255,255,255)
#In the level, W means wall & E means exit
level = [
"WWWWWWWWWWWWWWWWWWWWWWWW",
"W W",
"W WWW W",
"W W",
"W E W",
"W W WWWWWW W",
"W WW W",
"W WW W",
"W WWWWW W",
"W W",
"W WW WWWW",
"W W",
"W W",
"W WWWWWW W",
"W WWW W",
"W W",
"W W",
"WWWWWWWWWWWWWWWWWWWWWWWW",
]
levels = [[
"WWWWWWWWWWWWWWWWWWWWWWWW",
"W W",
"W W",
"W W",
"W E W",
"W WWWWWW W",
"W W",
"W W",
"W WWWWW W",
"W W",
"W WWWW",
"W W",
"W W",
"W WWWWWW W",
"W W",
"W W",
"W W",
"WWWWWWWWWWWWWWWWWWWWWWWW",
],[
"WWWWWWWWWWWWWWWWWWWWWWWW",
"W W",
"W E W",
"W WWWWW W",
"W WWW W",
"W W",
"W W",
"W WWWWW W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W WWWWWW W",
"W W",
"W W",
"W W",
"WWWWWWWWWWWWWWWWWWWWWWWW",
],[
"WWWWWWWWWWWWWWWWWWWWWWWW",
"W W",
"W W W",
"W WWW W WW",
"W W E WWW",
"W WWWW W",
"W W",
"W WWWWW W",
"W W",
"W W",
"W W W",
"W W",
"W W",
"W WWW W",
"W W",
"W W W",
"W W",
"WWWWWWWWWWWWWWWWWWWWWWWW",
]]
x = y = 0
for row in level:
for col in row:
if col == "W":
#For each wall, create a new instance of the 'Wall' class and append it to the list 'walls'
walls.append(Wall(x, y))
if col == "E":
end_rect = pygame.Rect(x,y,30,30)
x += 30
y += 30
x=0
#start the game play!
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if (event.type == pygame.KEYDOWN) and (event.key == pygame.K_SPACE):
if colour == (0,128,255):
colour = (255,100,0)
else:
colour = (0,128,255)
#allow the player to move
user_input = pygame.key.get_pressed()
#Whenever we call the player.move function, we have added another parameter.
#We pass the walls list in to the function so that it keeps the variable player.currentWalls up to date.
if user_input[pygame.K_UP]:
player.move(0,-5, walls)
elif player.rect.y < (height -60):
player.move(0,5, walls)
if user_input[pygame.K_DOWN]:
player.move(0,5, walls)
if user_input[pygame.K_LEFT]:
player.move(-5,0, walls)
if player.rect.x < 0:
player.rect.x= width -1
if user_input[pygame.K_RIGHT]:
player.move(5,0, walls)
if player.rect.x > width:
player.rect.x= -59
if player.rect.colliderect(end_rect):
#Clear the walls array.
#Gets rid of all walls that are currently being drawn.
del walls[:]
level = random.choice(levels)
wall_colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
x = y = 0
for row in level:
for col in row:
if col == "W":
#For each wall, create a new instance of the 'Wall' class and append it to the list 'walls'
walls.append(Wall(x, y))
if col == "E":
end_rect = pygame.Rect(x,y,30,30)
x += 30
y += 30
x=0
#draw the screen
screen.fill((0,0,0))
for wall in walls:
pygame.draw.rect(screen,wall_colour,wall.rect)
pygame.draw.rect(screen,(255,0,0),end_rect)
pygame.draw.rect(screen,colour,player.rect)
pygame.display.flip()
game_intro()
game_loop()
pygame.quit()
最佳答案
在 Player
的 __init__
方法中,您创建了一个矩形但未设置其坐标,因此它位于默认坐标 (0, 0)。然后你的碰撞检测代码似乎将它推到水平之上并且你无法再次输入它。
将矩形的坐标设置为关卡内的一个点,例如:
self.rect = self.image.get_rect(topleft=(400, 300))
# Or
self.rect.x = 400
self.rect.y = 300
# Or
self.rect.topleft = (400, 300)
您也可以将位置传递给 __init__
方法,然后将其分配给 rect,或者在级别发生变化时设置它。
关于python - Pygame玩家 Sprite 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50049642/
任何人都可以评论是否对图像使用 Sprite 的决定?我看到以下好处/权衡(其中一些可以减轻): 单个图像上的 Sprite 优点: 需要管理的图像更少 更容易实现主题图像 图像交换 (JS/CSS)
所以,我一直在 Unity 工作,但我决定是时候换成我更了解的东西了:JavaScript。我一直在考虑切换到 Phaser.js,但我有一些关于我什至在 Unity 中遇到的问题的问题,而且我在互联
所以我有一个木偶要在 Sprite Kit 中显示。木偶是由一堆不同的 body 部位组成的,当然每个部位都是一个.png。 所以我的过程是:我有一个 Marionette 对象(SKNode 子类)
我有一个 Sprite ,我将其初始化为 SKSPriteNode,它不断地从另一个 Sprite 上弹起,也以相同的方式初始化。 我无法弄清楚两者之间的冲突,并且到目前为止堆栈上没有任何帮助。 我将
标题说明了一切。我想知道 Sprite.getcontentsize、Sprite.gettexture、Sprite.getscale 之间有什么区别。以及它们是如何使用的。在这个问题之后我找不到任
我有兴趣尝试创建一些游戏,即在新的 sprite 工具包中。但是,对于我心目中的游戏,我宁愿使用方向键而不是操纵杆。因为我将从 Cocos 搬过来,所以我的旧程序不再有效(所以那个 dpad 也不会)
我正在 LibGdx 中开发 2D 射击游戏。 我不得不提一下,我是 LibGdx 的新手,我正在努力理解它是如何工作的。我有几年的Java和Android编程经验,所以我了解游戏概念。 我感兴趣的是
我正在使用 Compass 生成 CSS Sprite 。 我找到了一种方法来定义一次 Sprite 并在不同的 .scss 文件中使用它,但我不确定这是正确的解决方案。 到目前为止,我能找到的最好方
我在游戏中遇到背景音乐问题。当我从主菜单场景切换到游戏场景时,它停止,但是当游戏场景切换到gameOver场景时,它不停止。当我选择重播时,音乐也会重叠(从gameOver场景切换回游戏场景)。 要播
我是一名使用 libgdx 引擎的新程序员,想知道 Sprite 批处理的行为。特别是如何在程序生命周期中将 Sprite 添加到批处理中以进行绘制。到目前为止, Sprite 的所有示例都使用了一些
这可能是个愚蠢的问题,但如果有人能帮助我,我将不胜感激。 我有一个由 3 个垂直堆叠的不同图像组成的 Sprite ,但我试图让中间的图像(高度为 1px 和宽度为 194)重复,只是那条 1px 的
我正在尝试为我正在构建的菜单加载 spritesheet,但它不是一次显示一个图像,而是在元素的不同位置显示整个 spritesheet。 这是我使用两张图片的 CSS 代码: #mymenu ul.
我有两个 Sprite 组,ship_list 有 20 个飞船 Sprite ,all_sprites 有这 20 个 Sprite ,加上玩家 Sprite 。在主循环中,当检测到玩家与 ship
我制作了这个我可以抓取并四处移动的 Sprite 。我的问题是我希望能够“抛出” Sprite 。意思是,当我释放 Sprite 时,我希望它继续沿着我移动它的方向前进。就像扔球一样。 我该怎么办?
我目前正在开发 HTML/CSS 模板,我将实现以下社交媒体图标: http://www.premiumpixels.com/freebies/41-social-media-icons-png/ 它
在我的游戏中,我希望能够收集硬币。我有一个该硬币 Sprite 的数组列表,这样我就可以单独绘制多个硬币。这些硬币也随着背景移动(模拟汽车驾驶),我想要它,所以当硬币撞到汽车时,它会消失并被收集。感谢
我是 cocos2D 的新手,谁能提出一个简单的解决方案? 我有一个风车,风车上有 8 个条,每个条以 45 度隔开,其中风车的一半在屏幕上,一半在屏幕外。我想旋转风车,让它永远旋转。我还想在风车杆的
我使用的是主播中心分支,刚刚发现我的游戏出现的问题大部分都是这个原因。有没有一种方法可以使用 Sprite 的左下角而不是使用 (0, 0) 作为其中心来设置 Sprite ? 谢谢! 最佳答案 发现
我是 magic-importing我的 Sprite : // Creating a concatenated sprite image out of all sprites in the "/im
这个问题在这里已经有了答案: How do I detect collision in pygame? (5 个答案) 关闭去年。 想要创建一个包含 10 张图像的组。稍后屏幕上的图像不应重叠。我尝
我是一名优秀的程序员,十分优秀!