Running pygame instantly closes. Something is wrong with my player update function - python

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)

Related

My player isn't showing when I run my Pygame project

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)

I need to be able to pause the whole game, but only my car sprite pauses when I press the p key on my keyboard

I implemented a pause feature in my game in the while loop at the end of my code, but the problem is that only my car sprite pauses. I have tried a bunch of different ways setting up my pause code but none of the ways I arrange it seems to pause the whole game.
I want to be able to pause my whole game when I press the p key on my keyboard, not just my car sprite. Can someone please tell me how I can fix this code? The code that will pause the whole game will be nice. Please, the rearranged code that will pause the whole game will help. Also I don't just want the answer, I want a quick explanation of why only my car sprite would pause instead of the whole game.
My code:
import random
import pygame
import pygame.freetype
pygame.init()
#screen settings
WIDTH = 1000
HEIGHT = 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))
#fps
FPS = 120
clock = pygame.time.Clock()
#load images
bg = pygame.image.load('background/street.png').convert_alpha() # background
bullets = pygame.image.load('car/bullet.png').convert_alpha()
debris_img = pygame.image.load('debris/cement.png')
#define game variables
shoot = False
#player class
class Player(pygame.sprite.Sprite):
def __init__(self, scale, speed):
pygame.sprite.Sprite.__init__(self)
self.bullet = pygame.image.load('car/bullet.png').convert_alpha()
self.bullet_list = []
self.speed = speed
#self.x = x
#self.y = y
self.moving = True
self.frame = 0
self.flip = False
self.direction = 0
self.score = 0
#load car
self.images = []
img = pygame.image.load('car/car.png').convert_alpha()
img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.update_time = pygame.time.get_ticks()
self.movingLeft = False
self.movingRight = False
self.rect.x = 465
self.rect.y = 325
#draw car to screen
def draw(self):
screen.blit(self.image, (self.rect.centerx, self.rect.centery))
#move car
def move(self):
#reset the movement variables
dx = 0
dy = 0
#moving variables
if self.movingLeft and self.rect.x > 33:
dx -= self.speed
self.flip = True
self.direction = -1
if self.movingRight and self.rect.x < 900:
dx += self.speed
self.flip = False
self.direction = 1
#update rectangle position
self.rect.x += dx
self.rect.y += dy
#shoot
def shoot(self):
bullet = Bullet(self.rect.centerx + 18, self.rect.y + 30, self.direction)
bullet_group.add(bullet)
#check collision
def collision(self, debris_group):
for debris in debris_group:
if debris.health > 0 and pygame.sprite.spritecollide(debris, bullet_group, True):
debris.health -= 1
if debris.health <= 0:
self.score += 1
#player stats/score
def stats(self):
myfont = pygame.font.SysFont('comicsans', 30)
scoretext = myfont.render("Score: " + str(self.score), 1, (0,0,0))
screen.blit(scoretext, (100,10))
#bullet class
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, direction):
pygame.sprite.Sprite.__init__(self)
self.speed = 5
self.image = bullets
self.rect = self.image.get_rect()
self.rect.center = (x,y)
self.direction = direction
def update(self):
self.rect.centery -= self.speed
#check if bullet has gone off screen
if self.rect.centery < 1:
self.kill()
#debris class
class Debris(pygame.sprite.Sprite):
def __init__(self,scale,speed):
pygame.sprite.Sprite.__init__(self)
self.scale = scale
self.x = random.randrange(100,800)
self.speed_y = 10
self.y = 15
self.speed = speed
self.vy = 0
self.on_ground = True
self.move = True
self.health = 4
self.max_health = self.health
self.alive = True
self.velocity = random.randrange(1,2)
self.speed_x = random.randrange(-3,3)
self.moving_down = True
self.is_destroyed = False
#load debris
self.image = debris_img
self.rect = self.image.get_rect()
self.rect.x = random.randrange(100, 800)
self.rect.y = random.randrange(-150, -100)
self.rect.center = (self.x,self.y)
#load explosion
self.img_explosion_00 = pygame.image.load('explosion/0.png').convert_alpha()
self.img_explosion_00 = pygame.transform.scale(self.img_explosion_00, (self.img_explosion_00.get_width() * 2,
self.img_explosion_00.get_height() * 2))
self.img_explosion_01 = pygame.image.load('explosion/1.png').convert_alpha()
self.img_explosion_01 = pygame.transform.scale(self.img_explosion_01, (self.img_explosion_01.get_width() * 2,
self.img_explosion_01.get_height() * 2))
self.img_explosion_02 = pygame.image.load('explosion/2.png').convert_alpha()
self.img_explosion_02 = pygame.transform.scale(self.img_explosion_02, (self.img_explosion_02.get_width() * 2,
self.img_explosion_02.get_height() * 2))
self.img_explosion_03 = pygame.image.load('explosion/3.png').convert_alpha()
self.img_explosion_03 = pygame.transform.scale(self.img_explosion_03, (self.img_explosion_03.get_width() * 2,
self.img_explosion_03.get_height() * 2))
#explosion list
self.anim_explosion = [self.img_explosion_00,
self.img_explosion_01,
self.img_explosion_02,
self.img_explosion_03]
self.anim_index = 0
self.frame_len = 20 #frames before explosion animation disappears
#spawn new debris
def spawn_new_debris(self):
self.rect.x = random.randrange(100, 800)
self.rect.y = random.randrange(-150, -100)
self.velocity = random.randrange(1, 2)
self.speed_x = random.randrange(-3, 3)
#respawn debris when they go of the screen
def boundaries(self):
if self.rect.left > WIDTH + 10 or self.rect.right < -10 or self.rect.top > HEIGHT + 10:
self.spawn_new_debris()
#update image
def update(self):
self.rect.y += self.velocity
self.rect.x += self.speed_x
self.boundaries()
if self.health <= 0:
max_index = len(self.anim_explosion) - 1
if self.anim_index > max_index:
self.kill()
else:
if self.frame_len == 0:
self.image = self.anim_explosion[self.anim_index]
self.anim_index += 1
self.frame_len = 20
else:
self.frame_len -= 1
#make debris fall down
def falldown(self):
self.rect.centery += self.velocity
if self.moving_down and self.rect.y > 350:
self.kill()
######################CAR/DEBRIS##########################
player = Player(1,5)
##########################################################
#groups
bullet_group = pygame.sprite.Group()
debris_group = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
for x in range(100):
d = Debris(1, 5)
debris_group.add(d)
all_sprites.add(d)
#game runs here
run = True
paused = False
while run:
#draw street
screen.blit(bg, [0, 0])
#update groups
bullet_group.update()
bullet_group.draw(screen)
debris_group.update()
debris_group.draw(screen)
#draw car
player.draw()
player.move()
player.collision(debris_group)
player.stats()
#update all sprites
all_sprites.update()
all_sprites.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pause game
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
if paused == True:
continue
#check if key is down
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_a:
player.movingLeft = True
if event.key == pygame.K_d:
player.movingRight = True
if event.key == pygame.K_SPACE:
player.shoot()
shoot = True
#check if key is up
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player.movingLeft = False
if event.key == pygame.K_d:
player.movingRight = False
#update the display
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
Move your
for event in pygame.event.get():
right below the while loop, and move your
if paused:
continue
below the for loop, like so:
run = True
paused = False
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pause game
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
#check if key is down
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_a:
player.movingLeft = True
if event.key == pygame.K_d:
player.movingRight = True
if event.key == pygame.K_SPACE:
player.shoot()
shoot = True
#check if key is up
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player.movingLeft = False
if event.key == pygame.K_d:
player.movingRight = False
if paused:
continue
#draw street
screen.blit(bg, [0, 0])
#update groups
bullet_group.update()
bullet_group.draw(screen)
debris_group.update()
debris_group.draw(screen)
#draw car
player.draw()
player.move()
player.collision(debris_group)
player.stats()
#update all sprites
all_sprites.update()
all_sprites.draw(screen)
#update the display
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
The reason only your car sprite paused when you pressed the q key is because, as you can see in your original code here:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pause game
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
if paused == True:
continue
you put the
if paused == True:
continue
inside the for loop, not the while loop, so the lines that move the car would be blocked, but the rest of the code within the while loop will still get executed.

Python Snake doesn't grow [duplicate]

This question already has an answer here:
How do I get the snake to grow and chain the movement of the snake's body?
(1 answer)
Closed 1 year ago.
I'm new to python and only now the basics, I'm trying to make a snake game but I can't seem to figure out how to make my snake grow 1 extra square each time it eats an apple. My reasoning is that I keep a list of all the old positions but only show the last one on the screen, and each time the snakes eats another apple it shows 1 extra old positions making the snake 1 square longer.
This is my code:
import pygame
from random import randint
WIDTH = 400
HEIGHT = 300
dis = pygame.display.set_mode((WIDTH,HEIGHT))
white = (255,255,255)
BACKGROUND = white
blue = [0,0,255]
red = [255,0,0]
class Snake:
def __init__(self):
self.image = pygame.image.load("snake.bmp")
self.rect = self.image.get_rect()
self.position = (10,10)
self.direction = [0,0]
self.positionslist = [self.position]
self.length = 1
pygame.display.set_caption('Snake ')
def draw_snake(self,screen):
screen.blit(self.image, self.position)
def update(self):
self.position = (self.position[0] + self.direction[0],self.position[1] + self.direction[1])
self.rect = (self.position[0],self.position[1],5, 5 )
self.positionslist.append(self.position)
if self.position[0]< 0 :
self.position = (WIDTH,self.position[1])
elif self.position[0] > WIDTH:
self.position = (0,self.position[1])
elif self.position[1] > HEIGHT:
self.position = (self.position[0],0)
elif self.position[1] < 0 :
self.position = (self.position[0],HEIGHT)
class Food:
def __init__(self):
self.image = pygame.image.load("appel.png")
self.rect = self.image.get_rect()
apple_width = -self.image.get_width()
apple_height = -self.image.get_height()
self.position = (randint(0,WIDTH+apple_width-50),randint(0,HEIGHT+apple_height-50))
self.rect.x = self.position[0]
self.rect.y = self.position[1]
def draw_appel(self,screen):
screen.blit(self.image, self.position)
def eat_appel (self, snake):
if self.rect.colliderect(snake.rect) == True:
self.position = (randint(0,WIDTH),randint(0,HEIGHT))
self.rect.x = self.position[0]
self.rect.y = self.position[1]
snake.length += 1
def main():
game_over = False
while not game_over:
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
snake = Snake()
food = Food()
while True:
screen.fill(BACKGROUND)
font = pygame.font.Font('freesansbold.ttf', 12)
text = font.render(str(snake.length), True, red, white)
textRect = text.get_rect()
textRect.center = (387, 292)
screen.blit(text,textRect)
snake.update()
#snake.update_list()
snake.draw_snake(screen)
food.draw_appel(screen)
food.eat_appel(snake)
pygame.display.flip()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
snake.direction = [0,1]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
snake.direction = [1,0]
if event.key == pygame.K_LEFT:
snake.direction = [-1,0]
if event.key == pygame.K_UP:
snake.direction = [0,-1]
if event.key == pygame.K_DOWN:
snake.direction = [0,1]
if __name__ == "__main__":
main()
This can have multiple reasons. Firstly, I saw you tried to increase the length of the snake by typing snake.length += 1, which may work (probably won't because the module pygame allows the snake to hover around, but not like the loop or conditional statements). One of my tips would be, to increase the length of the snake by using the idea of adding the score with your present snake.length every time (because once your score is 1 by eating an apple, your snake.length would be 2. And it increases with the score). This is my code (a few modifications might be needed):
import pygame
import time
import random
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
orange = (255, 165, 0)
width, height = 600, 400
game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Snake Mania")
clock = pygame.time.Clock()
snake_size = 10
snake_speed = 15
message_font = pygame.font.SysFont('ubuntu', 30)
score_font = pygame.font.SysFont('ubuntu', 25)
def print_score(score):
text = score_font.render("Score: " + str(score), True, orange)
game_display.blit(text, [0,0])
def draw_snake(snake_size, snake_pixels):
for pixel in snake_pixels:
pygame.draw.rect(game_display, white, [pixel[0], pixel[1], snake_size, snake_size])
def run_game():
game_over = False
game_close = False
x = width / 2
y = height / 2
x_speed = 0
y_speed = 0
snake_pixels = []
snake_length = 1
target_x = round(random.randrange(0, width - snake_size) / 10.0) * 10.0
target_y = round(random.randrange(0, height - snake_size) / 10.0) * 10.0
while not game_over:
while game_close:
game_display.fill(black)
game_over_message = message_font.render("Game Over!", True, red)
game_display.blit(game_over_message, [width / 3, height / 3])
print_score(snake_length - 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
game_over = True
game_close = False
if event.key == pygame.K_2:
run_game()
if event.type == pygame.QUIT:
game_over = True
game_close = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_speed = -snake_size
y_speed = 0
if event.key == pygame.K_RIGHT:
x_speed = snake_size
y_speed = 0
if event.key == pygame.K_UP:
x_speed = 0
y_speed = -snake_size
if event.key == pygame.K_DOWN:
x_speed = 0
y_speed = snake_size
if x >= width or x < 0 or y >= height or y < 0:
game_close = True
x += x_speed
y += y_speed
game_display.fill(black)
pygame.draw.rect(game_display, orange, [target_x, target_y, snake_size, snake_size])
snake_pixels.append([x, y])
if len(snake_pixels) > snake_length:
del snake_pixels[0]
for pixel in snake_pixels[:-1]:
if pixel == [x, y]:
game_close = True
draw_snake(snake_size, snake_pixels)
print_score(snake_length - 1)
pygame.display.update()
if x == target_x and y == target_y:
target_x = round(random.randrange(0, width - snake_size) / 10.0) * 10.0
target_y = round(random.randrange(0, height - snake_size) / 10.0) * 10.0
snake_length += 1
clock.tick(snake_speed)
pygame.quit()
quit()
run_game()

Pygame black screen crash [closed]

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)

Snakes game - boundaries

I started making this snakes game and I ran into a struggle. I want that when the snake reaches the end of one side it will come from the other side, if it reaches the end of left side it will come out of the right side and if it reaches the up end it will come out from the bottom. I'll really appreciate it if someone could maybe show me how to do that.
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
segment_width = 15
segment_height = 15
segment_margin = 3
x_change = segment_width + segment_margin
y_change = 0
class Segment(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([segment_width, segment_height])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
pygame.init()
screen = pygame.display.set_mode([1200, 800])
pygame.display.set_caption('Snakes')
allspriteslist = pygame.sprite.Group()
snake_segments = []
for i in range(3):
x = 250 - (segment_width + segment_margin) * i
y = 30
segment = Segment(x, y)
snake_segments.append(segment)
allspriteslist.add(segment)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = (segment_width + segment_margin) * -1
y_change = 0
if event.key == pygame.K_RIGHT:
x_change = (segment_width + segment_margin)
y_change = 0
if event.key == pygame.K_UP:
x_change = 0
y_change = (segment_height + segment_margin) * -1
if event.key == pygame.K_DOWN:
x_change = 0
y_change = (segment_height + segment_margin)
old_segment = snake_segments.pop()
allspriteslist.remove(old_segment)
x = snake_segments[0].rect.x + x_change
y = snake_segments[0].rect.y + y_change
segment = Segment(x, y)
snake_segments.insert(0, segment)
allspriteslist.add(segment)
screen.fill(BLACK)
allspriteslist.draw(screen)
pygame.display.flip()
clock.tick(5)
pygame.quit()
You can use if to check position and move to other side
x = snake_segments[0].rect.x + x_change
y = snake_segments[0].rect.y + y_change
if x < screen_rect.left:
x += screen_rect.width
elif x + segment_width + segment_margin > screen_rect.right:
x -= screen_rect.width
if y < screen_rect.top:
y += screen_rect.height
elif y + segment_height + segment_margin > screen_rect.bottom:
y -= screen_rect.height
segment = Segment(x, y)
But problem is that segments doesn't fill screen ideally - sometimes you should display part of segment on one side and rest on other side. You have to add method draw() to Segment which will check if you have to draw segment on both sides and draw two segments - first on one side and second on other side.
def draw(self, screen):
# draw oryginal segment
screen_rect = screen.get_rect()
screen.blit(self.image, self.rect)
# draw second segment on other side
# left - right
if self.rect.left < screen_rect.left:
temp_rect = self.rect.copy()
temp_rect.x += screen_rect.width
screen.blit(self.image, temp_rect)
elif self.rect.right > screen_rect.right:
temp_rect = self.rect.copy()
temp_rect.x -= screen_rect.width
screen.blit(self.image, temp_rect)
# top - bottom
if self.rect.top < screen_rect.top:
temp_rect = self.rect.copy()
temp_rect.y += screen_rect.height
screen.blit(self.image, temp_rect)
elif self.rect.bottom > screen_rect.bottom:
temp_rect = self.rect.copy()
temp_rect.y -= screen_rect.height
screen.blit(self.image, temp_rect)
But it means that you have to manually execut this function because group.draw() runs directly screen.blit(self.image, self.rect)
for x in allspriteslist:
x.draw(screen)
Full code
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
segment_width = 15
segment_height = 15
segment_margin = 3
x_change = segment_width + segment_margin
y_change = 0
class Segment(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([segment_width, segment_height])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def draw(self, screen):
screen_rect = screen.get_rect()
screen.blit(self.image, self.rect)
if self.rect.left < screen_rect.left:
temp_rect = self.rect.copy()
temp_rect.x += screen_rect.width
screen.blit(self.image, temp_rect)
elif self.rect.right > screen_rect.right:
temp_rect = self.rect.copy()
temp_rect.x -= screen_rect.width
screen.blit(self.image, temp_rect)
if self.rect.top < screen_rect.top:
temp_rect = self.rect.copy()
temp_rect.y += screen_rect.height
screen.blit(self.image, temp_rect)
elif self.rect.bottom > screen_rect.bottom:
temp_rect = self.rect.copy()
temp_rect.y -= screen_rect.height
screen.blit(self.image, temp_rect)
pygame.init()
screen = pygame.display.set_mode((1200, 800))
screen_rect = screen.get_rect()
pygame.display.set_caption('Snakes')
allspriteslist = pygame.sprite.Group()
snake_segments = []
for i in range(3):
x = 250 - (segment_width + segment_margin) * i
y = 30
segment = Segment(x, y)
snake_segments.append(segment)
allspriteslist.add(segment)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = (segment_width + segment_margin) * -1
y_change = 0
if event.key == pygame.K_RIGHT:
x_change = (segment_width + segment_margin)
y_change = 0
if event.key == pygame.K_UP:
x_change = 0
y_change = (segment_height + segment_margin) * -1
if event.key == pygame.K_DOWN:
x_change = 0
y_change = (segment_height + segment_margin)
old_segment = snake_segments.pop()
allspriteslist.remove(old_segment)
x = snake_segments[0].rect.x + x_change
y = snake_segments[0].rect.y + y_change
if x < screen_rect.left:
x += screen_rect.width
elif x + segment_width + segment_margin > screen_rect.right:
x -= screen_rect.width
if y < screen_rect.top:
y += screen_rect.height
elif y + segment_height + segment_margin > screen_rect.bottom:
y -= screen_rect.height
segment = Segment(x, y)
snake_segments.insert(0, segment)
allspriteslist.add(segment)
screen.fill(BLACK)
#allspriteslist.draw(screen)
for x in allspriteslist:
x.draw(screen)
pygame.display.flip()
clock.tick(5)
pygame.quit()

Categories

Resources