未加星标

My sprite will not lead pygame right

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

earlier my sprite wouldn't move at all so i posted the code and i got it fixed for the most part but now my up/down arrows work fine but my right key doesn't work. (Also, when you press two keys, and then let go of one, the walk animation doesn't work, but I'm not desperate right now to get that fixed.) Also i would prefer not to use user made classes. thanks in advance. Here is the code:

from pygame.locals import * import pygame._view pygame.init() clock = pygame.time.Clock() height = 500 width = 500 screen = pygame.display.set_mode((width, height), 0, 32) pygame.display.set_caption('placeholder text') photo = 'grassbackground.png' background = pygame.image.load(photo).convert() rectexist = False photo1 = 1 user = pygame.sprite.Sprite() change = False up = False down = False left = False right = False speed = 5 while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_UP: up = True change = True if event.key == K_DOWN: down = True change = True if event.key == K_LEFT: left = True change = True if event.type == K_RIGHT: right = True change = True if event.type == KEYUP: if event.key == K_UP: up = False change = False if event.key == K_DOWN: down = False change = False if event.key == K_LEFT: left = False change = False if event.key == K_RIGHT: right = False change = False if down and user.rect.bottom < height: user.rect.top += speed if up and user.rect.top > 0: user.rect.top -= speed if left and user.rect.left > 0: user.rect.left -= speed if right and user.rect.right < width: user.rect.right += speed if change == True: pygame.time.wait(110) photo1 += 1 if change == False: photo1 = 1 if photo1 == 1: user.image = pygame.image.load("1.png").convert() if rectexist == False: user.rect = user.image.get_rect() rectexist = True screen.blit(user.image, user.rect) if photo1 == 2: user.image = pygame.image.load("2.png").convert() screen.blit(user.image, user.rect) if photo1 == 3: user.image = pygame.image.load("3.png").convert() screen.blit(user.image, user.rect) if photo1 >= 4: photo1 = 1 thesprites = pygame.sprite.RenderPlain((user)) thesprites.update() screen.blit(background, (0, 0)) thesprites.draw(screen) pygame.display.update() clock.tick(60)

In your code it says:

if event.type == K_RIGHT:

it should be:

if event.key == K_RIGHT:

To keep animating you'd need to change the code a little more, add a:

key_pressed = []

in the beginning. Then for each key-press block do:

key_pressed.append(event.key)

and key-release do:

key_pressed = [k for k in key_pressed if k != event.key]

instead of the change=True and change=False respectively. Then after the two segments of checking what was pressed and released have add these lines:

if len(key_pressed) > 0: change = True else: change = False

That should fix most of your issues...

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

代码区博客精选文章
分页:12
转载请注明
本文标题:My sprite will not lead pygame right
本站链接:https://www.codesec.net/view/610868.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(61)