我遇到一个问题,可以在背景上渲染精灵,但是当我使用箭头键时,什么也没发生。
#sidescrolling air-shooter
import pygame
#import pygame locals
from pygame.locals import(
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
#constants for screen width/height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((75,25))
self.surf.fill((255, 255,255))
self.rect = self.surf.get_rect()
#Moves sprite based on keypresses
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0,5)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5,0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
pygame.init()
#create screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate player
player = Player()
#keep the game running!
running = True
#loop time!
while running:
#look at all the events
for event in pygame.event.get():
#did the user hit a key?
if event.type == KEYDOWN:
#Was it escape? uh oh we gotta stop
if event.key == K_ESCAPE:
running = False
elif event.type ==QUIT:
running = False
#gets the keys that were just pressed
pressed_keys = pygame.key.get_pressed()
#updates location based on keys
player.update(pressed_keys)
#fill screen with white
screen.fill((255, 255, 255))
# Create a surface and pass in a tuple containing legth and width
surf = pygame.Surface((50, 50))
# Give the surface a color to separate it from the Background
surf.fill((0, 0, 0))
rect = surf.get_rect()
#This line says "Draw surf onto the screen at the center"
screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#Draw the player on the screen
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
pygame.display.flip()
我真的很难理解我在这里做错了什么。感谢帮助!
您需要在属性中存储的位置上绘制Spriterect
:
screen.blit(player.surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
screen.blit(player.surf, player.rect)
pygame.Surface.get_rect.get_rect()
返回具有Surface对象大小的矩形,但是由于Surface对象没有位置,因此它返回始终以(0,0)开始的矩形。
Surface放置在blit
显示器上的某个位置。
您必须通过关键字参数来设置矩形的位置,例如:
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((75,25))
self.surf.fill((255, 255,255))
self.rect = self.surf.get_rect(center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
# [...]
使用pygame.sprite.Group
。pygame.sprite.Group.update()
和pygame.sprite.Group.draw()
是由提供的方法pygame.sprite.Group
。
前者将委派给所update
包含pygame.sprite.Sprite
s的方法-您必须实现该方法。
在组中的所有Sprite上调用update()方法。
稍后使用包含的的image
和rect
属性pygame.sprite.Sprite
绘制对象-您必须确保pygame.sprite.Sprite
s具有必需的属性
将包含的Sprite绘制到Surface参数。这将Sprite.image属性用于源曲面,将Sprite.rect用于位置。
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.image = pygame.Surface((75,25))
self.image.fill((255, 255,255))
self.rect = self.image.get_rect(center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
player = Player()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
while running:
# [...]
#gets the keys that were just pressed
pressed_keys = pygame.key.get_pressed()
#updates location based on keys
all_sprites.update(pressed_keys)
# Give the surface a color to separate it from the Background
screen.fill((0, 0, 0))
all_sprites.draw(screen)
pygame.display.flip()
完整的例子:
import pygame
#import pygame locals
from pygame.locals import(
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
#constants for screen width/height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.image = pygame.Surface((75,25))
self.image.fill((255, 255,255))
self.rect = self.image.get_rect(center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
#Moves sprite based on keypresses
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0,5)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5,0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
pygame.init()
#create screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Instantiate player
player = Player()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
#keep the game running!
running = True
#loop time!
while running:
#look at all the events
for event in pygame.event.get():
#did the user hit a key?
if event.type == KEYDOWN:
#Was it escape? uh oh we gotta stop
if event.key == K_ESCAPE:
running = False
elif event.type ==QUIT:
running = False
#gets the keys that were just pressed
pressed_keys = pygame.key.get_pressed()
#updates location based on keys
all_sprites.update(pressed_keys)
# Give the surface a color to separate it from the Background
screen.fill((0, 0, 0))
all_sprites.draw(screen)
pygame.display.flip()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句