游戏开发过程
1.用pygame建立一个游戏界面
import pygame
import sys
pygame.init()
pygame.display.set_caption("我的第一个游戏")
screen = pygame.display.set_mode( (500, 500) )
while True:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
界面如图:
2.基本图形的绘制
import pygame
import sys
pygame.init()
size = width, height = 300, 300
pygame.display.set_caption("图形绘制")
screen = pygame.display.set_mode(size)
WHITE = pygame.color.Color(255, 255, 255)
BLACK = pygame.color.Color(0, 0, 0, a=255)
RED = "#FF0000"
GREEN = "0x00FF00"
BLUE = (0, 0, 255)
screen.fill(WHITE)
while True:
pygame.draw.line(screen, BLUE, (150, 130), (130, 170))
pygame.draw.line(screen, BLUE, (150, 130), (170, 170))
pygame.draw.line(screen, GREEN, (130, 170), (170, 170))
pygame.draw.circle(screen, BLACK, (100, 50), 30)
pygame.draw.circle(screen, BLACK, (200, 50), 30)
pygame.draw.rect(screen, RED, (100, 200, 100, 50), 2)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
绘制图形:
3.实现小车的运动 方式一:
import pygame
import sys
pygame.init()
size = width, height = (400, 600)
pygame.display.set_caption("图像绘制")
screen = pygame.display.set_mode(size)
background = pygame.image.load("AnimatedStreet.png")
player = pygame.image.load("Player.png")
x, y = 178, 504
FPS = 30
clock = pygame.time.Clock()
while True:
screen.blit(background, (0, 0))
screen.blit(player, (x, y))
y -= 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(FPS)
方式二:类的封装:
import pygame
import sys
pygame.init()
size = width, height = (400, 600)
pygame.display.set_caption("图像绘制")
screen = pygame.display.set_mode(size)
class Player():
def __init__(self):
x, y = (width/2, height/2)
self.image = pygame.image.load("Player.png")
self.rect = self.image.get_rect(center = (x, y))
def move(self):
self.rect.move_ip(0, -2)
background = pygame.image.load("AnimatedStreet.png")
FPS = 30
clock = pygame.time.Clock()
player = Player()
while True:
screen.blit(background, (0, 0))
screen.blit(player.image, player.rect)
player.move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(FPS)
图片展现:
|