[英]PyGame get sprite group
嘿伙计们,试图用pygame建立pacman,有一个小问题。 当pacman正在移动时,如果我按下一个键然后它会改变方向,不幸的是如果上面有一堵墙,那么pacman会停在那个地方并向上指向,直到我改变方向。 我想得到帮助,如何找出块3或4单位的pacman.rect.y属于精灵组级别,它具有所有的墙壁和东西..
您可以使用: http : //www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide
pacman.rect.y = pacman.rect.y - 3
colliding = pygame.sprite.spritecollide(pacman, level)
if colliding:
can_move_upwards = False
else:
can_move_upwards = True
pacman.rect.y = pacman.rect.y + 3
并为您想要测试的每个方向做同样的事情。
这是一个有墙的工作游戏的更长的例子:
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples
import pygame
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
# This class represents the bar at the bottom that the player controls
class Wall(pygame.sprite.Sprite):
# Constructor function
def __init__(self,x,y,width,height):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Make a blue wall, of the size specified in the parameters
self.image = pygame.Surface([width, height])
self.image.fill(blue)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
# This class represents the bar at the bottom that the player controls
class Player(pygame.sprite.Sprite):
# Set speed vector
change_x=0
change_y=0
# Constructor function
def __init__(self,x,y):
# Call the parent's constructor
pygame.sprite.Sprite.__init__(self)
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(white)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.top = y
self.rect.left = x
# Change the speed of the player
def changespeed(self,x,y):
self.change_x+=x
self.change_y+=y
# Find a new position for the player
def update(self,walls):
# Get the old position, in case we need to go back to it
old_x=self.rect.left
new_x=old_x+self.change_x
self.rect.left = new_x
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, walls, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.left=old_x
old_y=self.rect.top
new_y=old_y+self.change_y
self.rect.top = new_y
# Did this update cause us to hit a wall?
collide = pygame.sprite.spritecollide(self, walls, False)
if collide:
# Whoops, hit a wall. Go back to the old position
self.rect.top=old_y
score = 0
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create an 800x600 sized screen
screen = pygame.display.set_mode([800, 600])
# Set the title of the window
pygame.display.set_caption('Test')
# Create a surface we can draw on
background = pygame.Surface(screen.get_size())
# Used for converting color maps and such
background = background.convert()
# Fill the screen with a black background
background.fill(black)
# Create the player paddle object
player = Player( 50,50 )
movingsprites = pygame.sprite.RenderPlain()
movingsprites.add(player)
# Make the walls. (x_pos, y_pos, width, height)
wall_list=pygame.sprite.RenderPlain()
wall=Wall(0,0,10,600)
wall_list.add(wall)
wall=Wall(10,0,790,10)
wall_list.add(wall)
wall=Wall(10,200,100,10)
wall_list.add(wall)
clock = pygame.time.Clock()
done = False
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3,0)
if event.key == pygame.K_RIGHT:
player.changespeed(3,0)
if event.key == pygame.K_UP:
player.changespeed(0,-3)
if event.key == pygame.K_DOWN:
player.changespeed(0,3)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3,0)
if event.key == pygame.K_RIGHT:
player.changespeed(-3,0)
if event.key == pygame.K_UP:
player.changespeed(0,3)
if event.key == pygame.K_DOWN:
player.changespeed(0,-3)
player.update(wall_list)
screen.fill(black)
movingsprites.draw(screen)
wall_list.draw(screen)
pygame.display.flip()
clock.tick(40)
pygame.quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.