Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

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)
   
Quote
#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.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)