pygame游戏之旅3:载入小车图片、更新窗口

载入car图片(我自己画的),需要用到pygame.image模块,定义carImg用于接收载入的图片

carImg = pygame.image.load('car.png')
定义一个car函数绑定car的位置

def car(x, y):
    gameDisplay.blit(carImg,(x,y))
为窗口填充白色并调用car函数,更新窗口

gameDisplay.fill(white)
car(x,y)
pygame.display.update()
完整代码是:

import pygame

pygame.init()

white = (255,255,255)

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode( (display_width,display_height) )
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()

carImg = pygame.image.load('car.png')

def car(x, y):
    gameDisplay.blit(carImg,(x,y))

    
x = display_width * 0.45
y = display_height * 0.8


crashed = False

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        print(event)
    gameDisplay.fill(white)
    car(x,y)
    pygame.display.update()
    clock.tick(60)

pygame.quit()
quit()


结果图:

pygame游戏之旅3:载入小车图片、更新窗口_第1张图片









你可能感兴趣的:(pygame)