我想我在这里得到它:
import pygame
running = True
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
FPS = 30
pygame.init()
all_sprites = pygame.sprite.Group() #groupes all sprites
all_sprites.update()
tab.fill(BLACK) #any color
all_sprites.draw(tab)
clock = pygame.time.Clock()
WIDTH = 500 #YOUR WIDTH
HEIGHT = 500 #YOUR HEIGHT
tab = pygame.display.set_mode((WIDTH, HEIGHT)) #window width and height
class Player(pygame.sprite.Sprite):
#your Character
def __init__():
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("Ship.png") #your image or shape/sprites look
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, (HEIGHT / 2)) #sprites Position
def update(self):
key = pygame.key.get_pressed();
#stop at edge commands (might need to ajust)
if(self.rect.x >= (WIDTH-200)):
self.rect.x -= 10;
elif(self.rect.x <= 0):
self.rect.x += 10;
elif(self.rect.y <= 0):
self.rect.y += 10;
elif(self.rect.y >= (HEIGHT-200)):
self.rect.y -= 10
else:
#sprites move commands
if key[pygame.K_RIGHT]:self.rect.x += 10;
if key[pygame.K_LEFT]:self.rect.x -= 10;
if key[pygame.K_UP]:self.rect.y -= 10;
if key[pygame.K_DOWN]:self.rect.y += 10;
player = Player() #makes the sprite that the player controlles
all_sprites.add(player)
while (running):
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all_sprites.update() #updates screen
tab.fill(BLACK)
all_sprites.draw(tab) #adds all sprites to the screen
pygame.display.flip()
我从别人那里得到了一些,但我不记得了,但我认为这会有所帮助 ------------------------------ -------------------------------------------------- ------ 注意:您可能需要调整一些变量和空格等等。(ps:我不擅长语法)也很抱歉,它很长,我认为没有这些东西就行不通。