设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 47161|回复: 1
打印 上一主题 下一主题

【Pygame 第3课】 游戏中的事件

[复制链接]

3

主题

0

好友

243

积分

中级会员

Rank: 3Rank: 3

楼主
发表于 2013-8-22 13:45:04 |显示全部楼层

回帖奖励 +1

  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from sys import exit
  4. from pygame import *

  5. pygame.init()
  6. screen = pygame.display.set_mode((630, 300), 0, 32)
  7. pygame.display.set_caption("hello world")
  8. bg1 = image.load('bg.png').convert()
  9. bg2 = image.load('bg2.jpg').convert()
  10. target = True
  11. bg = bg1
  12. while 1:
  13.     for event in pygame.event.get():
  14.         if event.type == QUIT:
  15.             pygame.quit()
  16.             exit()
  17.         if event.type == MOUSEBUTTONDOWN:
  18.             if target:
  19.                 target = not target
  20.                 bg = bg2
  21.             else:
  22.                 target = not target
  23.                 bg = bg1
  24.     screen.blit(bg, (0,0))
  25.     pygame.display.update()
复制代码
回复

使用道具 举报

3

主题

0

好友

243

积分

中级会员

Rank: 3Rank: 3

沙发
发表于 2013-8-22 13:53:28 |显示全部楼层
多张图片随机切换:
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from sys import exit
  4. from pygame import *
  5. import random

  6. pygame.init()
  7. screen = pygame.display.set_mode((630, 300), 0, 32)
  8. pygame.display.set_caption("hello world")
  9. bg1 = image.load('bg.png').convert()
  10. bg2 = image.load('bg2.jpg').convert()
  11. bg3 = image.load('bg3.jpg').convert()
  12. bgcolor = [bg1, bg2, bg3]
  13. target = True
  14. bg = bg1
  15. while 1:
  16.     for event in pygame.event.get():
  17.         if event.type == QUIT:
  18.             pygame.quit()
  19.             exit()
  20.         if event.type == MOUSEBUTTONDOWN:
  21.             bg = random.choice(bgcolor)
  22.     screen.blit(bg, (0,0))
  23.     pygame.display.update()
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-3 01:16 , Processed in 0.016378 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部