[PyGame] keypress isnt working need help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] keypress isnt working need help (/thread-6803.html) |
keypress isnt working need help - jakegold98 - Dec-08-2017 DELETE THIS THREAD PLEASE I SOLVED THE ISSUE MY OWN EASY MISTAKE For some reason the spaceship only moves when I press L or R arrow keys but they should be moving when I hold them down instead of pressing them a bunch of times. here is the code I have I am following metulburr tutorial on part 3. I added the ship.png so you could see for yourself. import sys import pygame as pg pg.init() screen_width = 800 screen_height = 600 class player: def __init__(self, screen_rect): self.image = pg.image.load('ship.png').convert() self.image.set_colorkey((255,0,255)) self.transformed_image = pg.transform.rotate(self.image, 180) self.rect = self.image.get_rect(center = screen_rect.center) self.speed = 5 def update(self, keys): if keys[pg.K_LEFT]: self.rect.x -= self.speed elif keys[pg.K_RIGHT]: self.rect.x += self.speed def draw(self, surf): surf.blit(self.transformed_image, self.rect) screen = pg.display.set_mode((screen_width, screen_height)) screen_rect = screen.get_rect() player = player(screen_rect) clock = pg.time.Clock() gameOver = False while not gameOver: screen.fill((0,0,0)) keys = pg.key.get_pressed() for event in pg.event.get(): if event.type == pg.QUIT: gameOver = True player.update(keys) player.draw(screen) pg.display.update() clock.tick(60) player.draw(screen) pg.display.update() pg.quit() sys.exit() RE: keypress isnt working need help - micseydel - Dec-08-2017 (Dec-08-2017, 10:13 PM)jakegold98 Wrote: DELETE THIS THREAD PLEASE I SOLVED THE ISSUE MY OWN EASY MISTAKEWe don't delete posts that people figure out. You can post your solution here to potentially help others. |