I am having this problem where I can draw a grid for my snake but I cant fill in the colour between it.It seems like the problem is that because the grid on the surface overrides the actual colour of the background but I dont know how to fix this, can anyone who knows more about pygame help.
See line 74 of my code and that is the colour it should be.
How could I optimise this simple python pygame code
import pygame
import time
import random
pygame.init()
display_width = 960
display_height = 540
display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Snake")
pureblue = (0,0,255)
purered = (255,0,0)
puregreen = (0,255,0)
green = (0,150,0)
white = (255,255,255)
black = (1,1,1)
grey = (75,75,75)
darkgrey = (50,50,50)
clock = pygame.time.Clock()
snake_block = 10
snake_speed = 5
font_style = pygame.font.SysFont(None, 50)
def drawGrid(surf):
blockSize = snake_block
for x in range(display_width):
for y in range(display_height):
rect = pygame.Rect(x*blockSize, y*blockSize,blockSize, blockSize)
pygame.draw.rect(surf, darkgrey, rect, 1)
grid_surf = pygame.Surface(display.get_size())
drawGrid(grid_surf)
def message(msg, colour):
text = font_style.render(msg, True, colour)
display.blit(text, [display_width/2, display_height/2])
def SnakeGameLoop():
game_over = False
X = display_width/2
Y = display_height/2
X_change = 0
Y_change = 0
while not game_over:
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_change = -snake_block
Y_change = 0
elif event.key == pygame.K_RIGHT:
X_change = snake_block
Y_change = 0
elif event.key == pygame.K_UP:
X_change = 0
Y_change = -snake_block
elif event.key == pygame.K_DOWN:
X_change = 0
Y_change = snake_block
if X >= display_width or X < 0 or Y >= display_height or Y < 0:
game_over = True
X += X_change
Y += Y_change
display.fill(grey)
display.blit(grid_surf, (0,0))
pygame.draw.rect(display,puregreen,[X,Y,snake_block,snake_block])
pygame.display.update()
clock.tick(snake_speed)
message("You lost", purered)
pygame.display.update()
time.sleep(2)
pygame.quit()
quit()
SnakeGameLoop()
You need to fill the grid Surface with the background color:
def drawGrid(surf):
surf.fill(grey) # <--- ADD
blockSize = snake_block
for x in range(display_width):
for y in range(display_height):
rect = pygame.Rect(x*blockSize, y*blockSize,blockSize, blockSize)
pygame.draw.rect(surf, darkgrey, rect, 1)
grid_surf = pygame.Surface(display.get_size())
drawGrid(grid_surf)
Since the grid Surface covers the entire display it is not necessary to clear the display in the application loop:
def SnakeGameLoop():
# [...]
while not game_over:
# [...]
# display.fill(grey) <--- DELETE
display.blit(grid_surf, (0,0))
pygame.draw.rect(display,puregreen,[X,Y,snake_block,snake_block])
pygame.display.update()
# [...]
Related
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()
I am unable to detect when two rectangles of different sizes collide.
I've tried, "if x == obj_x and y == obj_y:" where x is one rectangle's x-value, obj_x is the other rectangle's x value, and the same for the y-values.
import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
display_width = 500
display_height = 500
screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Avoid')
x_change = 0
y_change = 0
FPS = 30
block_size = 10
font = pygame.font.SysFont(None, 25)
smallfont = pygame.font.SysFont("comicsansms",25)
def showLives(lives):
text = smallfont.render("Lives: "+str(lives), True, white)
screen.blit(text, [0,0])
def message_to_screen(msg,color):
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [100,250])
clock = pygame.time.Clock()
def gameLoop():
gameExit = False
gameOver = False
x = 250
y = 425
x_change = 0
y_change = 0
obj_speed = 5
obj_y = 0
obj_x = 0
obj2_y = 0
obj2_x = 0
obj2_speed = 3
lives = 3
while not gameExit:
while gameOver == True:
screen.fill(white)
message_to_screen("Game Over, Press C to play again or Q to quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -block_size
y_change = 0
elif event.key == pygame.K_RIGHT:
x_change = block_size
y_change = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
x_change = 0
y_change = 0
if x > 500-block_size:
x-=block_size
if x < 0+block_size:
x+=block_size
# if x >= display_width-block_size-block_size or x < 0:
# gameOver = True
obj_y = obj_y + obj_speed
if obj_y > display_height:
obj_x = random.randrange(0, display_width-block_size)
obj_y = -25
obj2_y = obj2_y + obj2_speed
if obj2_y > display_height:
obj2_x = random.randrange(1, display_width-block_size)
obj2_y = -27
x += x_change
y += y_change
screen.fill(black)
pygame.draw.rect(screen,white, [obj_x,obj_y,20,20])
pygame.draw.rect(screen,white, [obj2_x,obj2_y,20,20])
pygame.draw.rect(screen, red, [x,y,block_size,block_size])
showLives(lives)
pygame.display.update()
if x == obj_x and y == obj_y:
lives -= 1
if lives == 0:
gameOver = True
clock.tick(FPS)
pygame.quit()
quit()
gameLoop()
I want the program to detect when any part of the rectangles collide instead of just detecting when one point on each of the rectangles collide.
PyGame has class pygame.Rect() to keep rectangle's position and size. It uses it to draw images/sprites and check collision between sprites.
x = 250
y = 425
obj_y = 0
obj_x = 0
rect_1 = pygame.Rect(x, y, 10, 10)
rect_2 = pygame.Rect(obj_x, obj_y, 20, 20)
and then you can check collision
rect_1.colliderect(rect_2)
You can also use it to draw rectangle on screen
pygame.draw.rect(screen, white, rect_2)
pygame.draw.rect(screen, red, rect_1)
You can also use it to check collision between rectangle and point - ie. to check if button was clicked by mouse
button_rect.collidepoint(event.pos)
To change values in rectangle you have rect_1.x, rect_1.y, rect_1.width, rect_1.height but also
x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h
Some of them takes tuple with (x, y)
for example: center rectangle on screen
rect_1.center = (display_width//2, display_height//2)
or event using screen
rect_1.center = screen.get_rect().center
OR center text on screen
screen_text_rect = screen_text.get_rect()
screen_text_rect.center = screen.get_rect().center
screen.blit(screen_text, screen_text_rect)
I'm attempting to detect a collision between two objects (both images).
I have rect's for both the objects, but cannot get the collision to be detected.
I've attempted to use rect's, comparing x and y positions.
import sys
import pygame
from pygame.locals import *
#init pygame
pygame.init()
window_width = 840
window_height = 650
size = (window_width, window_height)
screen = pygame.display.set_mode(size)
bg_img = pygame.image.load("img.png").convert_alpha()
crash = False
bot1x = 150
bot1y = 100
x = (window_width * 0.45)
y = (450)
clock = pygame.time.Clock()
fr = clock.tick(30)
x_speed = 0
car_img = pygame.image.load("car.png").convert_alpha()
car_img = pygame.transform.scale(car_img, (125, 175))
bot_img = pygame.image.load("bot.png").convert_alpha()
bot_img = pygame.transform.scale(bot_img, (175, 200))
def car(x, y):
screen.blit(car_img, (x,y))
def bot(x, y):
screen.blit(bot_img, (x,y))
car_rect = pygame.Rect(x, y, 125, 175)
bot_rect = pygame.Rect(bot1x, bot1y, 175, 200)
#game loop
while(crash==False):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == K_LEFT:
x_speed = -.9
print("Moving Left")
if event.key == K_RIGHT:
x_speed = .9
print("Moving Right")
if event.type == pygame.KEYUP:
if event.key == K_LEFT:
x_speed = 0
if event.key == K_RIGHT:
x_speed= 0
if event.type == pygame.QUIT:
crash = True
bot1y += .5
# Move car
x += x_speed
if car_rect.colliderect(bot_rect):
print("collision deceted")
#blit images to screen
screen.blit(bg_img, [0, 0])
#spawn car
car(x,y)
bot(bot1x, bot1y)
#flip game display
pygame.display.flip()
#end game
pygame.quit()
quit()
I expect the game to output "Collision detected" when the two objects touch eachother.
Thanks,
Lachlan
import pygame, time, random
pygame.init()
display_width = 800
display_height = 600
white = (255,255,255)
green = (0,255,0)
black = (0,0,0)
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
boy = pygame.image.load("pig.png")
fence = pygame.image.load("fence.png")
def message_display(text):
largeText = pygame.font.Font("freesansbold.ttf",115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = (display_width/2),(display_height/2)
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def crash():
message_display("Game Over")
def fence1(fencex, fencey, fencew, fenceh, color):
gameDisplay.blit(fence, (fencex, fencey, fencew, fenceh))
def collision():
pig_image = pygame.image.load("pig.png")
pig_rect = pig_image.get_rect()
fence_image = pygame.image.load("fence.png")
fence_rect = fence_image.get_rect()
if pig_rect.colliderect(fence_rect):
crash()
def game_loop():
fence_startx = 900
fence_starty = gameDisplay.get_height() * 0.8
fence_speed = 15
fence_width = 50
fence_height = 50
x = gameDisplay.get_width() * 0.1
y = gameDisplay.get_height() * 0.8
y_change = 0
on_ground = True
gravity = .9
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if on_ground:
y_change = -20
on_ground = False
y_change += gravity
y += y_change
if y >= 500:
y = 500
y_change = 0
on_ground = True
gameDisplay.fill(green)
gameDisplay.blit(boy, (x, y))
fence1(fence_startx, fence_starty, fence_width, fence_height, white)
fence_startx -= fence_speed
randomx = random.randint(1,3)
if fence_startx <= -50:
if randomx == 1:
fence_startx = 1000
if randomx == 2:
fence_startx = 800
if randomx == 3:
fence_startx = 1500
collision()
pygame.display.flip()
clock.tick(60)
game_loop()
pygame.quit()
quit()
For some reason when I run this code, it spams me "Game Over". Could someone please help me on this issue. I use a collision function in this code. Is using the pygame.rect code the best way to do collision in pygame? Everything works except for my collision in the game. I would greatly appreciate some help. Thanks.
In the collision function you create two rects but leave their coordinates at the default position (0, 0) so the rects overlap. You should add rects for the player and the fence in the main game_loop function, set their x and y (or the topleft) attributes to the desired coordinates and then update them every iteration of the while loop. To check if the rects collide, pass them to the collision function and do this:
def collision(boy_rect, fence_rect):
if boy_rect.colliderect(fence_rect):
# I'm just printing it to shorten the code example.
print('crash')
Here's a minimal, complete example:
import random
import pygame
pygame.init()
green = (0,255,0)
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
boy = pygame.Surface((40, 60))
boy.fill((0, 40, 100))
fence = pygame.Surface((50, 50))
fence.fill((100, 60, 30))
def collision(boy_rect, fence_rect):
if boy_rect.colliderect(fence_rect):
# I'm just printing it to shorten the code example.
print('crash')
def game_loop():
fence_speed = 15
# Create a fence and a boy rect and set their x,y or topleft coordinates.
fence_rect = fence.get_rect()
fence_rect.x = 900
fence_rect.y = gameDisplay.get_height() * 0.8
x = gameDisplay.get_width() * 0.1
y = gameDisplay.get_height() * 0.8
boy_rect = boy.get_rect(topleft=(x, y))
y_change = 0
on_ground = True
gravity = .9
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_UP:
if on_ground:
y_change = -20
on_ground = False
y_change += gravity
y += y_change
if y >= 500:
y = 500
y_change = 0
on_ground = True
boy_rect.y = y # Update the rect position.
gameDisplay.fill(green)
gameDisplay.blit(boy, boy_rect)
gameDisplay.blit(fence, fence_rect)
# Update the position of the fence rect.
fence_rect.x -= fence_speed
if fence_rect.x <= -50:
# You can use random.choice to pick one of these values.
fence_rect.x = random.choice((800, 1000, 1500))
# Pass the two rects and check if they collide.
collision(boy_rect, fence_rect)
pygame.display.flip()
clock.tick(60)
game_loop()
pygame.quit()
I decided to try and make the snake-eating dot game on my own. I can't make my pygame window stay open when I try and run what I have. How can I make it stay open?
import pygame
import time
import random
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
white = (255,255,255)
snake_width = 20
snake_height = 20
pygame.init()
game_width = 800
game_height = 600
gameDisplay = pygame.display.set_mode((game_width,game_height))
pygame.display.set_caption('Snake Game!')
clock = pygame.time.Clock()
class game:
def _init_(self):
snake_startx = (game_width/2)
snake_starty = (game_heiht/2)
x = thingx
y = thingy
x_change = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
x_change = -5
elif event.key == pygame.K_UP:
x_change = 5
snake(white, thing_startx, thing_starty, snake_width,
snake_height, x_change)
pygame.display.update()
clock.tick(60)
class snake:
def _init_(self, color, thingx, thingy, thingw, thingh, things):
color = white
thingx = snake_startx
thingy = snake_starty
thingw = snake_width
thingh = snake_height
things = x_change
pygame.draw.rect(gameDisplay, white, [snake_startx,
snake_starty,
snake_width, snake_height,])
game()
pygame.quit()
quit()
Had to rewrite more than half of the code. I wouldn't suggest following what guide you were using. Furthermore if you are very beginner at the python language I wouldn't recommend looking into object orientation just yet. First learn about data types, and loops. You can still make pygame projects without object orientation, but if you know how to use OOP then still use it.
import pygame
import time
import random
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
black = (0,0,0)
white = (255,255,255)
snake_width = 20
snake_height = 20
pygame.init()
game_width = 800
game_height = 600
x_change = 0
y_change = 0
gameDisplay = pygame.display.set_mode((game_width,game_height))
pygame.display.set_caption('Snake Game!')
clock = pygame.time.Clock()
gameDisplay.fill(black)
class snake:
def __init__(self, color, thingw, thingh, movementspeedX, movementspeedY):
self.color = color
self.width = thingw
self.height = thingh
self.Xspeed = movementspeedX
self.Yspeed = movementspeedY
self.snake_startx = (game_width/2)
self.snake_starty = (game_height/2)
self.Xpos = self.snake_startx
self.Ypos = self.snake_starty
def Draw(self):
pygame.draw.rect(gameDisplay, self.color, pygame.Rect([self.Xpos, self.Ypos, self.width, self.height]))
def Update(self):
self.Xpos += self.Xspeed
self.Ypos += self.Yspeed
SnakeObject = snake(white, snake_width, snake_height, x_change, y_change)
SnakeObject.Draw()
gameRunning = True
while gameRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("QUIT")
pygame.quit()
gameRunning = False
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
SnakeObject.Xspeed = -5
SnakeObject.Yspeed = 0
elif event.key == pygame.K_RIGHT:
SnakeObject.Xspeed = 5
SnakeObject.Yspeed = 0
elif event.key == pygame.K_DOWN:
SnakeObject.Yspeed = 5
SnakeObject.Xspeed = 0
elif event.key == pygame.K_UP:
SnakeObject.Yspeed = -5
SnakeObject.Xspeed = 0
print(SnakeObject.Yspeed)
print(SnakeObject.Xspeed)
SnakeObject.Update()
SnakeObject.Draw()
pygame.display.update()
clock.tick(60)