Crossin的编程教室
标题:
求助,找不到我的代码的问题啊
[打印本页]
作者:
waller516435165
时间:
2013-9-14 03:49
标题:
求助,找不到我的代码的问题啊
本帖最后由 waller516435165 于 2013-9-14 03:49 编辑
我按照
http://www.oschina.net/translate/beginning-game-programming-for-teens-with-python
这个游戏的编写过程来进行尝试,在进行到第3步后,代码编完进行测验时,发现我的bunny无法移动。是哪里的问题呢?还望各位高手指教
# 1 - import library
import pygame
from pygame.locals import *
# 2 - initialize the game
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width,height))
keys = [False,False,False,False]
playerpos = [100,100]
# 3 - load images
player = pygame.image.load("resources/images/dude.png")
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
# 4 - keep looping through
while 1:
# 5 - clear the screen before drawing it again
screen.fill(0)
# 6 - draw the screen elements
for x in range(width/grass.get_width()+1):
for y in range(height/grass.get_height()+1):
screen.blit(grass,(x*100,y*100))
screen.blit(castle,(0,30))
screen.blit(castle,(0,135))
screen.blit(castle,(0,240))
screen.blit(castle,(0,345))
screen.blit(player,playerpos)
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
#check if the event is the X button
if event.type == pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
if event.type == pygame.KEYDOWN:
if event.key == K_w:
keys[0] = True
elif event.key == K_a:
keys[1] = True
elif event.key == K_s:
keys[2] = True
elif event.key == K_d:
keys[3] = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
keys[0] = False
elif event.key == pygame.K_a:
keys[0] = False
elif event.key == pygame.K_s:
keys[0] = False
elif event.key == pygame.K_d:
keys[0] = False
# 9 - Move player
if keys[0]:
playerpos[1] -= 5
elif keys[2]:
playerpos[1] += 5
if keys[1]:
playerpos[0] -= 5
elif keys[3]:
playerpos[0] += 5
复制代码
作者:
crossin先生
时间:
2013-9-16 01:18
缩进!还是缩进!
最后一段到了主循环外面去了?
欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/)
Powered by Discuz! X2.5