网络编程
位置:首页>> 网络编程>> Python编程>> pygame游戏之旅 载入小车图片、更新窗口

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

作者:观月执白  发布时间:2022-08-06 18:12:39 

标签:pygame,窗口,游戏

本文为大家分享了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游戏之旅 载入小车图片、更新窗口

来源:https://blog.csdn.net/pianzang5201/article/details/78294415

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com