设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【Pygame 第4课】 获取鼠标位置

[复制链接]

0

主题

1

好友

273

积分

中级会员

Rank: 3Rank: 3

楼主
发表于 2016-1-30 13:03:00 |显示全部楼层
get it, 原来游戏窗口的最左上角坐标是(0,0),坐标值越小越接近左上角,跟正常的坐标系有点不一样。这样x-= plane.get_width() / 2,y-= plane.get_height() / 2就说得通了。
回复

使用道具 举报

0

主题

1

好友

273

积分

中级会员

Rank: 3Rank: 3

沙发
发表于 2016-1-30 13:45:46 |显示全部楼层
加了一些限制让飞机保持在窗口内
  1. import pygame
  2. from sys import exit
  3. pygame.init()
  4. screen=pygame.display.set_mode((960,640))
  5. pygame.display.set_caption('Hello, World!')
  6. background=pygame.image.load('.\data file\pypic2.jpg').convert()
  7. plane=pygame.image.load('.\data file\plane.gif').convert()
  8. width=plane.get_width()/2
  9. height=plane.get_height()/2
  10. while True:
  11.     for event in pygame.event.get():
  12.         if event.type==pygame.QUIT:
  13.             pygame.quit()
  14.             exit()
  15.     screen.blit(background,(0,0))
  16.     x,y=pygame.mouse.get_pos()
  17.     if y>=640-height:
  18.         if x<width:
  19.             screen.blit(plane,(0,640-2*height))
  20.         elif x>960-width:
  21.             screen.blit(plane,(960-2*width,640-2*height))
  22.         else:
  23.             screen.blit(plane,(x-width,640-2*height))
  24.     elif y<height:
  25.         if x<width:
  26.             screen.blit(plane,(0,0))
  27.         elif x>960-width:
  28.             screen.blit(plane,(960-2*width,0))
  29.         else:
  30.             screen.blit(plane,(x-width,0))
  31.     else:
  32.         if x<width:
  33.             screen.blit(plane,(0,y-height))
  34.         elif x>960-width:
  35.             screen.blit(plane,(960-2*width,y-height))
  36.         else:
  37.             screen.blit(plane,(x-width,y-height))  
  38.     pygame.display.update()
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-18 18:37 , Processed in 0.025388 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部