I was programming player movement for my game and I'm not getting errors but the code isn't working correctly. The game is just presenting a black screen, if your able to help, please do that would be greatly appreciated! I've tried resetting my browser and such, but nothings working anyone able to help? (I'm using repl.it)
import pygame, sys
WIDTH, HEIGHT = 400, 400
TITLE = "DarkRoom"
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
clock = pygame.time.Clock()
class Player:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 32, 32)
self.x = int(x)
self.y = int(y)
self.color = (255, 255, 255)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 4
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
def update(self):
self.velX = 0
self.velY = 0
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed:
self.velY = -self.speed
if self.down_pressed and not self.up_pressed:
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(self.x, self.y, 32, 32)
player = Player(WIDTH/2, HEIGHT/2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False
win.fill((12, 24, 36))
player.draw(win)
player.update()
pygame.display.flip()
clock.tick(120)
It is just a matter of Indentation. You have to draw the player and update the display in the application loop, but not after the application loop:
while True:
for event in pygame.event.get():
# [...]
# INDENTATION
#->|
player.update()
pygame.display.flip()
clock.tick(120)
Related
When I run my code, it instantly opens then closes the pygame window, however when I delete the player.update() code it works. I can't seem to find what is wrong with the update function in my player class
"""Dot Game"""
#Imports
import pygame, sys
from game import Game
g = Game()
while g.running:
g.curr_menu.display_menu()
g.game_loop()
#Constants
WIDTH, HEIGHT = 1280, 720
TITLE = "Dot."
background = pygame.image.load('BG.png')
background = pygame.transform.scale(background, (WIDTH, HEIGHT))
#pygame initialization
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
#fps
FPS = 60
fpsclock = pygame.time.Clock()
#colors
bgc = (247, 226, 222)
pc = (152, 193, 217)
pc2 = (61, 90, 128) #dark
ec = (119, 2, 26) #dark
ec2 = (220, 66, 73)
#accel
x_change = 0
y_change = 0
accel_x = 0
accel_y = 0
max_speed = 10
display_width, display_height = pygame.display.get_surface().get_size()
x = display_width * 0.45
y = display_height * 0.8
#Player Class
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.x = int(x)
self.y = int(y)
self.radius = 32
self.width = 2
self.color = pc
self.color2 = pc2
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.a_pressed = False
self.d_pressed = False
self.w_pressed = False
self.s_pressed = False
self.attack_animation = False
self.sprites = []
self.sprites.append(pygame.image.load('0000.png'))
self.sprites.append(pygame.image.load('0001.png'))
self.sprites.append(pygame.image.load('0002.png'))
self.sprites.append(pygame.image.load('0003.png'))
self.current_sprite = 0
self.image = self.sprites[self.current_sprite]
self.rect = self.image.get_rect()
self.rect.topleft = [x,y]
self.acceleration = 0.4
self.friction = 0.92 # = 1 is no friction
def attack(self):
self.attack_animation = True
def update(self,speed):
if self.attack_animation == True:
self.current_sprite += speed
if int(self.current_sprite) >= len(self.sprites):
self.current_sprite = 1
self.attack_animation = False
self.image = self.sprites[int(self.current_sprite)]
if self.left_pressed:
if self.velX > -max_speed:
self.velX -= self.acceleration
if self.right_pressed:
if self.velX < max_speed:
self.velX += self.acceleration
if self.up_pressed:
if self.velY > -max_speed:
self.velY -= self.acceleration
if self.down_pressed :
if self.velY < max_speed:
self.velY += self.acceleration
self.x += self.velX
self.y += self.velY
self.velX *= self.friction
self.velY *= self.friction
player.attack()
class Enemy:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.radius = 32
self.color = ec
self.color2 = ec2
self.vel = 3
self.hitbox = (self.x -20, self.y -20, 40, 40)
self.points = [(self.x -10, self.y +14), (self.x, self.y -6), (self.x +10, self.y +14)]
self.points2 = [(self.x -20, self.y +20), (self.x, self.y -20), (self.x +20, self.y +20)] # L U R
def draw(self, win):
pygame.draw.polygon(win, self.color, self.points2)
pygame.draw.polygon(win, self.color2, self.points)
self.hitbox = (self.x -20, self.y -20, 40, 40)
#pygame.draw.rect(win, ec2, self.hitbox, 2)
#Player Initialization
moving_sprites = pygame.sprite.Group()
player = Player(WIDTH/2, HEIGHT/2)
enemy = Enemy(WIDTH/2, HEIGHT/2)
moving_sprites.add(player)
#Main Loop
collide = False
while not collide:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
elif event.key == pygame.K_a:
player.left_pressed = True
elif event.key == pygame.K_RIGHT:
player.right_pressed = True
elif event.key == pygame.K_d:
player.right_pressed = True
elif event.key == pygame.K_UP:
player.up_pressed = True
elif event.key == pygame.K_w:
player.up_pressed = True
elif event.key == pygame.K_DOWN:
player.down_pressed = True
elif event.key == pygame.K_s:
player.down_pressed = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
elif event.key == pygame.K_a:
player.left_pressed = False
elif event.key == pygame.K_RIGHT:
player.right_pressed = False
elif event.key == pygame.K_d:
player.right_pressed = False
elif event.key == pygame.K_UP:
player.up_pressed = False
elif event.key == pygame.K_w:
player.up_pressed = False
elif event.key == pygame.K_DOWN:
player.down_pressed = False
elif event.key == pygame.K_s:
player.down_pressed = False
x_change += accel_x # Accelerate.
if abs(x_change) >= max_speed: # If max_speed is exceeded.
# Normalize the x_change and multiply it with the max_speed.
x_change = x_change/abs(x_change) * max_speed
y_change += accel_y # Accelerate.
if abs(y_change) >= max_speed: # If max_speed is exceeded.
# Normalize the x_change and multiply it with the max_speed.
y_change = y_change/abs(y_change) * max_speed
#Draw
win.blit(background,(0,0))
enemy.draw(win)
moving_sprites.draw(win)
#update
#player.update()
pygame.display.update()
moving_sprites.update(0.12)
fpsclock.tick(FPS)
The update method has a speed argument.
def update(self,speed):
Therefore you need to pass the speed to the update method:
player.update()
speed = 1 # just for example
player.update(speed)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I don't what happened, it was working few minutes ago, but now it's not. It's crashing every time.
import pygame, sys
WIDTH, HEIGHT = 800, 700
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Qix")
clock = pygame.time.Clock()
# Player Class
class Player:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.rect = pygame.Rect(self.x, self.y, 32, 32)
self.color = (250, 120, 60)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 4
self.enterPush = False
while self.enterPush is True:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
self.x += self.velX
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
def push(self):
if self.rect.x>760 or self.rect.y>560:
self.enterPush = True
def update(self):
self.velX = 0
self.velY = 0
while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
player = Player(WIDTH / 2, HEIGHT / 2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False
win.fill((12, 24, 36))
player.draw(win)
player.update()
pygame.display.flip()
clock.tick(60)
pygame.display.update()
I have to fix this in one hour.
I read other answers related to this but nothing seems to work.
Just adding details
Just adding details Just adding details
Just adding details
Just adding details
Just adding details
Here You go, fixed the code:
import pygame
WIDTH, HEIGHT = 800, 700
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Qix")
clock = pygame.time.Clock()
# Player Class
class Player:
def __init__(self, x, y):
self.x = int(x)
self.y = int(y)
self.rect = pygame.Rect(self.x, self.y, 32, 32)
self.color = (250, 120, 60)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 4
self.enterPush = False
while self.enterPush is True:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
self.x += self.velX
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)
def push(self):
if self.rect.x>760 or self.rect.y>560:
self.enterPush = True
def update(self):
self.velX = 0
self.velY = 0
# while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
player = Player(WIDTH / 2, HEIGHT / 2)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False
win.fill((12, 24, 36))
player.draw(win)
player.update()
# pygame.display.flip()
clock.tick(60)
pygame.display.update()
First of about formatting (suggestion according to PEP 8): 1 newline between class method definitions, import like in the code I edited (tho this is acceptable: from module import class, func, etc).
Also I don't see the point of both .flip() and .update() since they basically do the same.
About Your poblem:
while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
this was Your issue: the while loop
solution: not using it:
def update(self):
self.velX = 0
self.velY = 0
# while self.enterPush is False:
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed :
self.velY = -self.speed
if self.down_pressed and not self.up_pressed :
self.velY = self.speed
self.x += self.velX
self.y += self.velY
self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)
This question already has an answer here:
Python pygame need help disabling multiple keypresses at once [duplicate]
(1 answer)
Closed 2 years ago.
I am working on a pygame version of the game snake. The problem I am having so far is that I don't want to allow diagonal movement but currently there is. Any help would be much appreciated. Thanks in advance 🙂
import pygame
class Snake:
def __init__(self):
self.x = 250
self.y = 250
self.x_change = 0
self.y_change = 0
self.width = 25
self.height = 25
self.win = pygame.display.set_mode((500,500))
self.clock = pygame.time.Clock()
self.run = True
pygame.init()
pygame.display.set_caption('Snake')
def gameLoop(self):
while self.run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.x_change = -10
elif event.key == pygame.K_RIGHT:
self.x_change = 10
elif event.key == pygame.K_UP:
self.y_change = -10
elif event.key == pygame.K_DOWN:
self.y_change = 10
self.x += self.x_change
self.y += self.y_change
self.win.fill((76, 66, 53))
pygame.draw.rect(self.win, (145, 255, 51), (self.x, self.y, self.width, self.height))
pygame.display.update()
self.clock.tick(3)
pygame.quit()
p1 = Snake()
p1.gameLoop()
You can reset the change for the other axis back to 0. This way only one direction is changed per key press
def gameLoop(self):
while self.run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.x_change = -10
self.y_change = 0
elif event.key == pygame.K_RIGHT:
self.x_change = 10
self.y_change = 0
elif event.key == pygame.K_UP:
self.y_change = -10
self.x_change = 0
elif event.key == pygame.K_DOWN:
self.y_change = 10
self.x_change = 0
self.x += self.x_change
self.y += self.y_change
self.win.fill((76, 66, 53))
pygame.draw.rect(self.win, (145, 255, 51), (self.x, self.y, self.width, self.height))
pygame.display.update()
self.clock.tick(3)
pygame.quit()
It should call events() function and restart the whole process by displaying the block on the screen again, why isn't it working?
I tried breaking out of gameover(), tried calling pygame.display.update() but nothing seems to work [This is my 1st python game ever, so I may have been a little messy in typing my code]. Can anyone help me solve the problem?
import pygame
import time
pygame.init()
global display_height
global display_width
display_height = 800
display_width = 800
class slither(object):
def __init__(self,top):
self.top=top
self.x = display_height/2
self.y = display_width/2
self.x_change = 0
self.y_change = 0
self.gameOver = False
self.gameLoop = False
self.FPS=30
self.brick_size=10
self.black = (0,0,0)
self.white = (255,255,255)
self.red = (255,0,0)
self.font = pygame.font.SysFont(None, 30)
self.clock = pygame.time.Clock()
self.events()
self.message('',(0,0,0))
self.gameover()
def create_move(self, a, b):
pygame.draw.rect(screen, self.black , [a,b,self.brick_size,self.brick_size])
def message(self, msg, color):
screen_text=self.font.render(msg, True, color)
screen.blit(screen_text, [display_height/2, display_width/2])
pygame.display.update()
def events(self):
while self.gameLoop == False:
screen.fill(self.white)
self.create_move(self.x,self.y)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.const=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.x_change = -self.brick_size
self.y_change = 0
elif event.key == pygame.K_RIGHT:
self.x_change = self.brick_size
self.y_change = 0
elif event.key == pygame.K_UP:
self.y_change = -self.brick_size
self.x_change = 0
elif event.key == pygame.K_DOWN:
self.y_change = self.brick_size
self.x_change = 0
if self.x_change == -self.brick_size:
self.x -= self.brick_size
y=self.y
self.create_move(self.x, y)
elif self.x_change == self.brick_size:
self.x += self.brick_size
y=self.y
self.create_move(self.x, y)
if self.y_change == -self.brick_size:
self.y -= self.brick_size
x=self.x
self.create_move(x, self.y)
elif self.y_change == self.brick_size:
self.y += self.brick_size
x=self.x
self.create_move(x, self.y)
if self.x == display_height or self.x < 0 or self.y == display_width or self.y < 0 :
self.gameover()
pygame.display.update()
self.clock.tick(30)
def gameover(self):
while self.gameOver == False:
screen.fill((255, 255, 255))
self.message('Game over, Press any to continue or W to quit ', self.black)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
screen.fill(self.white)
self.message('Game Over, You lose', self.black)
time.sleep(2)
pygame.quit()
quit()
else:
self.events() #continue
screen=pygame.display.set_mode((display_height,display_width))
pygame.display.set_caption('Snake')
slither(0).events()
pygame.quit()
quit()
EDIT: I SOLVED IT, THANKS.
This is all I had to change.
Basically, because the block was still out of the screen, hence the gameover() used to get executed again, so when I brought the block back onto the screen, it worked.
else: #continue
self.x=display_height/2
self.y=display_width/2
self.events()
I've been at it for a while, and I cannot seem to figure out why my sprite is not moving up or down when the program is run.
Player Sprite Class
def __init__(self,name,x,y):
pygame.sprite.Sprite.__init__(self)
self.name = str(name)
self.speed = 5
self.image = pygame.image.load(name+'.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def move_up(self):
self.rect.y -= self.speed
def move_down(self):
self.rect.y += self.speed
def move_left(self):
self.rect.x -= self.speed
def move_right(self):
self.rect.x += self.speed
def update(self):
self.screen.blit(self.image,(self.rect))
Main File / Loop
class Console:
def __init__(self,width,height):
pygame.init()
self.w = width
self.h = height
self.clock = pygame.time.Clock()
self.screen = pygame.display.set_mode((self.w,self.h))
self.screenName = pygame.display.set_caption("Star's Labyrinth")
class Background(pygame.sprite.Sprite):
def __init__(self,image_file,x,y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def main():
gameExit = False
game_display = Console(1024,576)
game_display.screen
starboy = Player("starboy",200,200)
background = Background("background.png",0,0)
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
starboy.move_left()
elif event.key == pygame.K_d:
starboy.move_right()
elif event.type == pygame.K_w:
starboy.move_up()
elif event.type == pygame.K_s:
starboy.move_down()
print(pygame.event.get())
game_display.screen.blit(background.image,background.rect)
starboy.update
game_display.screen.blit(starboy.image,(starboy.rect.x,starboy.rect.y))
pygame.display.update()
game_display.clock.tick(30)
I have tried different types of commands trying to figure out whether it's a variable or function problem. Also is there a way I can make it move without having to tap the key.
In the event loop you're checking if the event.type is pygame.K_w or pygame.K_s, but you have to check if the event.key is equal to these constants. Change
elif event.type == pygame.K_w:
starboy.move_up()
elif event.type == pygame.K_s:
starboy.move_down()
to:
elif event.key == pygame.K_w:
starboy.move_up()
elif event.key == pygame.K_s:
starboy.move_down()
You also forgot the parentheses behind starboy.update.