Python Forum
[PyGame] keypress isnt working need help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] keypress isnt working need help
#1
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()

Attached Files

Thumbnail(s)
   
Reply
#2
(Dec-08-2017, 10:13 PM)jakegold98 Wrote: DELETE THIS THREAD PLEASE I SOLVED THE ISSUE MY OWN EASY MISTAKE
We don't delete posts that people figure out. You can post your solution here to potentially help others.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] can you explain me about addition of keypress to move the turtle..? berto_simarmata 1 2,051 Jun-13-2020, 03:33 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020