设为首页收藏本站

Crossin的编程教室

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

求助,找不到我的代码的问题啊

[复制链接]

3

主题

0

好友

32

积分

新手上路

Rank: 1

跳转到指定楼层
楼主
发表于 2013-9-14 03:49:30 |只看该作者 |倒序浏览
本帖最后由 waller516435165 于 2013-9-14 03:49 编辑

我按照http://www.oschina.net/translate/beginning-game-programming-for-teens-with-python这个游戏的编写过程来进行尝试,在进行到第3步后,代码编完进行测验时,发现我的bunny无法移动。是哪里的问题呢?还望各位高手指教
  1. # 1 - import library
  2. import pygame
  3. from pygame.locals import *

  4. # 2 - initialize the game
  5. pygame.init()
  6. width, height = 640, 480
  7. screen = pygame.display.set_mode((width,height))
  8. keys = [False,False,False,False]
  9. playerpos = [100,100]

  10. # 3 - load images
  11. player = pygame.image.load("resources/images/dude.png")
  12. grass = pygame.image.load("resources/images/grass.png")
  13. castle = pygame.image.load("resources/images/castle.png")


  14. # 4 - keep looping through
  15. while 1:
  16.         # 5 - clear the screen before drawing it again
  17.         screen.fill(0)
  18.         # 6 - draw the screen elements
  19.         for x in range(width/grass.get_width()+1):
  20.                 for y in range(height/grass.get_height()+1):
  21.                         screen.blit(grass,(x*100,y*100))
  22.         screen.blit(castle,(0,30))
  23.         screen.blit(castle,(0,135))
  24.         screen.blit(castle,(0,240))
  25.         screen.blit(castle,(0,345))
  26.         screen.blit(player,playerpos)
  27.         # 7 - update the screen
  28.         pygame.display.flip()
  29.         # 8 - loop through the events
  30.         for event in pygame.event.get():
  31.                 #check if the event is the X button
  32.                 if event.type == pygame.QUIT:
  33.                         # if it is quit the game
  34.                         pygame.quit()
  35.                         exit(0)
  36.                 if event.type == pygame.KEYDOWN:
  37.                         if event.key == K_w:
  38.                                 keys[0] = True
  39.                         elif event.key == K_a:
  40.                                 keys[1] = True
  41.                         elif event.key == K_s:
  42.                                 keys[2] = True
  43.                         elif event.key == K_d:
  44.                                 keys[3] = True
  45.                 if event.type == pygame.KEYUP:
  46.                         if event.key == pygame.K_w:
  47.                                 keys[0] = False
  48.                         elif event.key == pygame.K_a:
  49.                                 keys[0] = False
  50.                         elif event.key == pygame.K_s:
  51.                                 keys[0] = False
  52.                         elif event.key == pygame.K_d:
  53.                                 keys[0] = False
  54.                                 
  55. # 9 - Move player
  56. if keys[0]:
  57.         playerpos[1] -= 5
  58. elif keys[2]:
  59.         playerpos[1] += 5
  60. if keys[1]:
  61.         playerpos[0] -= 5
  62. elif keys[3]:
  63.         playerpos[0] += 5
  64.         
复制代码
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2013-9-16 01:18:28 |只看该作者
缩进!还是缩进!
最后一段到了主循环外面去了?
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

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

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

GMT+8, 2024-5-13 13:17 , Processed in 0.024335 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部