This question already has an answer here:
How to get if a key is pressed pygame [duplicate]
(1 answer)
Closed 2 years ago.
I'm making a submarine game in pygame and I tried to make my submarine shoot bullets. I've tried the code, that I used in my other pygame project, however it doesn't work in this one. Compiler doesn't give any error, but when I press space, it won't shoot. I tried to find a mistake, but I couldn't. I also tried to browse Stack Overflow, but didn't find the answer I was looking for.
Here's the code:
import pygame
pygame.init()
run = True
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption('Podmornca')
desno = pygame.image.load('podmornicaD.png')
levo = pygame.image.load('podmornica.png')
ozadje = pygame.image.load('ozadje.png')
torpedoD = pygame.image.load('torpedo.png')
torpedoL = pygame.image.load('torpedoL.png')
class podmornica():
def __init__(self, x, y, v):
self.x = x
self.y = y
self.v = v
self.ziv = 100
self.levo = False
self.desno = True
def naris(self):
if self.levo:
screen.blit(levo, (self.x, self.y))
elif self.desno:
screen.blit(desno, (self.x, self.y))
class torpedo():
def __init__(self, x, y, smer):
self.x = x
self.y = y
self.smer = smer
self.v = 5 * smer
def naris(self, screen):
if self.smer < 0:
screen.blit(torpedoD, (self.x, self.y))
else:
screen.blit(torpedoL, (self.x, self.y))
igralec = podmornica(150, 300, 10)
#the list of bullets:
metki = []
def grafika():
screen.blit(ozadje, (0,0))
igralec.naris()
#Here is code for displaying the bullets
for metek in metki:
metek.naris(screen)
pygame.display.flip()
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#This piece of code is for moving of bullets:
for metek in metki:
if metek.x < 600 and metek.x > 0:
metek.x += metek.v
else:
metki.pop(metki.index(metek))
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP and igralec.y > 10:
igralec.y -= 3
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN and igralec.y < 350:
igralec.y += 3
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
igralec.levo = True
igralec.desno = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
igralec.levo = False
igralec.desno = True
#the trigger for bullet:
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
if igralec.levo:
smer = -1
else:
smer = 1
if len(metki) < 5:
metki.append(torpedo(igralec.x, igralec.y, smer))
grafika()
pygame.quit()
The events have to be handled in the event loop. If you want to achieve a smooth movement, then you have get the key states by pygame.key.get_pressed().
Furthermore the bullets move much to fast. Control the frames by pygame.time.Clock() respectively .tick(). e.g:
FPS = 60
clock = pygame.time.Clock()
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
#the trigger for bullet:
if event.key == pygame.K_SPACE:
if igralec.levo:
smer = -1
else:
smer = 1
if len(metki) < 5:
metki.append(torpedo(igralec.x, igralec.y, smer))
# This piece of code is for moving of bullets:
for metek in metki[:]:
if metek.x < 600 and metek.x > 0:
metek.x += metek.v
else:
metki.remove(metek)
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and igralec.y > 10:
igralec.y -= 3
if keys[pygame.K_DOWN] and igralec.y < 350:
igralec.y += 3
if keys[pygame.K_LEFT]:
igralec.levo = True
igralec.desno = False
if keys[pygame.K_RIGHT]:
igralec.levo = False
igralec.desno = True
grafika()
Related
This question already has answers here:
How can i shoot a bullet with space bar?
(1 answer)
How do I stop more than 1 bullet firing at once?
(1 answer)
Closed 7 months ago.
So basically I'm making an escape room game. In the game, you have to interact with a desk and a key would spawn on the map. Then you would interact with the key and pick it up and it would go on top of your head, and you are free to take it anywhere to escape the escape room. Now the problem I'm having, is I'm able to interact with the desk, and the collision works. But the key doesn't spawn.
Here is my code:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
#VARIABLES
velocity = 1
yellow = (255,255,0)
clock = pygame.time.Clock()
white = (255,255,255)
blue = (0,0,255)
black = (0,0,0)
brown = (110,38,14)
x = 300
y = 300
#VARIABLES
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
def Room():
global Player
global x
global y
screen.fill(black)
Player = pygame.draw.rect(screen,blue,(x,y,50,50))
wall_1 = pygame.draw.rect(screen,white,(10,50,20,500))
wall_2 = pygame.draw.rect(screen,white,(10,30,580,20))
wall_3 = pygame.draw.rect(screen,white,(570,40,20,520))
wall_4 = pygame.draw.rect(screen,white,(10,540,580,20))
if Player.colliderect(wall_1):
x = x + 70
if Player.colliderect(wall_2):
y = y + 70
if Player.colliderect(wall_3):
x = x - 70
if Player.colliderect(wall_4):
y = y - 70
def Movement():
global velocity
global x
global y
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
x = x - velocity
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == ord('d'):
x = x + velocity
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == ord('w'):
y = y - velocity
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN or event.key == ord('s'):
y = y + velocity
def Text():
font = pygame.font.Font("freesansbold.ttf",15)
interact = font.render("E To Interact!",True, (255,255,255))
screen.blit(interact,(75,275))
def FirstKey():
global Player
global x
global y
KeyX = 300
KeyY = 400
key = pygame.draw.rect(screen,yellow,(KeyX,KeyY,15,15))
if Player.colliderect(key) == True:
if key[K_e]:
KeyX = x
KeyY = y - 35
def Desk():
global x
global velocity
Desk = pygame.draw.rect(screen,brown,(75,300,20,60))
if Player.colliderect(Desk) == True:
velocity = 0
Text()
keys = pygame.key.get_pressed()
if keys[K_e]:
x = 300
velocity = 1
FirstKey()
else:
None
#Function Calls#
Room()
Movement()
Desk()
#Function Calls#
pygame.display.update()
#FPS#
clock.tick(120)
#FPS#
I would appreciate an answer as well as an explanation on what I'm doing wrong.
P.S. I'm using the latest version of Python(3.10.5) and the Pygame module.
Try to encapsulate things in a class,
instead of calling the functions secuentially, as it happens in the game, split the functions on what they need to do, and call them on every cycle.
So you need to check for input, check for collisions, check inventary, check states, draw the objects, and so on.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,600))
#VARIABLES
velocity = 1
yellow = (255,255,0)
clock = pygame.time.Clock()
white = (255,255,255)
blue = (0,0,255)
black = (0,0,0)
brown = (110,38,14)
#VARIABLES
class mygame():
x = 300
y = 300
velocity = 1
state = 'idle'
draw_key = False
def draw_objects(self):
screen.fill(black)
self.player = pygame.draw.rect(screen,blue,(self.x,self.y,50,50))
self.wall_1 = pygame.draw.rect(screen,white,(10,50,20,500))
self.wall_2 = pygame.draw.rect(screen,white,(10,30,580,20))
self.wall_3 = pygame.draw.rect(screen,white,(570,40,20,520))
self.wall_4 = pygame.draw.rect(screen,white,(10,540,580,20))
self.desk = pygame.draw.rect(screen,brown,(75,300,20,60))
if self.draw_key:
self.key = pygame.draw.rect(screen,yellow,(200,200,20,20))
def check_collisions(self):
if self.player.colliderect(self.desk) == True:
font = pygame.font.Font("freesansbold.ttf",15)
interact = font.render("E To Interact!",True, (255,255,255))
screen.blit(interact,(75,275))
if self.player.colliderect(self.wall_1) == True:
self.x = 10
if self.player.colliderect(self.wall_2) == True:
self.y = 10
if self.player.colliderect(self.wall_3) == True:
self.x = 570
if self.player.colliderect(self.wall_4) == True:
self.y = 540
def check_buttons(self):
events = pygame.event.get()
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
self.state = 'interacting'
self.draw_key = False
if event.key == pygame.K_w or event.key == pygame.K_UP:
self.state = 'up'
if event.key == pygame.K_s or event.key == pygame.K_DOWN:
self.state = 'down'
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
self.state = 'left'
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
self.state = 'right'
if event.key == pygame.K_ESCAPE:
pygame.quit()
exit()
if event.type == pygame.KEYUP:
self.state = 'idle'
def move(self):
if self.state == 'up':
self.y -= self.velocity
if self.state == 'down':
self.y += self.velocity
if self.state == 'left':
self.x -= self.velocity
if self.state == 'right':
self.x += self.velocity
if self.state == 'idle':
pass
if self.state == 'interacting':
self.draw_key = True
def play(self):
while True:
self.draw_objects()
self.check_buttons()
self.check_collisions()
self.move()
pygame.display.update()
clock.tick(120)
game = mygame()
game.play()
The issue is when i press the left or right key while i press it, the sprite crosses the left and right boundaries of the screen. But when i tap it, it will not cross only when i hold the key continuosly
this is the class for the humanship
class Human:
y = display_height * 0.8
x = display_width * 0.45
width = 120
image = pygame.image.load('yasin/alien1.png')
def run(self):
gameDisplay.blit(Human.image, (Human.x, Human.y))
This is the main loop which iterates throughout the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if human.x > 0:
x_change = -8
else:
x_change = 0
elif event.key == pygame.K_RIGHT:
if human.x < display_width - human.width:
x_change = 8
else:
x_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
human.x += x_change
human.run()
Move the if human.x > 0: and if human.x < display_width - human.width: out of the event loop because they'll only be executed once per event in the event queue. Rather check in the main while loop if the player is still inside of the game area, otherwise stop it.
I've also changed a few more things: The attributes should be defined in the __init__ method to make them instance attributes instead of class attributes. Use self.x instead of Human.x in the class. The x_change and y_change variables belong to the human object, so they should be attributes as well. Then you can add an update method to the Human in which you do the bounds checking and the movement.
import pygame
display_width, display_height = 640, 480
class Human:
def __init__(self):
self.image = pygame.image.load('yasin/alien1.png')
self.y = display_height * 0.8
self.x = display_width * 0.45
self.x_change = 0
self.y_change = 0
self.width = 120
def run(self, gameDisplay):
gameDisplay.blit(self.image, (self.x, self.y))
def update(self):
self.x += self.x_change
# Check if the human is outside of the game area.
if self.x < 0:
self.x_change = 0 # Stop it.
self.x = 0 # Reset the position, so that we can move again.
elif self.x > display_width - self.width:
self.x_change = 0
self.x = display_width - self.width
def main():
pygame.init()
gameDisplay = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
human = Human()
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
human.x_change = -8
elif event.key == pygame.K_RIGHT:
human.x_change = 8
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
human.x_change = 0
human.update()
gameDisplay.fill((30, 30, 30))
human.run(gameDisplay)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pygame.quit()
I'm a new programmer and am trying to figure out why my bullets aren't showing up. It seems that the Y coordinate changes, but for some reason the bullets are not showing up. This is my code in Python:
#Importing necessary modules
import random
import pygame
import sys
#Setting up pygame
pygame.init()
shooting = False
n = 0
keys = [False,False,False,False]
clock = pygame.time.Clock()
screen = pygame.display.set_mode([500,500])
font = pygame.font.Font(None,50)
#Creating class for player
class Player:
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self):
pygame.draw.rect(screen,[0,255,0],
[int(self.x),int(self.y),int(self.width),int(self.height)],0)
def move(self):
if keys[1] == True:
self.x -= 1
elif keys[3] == True:
self.x += 1
if self.x < 0:
print(self.x)
self.x = 0
if self.x > 500 - self.width:
print(self.x)
self.x = 500 - self.width
def shoot(self):
return
class Bullet:
def __init__(self,x,y):
self.x = x
self.y = y
def update(self,y_amount = 5):
self.y += y_amount
return
def draw(self):
pygame.draw.rect(screen,[0,255,0],[int(self.x),int(self.y),10,30],0)
bullets = []
#Creating a player
player = Player(200,450,40,20)
#Main Loop
while True:
clock.tick(60)
#Background
screen.fill([0,0,0])
#Letting Player move
player.move()
#Drawing Player
player.draw()
#Updating screen
pygame.display.flip()
#Checking for events
for event in pygame.event.get():
#Checking for quit
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
#Checking for keys
if event.key == pygame.K_w:
keys[0] = True
elif event.key == pygame.K_a:
keys[1]=True
elif event.key == pygame.K_s:
keys[2]=True
elif event.key == pygame.K_d:
keys[3]=True
elif event.key == pygame.K_SPACE:
shooting = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
keys[0]=False
elif event.key == pygame.K_a:
keys[1]=False
elif event.key == pygame.K_s:
keys[2]=False
elif event.key == pygame.K_d:
keys[3]=False
elif event.key == pygame.K_SPACE:
shooting = False
if shooting == True:
bullets.append(Bullet(player.x, player.y))
for bullet in bullets:
bullet.update()
bullet.draw()
Rule 1: Check your coordinate system.
Pygame has (0,0) at the top left, your player is at (x, 450) - at the bottom. When you create a bullet, you do so at the player coordinate and then update the position to increase Y, i.e. move downwards rather than upwards.
I believe that you need to update your screen at the end of the while-loop, not at the beginning:
while True:
#fill screen
for event in pygame.event.get():
#get user input
pygame.display().flip()
I am moving a sprite on the x-axis. It is properly moving left and right. When I press both LEFT and RIGHT at the same time it stop moving properly.
I am trying to make it when a user presses both keys and then lets go of one, for it to continue moving in the direction still pressed.
Weirdly it works while I holding right and letting go of left. It continues moving right.
When I hold left and tap right it stops moving until I press right again.
I commented out some ideas I had to make this work, but they failed me.
I am sure its a simple fix or a logic failure on my part.
I have worked a couple hours on this.
Thanks for responses ahead of time.
import pygame
import time
import random
import sys
import math
pygame.init()
displayWidth = 1200
displayHeight = 800
white = (255,255,255)
black = (0,0,0)
gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption('Game 3')
clock = pygame.time.Clock()
class firstSquare:
def __init__(self,player_x,player_y):
self.x = player_x
self.y = player_y
self.width = 100
self.height = 100
def render(self):
pygame.draw.rect(gameDisplay, white,(self.x, self.y, self.width, self.height))
class secondSquare:
def __init__(self,cpu_x,cpu_y):
self.x = cpu_x
self.y = cpu_y
self.width = 100
self.height = 100
def render(self):
pygame.draw.rect(gameDisplay, white,(self.x, self.y, self.width, self.height))
player = firstSquare(300,300)
cpu = secondSquare(100,100)
def gameLoop():
### variables##
player_x = 100
player_y = 100
x = 100
y = 100
movement_x = 0
movement_y = 0
frame_rate = 0
frame_table = 0
inGame = True
while inGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
inGame = False
pygame.quit()
sys.exit()
keyPressed= pygame.key.get_pressed()
#### this is moving the player on x-axis##
if keyPressed[pygame.K_LEFT]:
movement_x = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
movement_x = 0
if keyPressed[pygame.K_RIGHT]:
movement_x = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
movement_x = 0
### two keys at once won't move the player###
if keyPressed[pygame.K_LEFT] and keyPressed[pygame.K_RIGHT]:
movement_x = 0
### pressing one key and letting go the other will continue movement
## if keyPressed[pygame.K_LEFT] and keyPressed[pygame.K_RIGHT]:
## if event.type == pygame.KEYUP:
## if event.key == pygame.K_LEFT:
## movement_x = 5
## print("left dropped")
## if keyPressed[pygame.K_RIGHT] and keyPressed[pygame.K_LEFT]:
## if event.type == pygame.KEYUP:
## if event.key == pygame.K_RIGHT:
## movement_x = -5
## print("Right dropped")
gameDisplay.fill(black)
player.render()
cpu.render()
player.x += movement_x
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()
I think what you need is this:
if keyPressed[pygame.K_LEFT]:
movement_x = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and movement_x < 0:
movement_x = 0
if keyPressed[pygame.K_RIGHT]:
movement_x = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT and movement_x > 0:
movement_x = 0
And that would be it... Hope it helps.
Try to use the following code in your movement:
if keyPressed[pygame.K_LEFT]:
movement_x = -5
elif keyPressed[pygame.K_RIGHT]:
movement_x = +5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
movement_x = 0
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
movement_x = 0
I would like to know how to create a border in Pygame to stop the user controlled object from exiting the screen. Right now, I only have it so python prints some text when the user controlled object has come near one of the 4 sides.
Here is my code so far.
import pygame
from pygame.locals import *
pygame.init()
#Display Stuff
screenx = 1000
screeny = 900
screen = pygame.display.set_mode((screenx,screeny))
pygame.display.set_caption('Block Runner')
clock = pygame.time.Clock()
image = pygame.image.load('square.png')
#Color Stuff
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
black = (0,0,0)
#Variables
x_blocky = 50
y_blocky = 750
blocky_y_move = 0
blocky_x_move = 0
#Animations
def Blocky(x_blocky, y_blocky, image):
screen.blit(image,(x_blocky,y_blocky))
#Game Loop
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
blocky_y_move = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
blocky_y_move = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
blocky_y_move = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
blocky_y_move = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
blocky_x_move = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
blocky_x_move = 0
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
blocky_x_move = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
blocky_x_move = 0
if x_blocky > 870 or x_blocky < 0:
print(' X Border')
if y_blocky > 750 or y_blocky < 2:
print(' Y Border')
y_blocky += blocky_y_move
x_blocky += blocky_x_move
screen.fill(white)
Blocky(x_blocky, y_blocky, image)
pygame.display.update()
clock.tick(60)
Don't use integers to store your position. Use a Rect.
So instead of
x_blocky = 50
y_blocky = 750
use
blocky_pos = pygame.rect.Rect(50, 750)
Now you can simply use
blocky_pos.move_ip(blocky_x_move, blocky_y_move)
to move your object.
After moving, you can simply call clamp/clamp_ip to ensure the blocky_pos Rect is always inside the screen.
blocky_pos.clamp_ip(screen.get_rect())
Also, you don't need to define basic colors yourself, you could simply use pygame.color.Color('Red') for example.
I also suggest you use pygame.key.get_pressed() to get all pressed keys to see how to move your object instead of creating 1000 lines of event handling code.
Well, simply don't increase your move variable any further, if you detect that the user object is near or at the border. Or reverse the move direction, depending on your general intent.
if x_blocky > 870 or x_blocky < 0:
print(' X Border')
blocky_x_move = 0
if y_blocky > 750 or y_blocky < 2:
print(' Y Border')
blocky_y_move = 0
Also, you have some redundant code with your keyboard movement. Instead of writing
if event.type == KEYDOWN:
over and over again, group the KEYUP if statements and KEYDOWN if statements.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
blocky_y_move = -3
elif event.key == pygame.K_DOWN:
blocky_y_move = +3
etc, and:
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
blocky_y_move = 0
elif event.type == pygame.K_DOWN:
blocky_y_move = 0
etc
You can set the boundaries using the min and max functions.
Here is the concept:
We have a pygame object that moves in all four directions; lets say the user holds down the LEFT arrow key, so that the object reaches the top of the screen. The y-coordinate of the top of the screen will always be 0, so we want the object to come to a stop at y-coordinate 0.
This may seem as simple as:
if char.rect.y > 0:
char.rect.y -= char.speed
But this will result in a bug ig char.speed is greater than 1. Like when the object is at y-coordinate 5,
and its speed is 10; the condition still allows for one more step for the object, resulting in the object
coming 5 pixels out of the pygame window. What we want to do is more like:
if char.rect.y > 0:
char.rect.y -= char.speed
if char.rect.y < 0:
char.rect.y = 0
to push the object back into the boundaries. The above block of code can be simplified with the max function:
self.rect.y = max([self.rect.y - self.speed, 0])
For the object moving down:
if char.rect.y < HEIGHT - char.height:
char.rect.y += char.speed
if char.rect.y > HEIGHT - char.height:
char.rect.y = HEIGHT - char.height
or, the more efficient and clean method:
self.rect.y = min([self.rect.y + self.speed, HEIGHT - self.height])
For going left and right, simply replace the ys and height (and HEIGHT) from two lines above with xs and widths (and WIDTH).
All together:
import pygame
pygame.init()
WIDTH = 600
HEIGHT = 600
wn = pygame.display.set_mode((WIDTH, HEIGHT))
class Player:
def __init__(self):
self.speed = 1
self.width = 20
self.height = 20
self.color = (255, 255, 0)
self.rect = pygame.Rect((WIDTH - self.width) / 2, (HEIGHT - self.height) / 2, 20, 20)
def up(self):
self.rect.y = max([self.rect.y - self.speed, 0])
def down(self):
self.rect.y = min([self.rect.y + self.speed, HEIGHT - self.height])
def left(self):
self.rect.x = max([self.rect.x - self.speed, 0])
def right(self):
self.rect.x = min([self.rect.x + self.speed, WIDTH - self.width])
def draw(self):
pygame.draw.rect(wn, self.color, self.rect)
char = Player()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
char.up()
if keys[pygame.K_DOWN]:
char.down()
if keys[pygame.K_LEFT]:
char.left()
if keys[pygame.K_RIGHT]:
char.right()
wn.fill((0, 0, 0))
char.draw()
pygame.display.update()
Good luck!