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

yipeiwu_com6年前Python基础

本文为大家分享了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()

结果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

django实现web接口 python3模拟Post请求方式

django实现web接口 python3模拟Post请求方式

作为抛砖引玉,用python3实现百度云语音解析,首先需要模拟Post请求把音频压缩文件丢给百度解析。 但是遇到一个问题客户端怎麽丢数据都是返回错误,后来在本地用django搭建了一个接...

对web.py设置favicon.ico的方法详解

本文介绍在web.py中设置favicon.ico的方法: 如果没设置favicon,后台日志是这样的: 127.0.0.1:4133 - - [03/Sep/2015 18:49:...

浅析Python中的多进程与多线程的使用

在批评Python的讨论中,常常说起Python多线程是多么的难用。还有人对 global interpreter lock(也被亲切的称为“GIL”)指指点点,说它阻碍了Python的...

python调用动态链接库的基本过程详解

python调用动态链接库的基本过程详解

动态链接库在Windows中为.dll文件,在linux中为.so文件。以linux平台为例说明python调用.so文件的使用方法。 本例中默认读者已经掌握动态链接库的生成方法,如果不...

Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录

Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录

本文介绍了Python+Selenium+PIL+Tesseract自动识别验证码进行一键登录,分享给大家,具体如下: Python 2.7 IDE Pycharm 5.0.3...