I'm having trouble keeping track of two rectangles that I blit onto a surface after I then rotate that surface.
There are three rectangles draw on to the surface, representing the player. As the left and right keys are pressed, the rectangles all rotate around the centre point correctly.
When the space bar is pressed, the "lights" are supposed to toggle on and off, however they are always redrawn at the bottom of the surface.
Can you advise what I'm doing wrong?
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((self.width, self.height))
self.surf.fill((0, 0, 0))
self.rect = self.surf.get_rect(center = (screen_width / 2, screen_height / 2))
self.rotation = 0
self.PlayerBody = pygame.draw.rect(self.surf, self.colour, Rect(15, 15, 20, 35))
self.PlayerLightLeft = pygame.draw.rect(self.surf, (125, 0, 0), Rect(19, 45, 4, 4))
self.PlayerLightRight = pygame.draw.rect(self.surf, (125, 0, 0), Rect(27, 45, 4, 4))
self.lights = False
self.lightsOnColour = (255, 0, 0)
self.lightsOffColour = (125, 0, 0)
self.lightsColour = self.lightsOffColour
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.surf = pygame.transform.rotate(self.surf, -90)
if pressedKey == pygame.K_LEFT:
self.surf = pygame.transform.rotate(self.surf, 90)
if pressedKey == pygame.K_SPACE:
if self.lights:
self.lightsColour = self.lightsOffColour
self.lights = False
else:
self.lightsColour = self.lightsOnColour
self.lights = True
# always draws rectangles at the bottom of the surface
self.PlayerLightLeft = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightLeft)
self.PlayerLightRight = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightRight)
# initialize pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
Player = Player()
running = True
while running:
pressedKey = pygame.K_0
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pressedKey = event.key
elif event.type == QUIT:
running = False
screen.blit(background, (0, 0))
Player.update(pressedKey)
screen.blit(Player.surf, Player.rect)
pygame.display.flip()
# end of game, quit
pygame.quit()
Don't rotate the player surface when a key is presse, but add an attribute angle to the class Player, which stores the current angle of the surface.
Change the angle when the K_RIGHT or K_LEFT key is pressed.
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
# [...]
self.angle = 0
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.angle = (self.angle - 90) % 360
if pressedKey == pygame.K_LEFT:
self.angle = (self.angle + 90) % 360
# [...]
this causes that the original surface is never changed and changing the "lights" will always work.
Create a rotated surface, which is rotated by Player.angle and blit the rotated surface:
rotSurf = pygame.transform.rotate(Player.surf, Player.angle)
screen.blit(rotSurf, Player.rect)
Brilliant #Rabbid76! Thank you!
Updated code below in case anyone has a similar problem.
import pygame
from pygame.locals import *
class Player(pygame.sprite.Sprite):
height = 48
width = 48
colour = (30,144,255)
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((self.width, self.height))
self.surf.fill((0, 0, 0))
self.rect = self.surf.get_rect(center = (screen_width / 2, screen_height / 2))
self.angle = 0
self.PlayerBody = pygame.draw.rect(self.surf, self.colour, Rect(15, 15, 20, 35))
self.PlayerLightLeft = pygame.draw.rect(self.surf, (125, 0, 0), Rect(19, 45, 4, 4))
self.PlayerLightRight = pygame.draw.rect(self.surf, (125, 0, 0), Rect(27, 45, 4, 4))
self.lights = False
self.lightsOnColour = (255, 0, 0)
self.lightsOffColour = (125, 0, 0)
self.lightsColour = self.lightsOffColour
def update(self, pressedKey):
if pressedKey == pygame.K_RIGHT:
self.angle = (self.angle - 90) % 360
if pressedKey == pygame.K_LEFT:
self.angle = (self.angle + 90) % 360
if pressedKey == pygame.K_SPACE:
if self.lights:
self.lightsColour = self.lightsOffColour
self.lights = False
else:
self.lightsColour = self.lightsOnColour
self.lights = True
self.PlayerLightLeft = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightLeft)
self.PlayerLightRight = pygame.draw.rect(self.surf, self.lightsColour, self.PlayerLightRight)
# initialize pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.Surface(screen.get_size())
background.fill((255, 255, 255))
Player = Player()
running = True
while running:
pressedKey = pygame.K_0
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
pressedKey = event.key
elif event.type == QUIT:
running = False
screen.blit(background, (0, 0))
Player.update(pressedKey)
rotSurf = pygame.transform.rotate(Player.surf, Player.angle)
screen.blit(rotSurf, Player.rect)
pygame.display.flip()
# end of game, quit
pygame.quit()
Related
I am currently just testing the waters with Pygame display as I am extremely new to the module.
Here is the code:
import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0, 0, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
green = (44, 215, 223)
red = (255, 67, 34)
blue = (77, 77, 77)
count = 0
running = True
box1_image = True
box3_image = True
background = pygame.image.load('C:/Users/Justi/source/repos/03 Game/Game/assets/background.jpg')
dirty_spot = pygame.image.load('C:/Users/Justi/source/repos/03 Game/Game/assets/dirty.png')
background_resized = pygame.transform.scale(dirty_spot, (display_width, display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode(background_rect.size)
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot, (200, 200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
def dirtyspot1(imagex, imagey):
gameDisplay.blit(dirty_spot_resized, (imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.quit:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
#if box3_image: #Replacing the Bool passing variable with a simple loop count to trigger the else condition
if count < 50:
print("Test 1")
dirtyspot1(0,0)
pygame.display.update()
else:
print("Test 2")
dirty_rect = background.subsurface(rect_dirty_spot)
gameDisplay.blit(dirty_rect, (0,0))
rects.append(pygame.Rect(0,0, 200, 200))
gameDisplay.fill(white)
clock.tick(30)
count += 1
pygame.quit()
quit()
box1_image & box1_image are bool variables to flag certain condition which is passed from another function.
But I have gotten the passing of said variables working by doing a simple test with print("Test 2"). However, when it tries to blit dirty_rect. Nothing changes on the pygame display.
Assets (if needed):
background.jpg
dirty.png
May I check what is missing to properly "remove/delete" the dirty_spot blit? Thank you in advance.
import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0, 0, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
green = (44, 215, 223)
red = (255, 67, 34)
blue = (77, 77, 77)
count = 0
running = True
box1_image = True
box3_image = True
background = pygame.image.load('background.jpg')
dirty_spot = pygame.image.load('dirty.png')
background_resized = pygame.transform.scale(background, (display_width, display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot, (200, 200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
show_dirt = False
def dirtyspot1(imagex, imagey):
gameDisplay.blit(dirty_spot_resized, (imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
if event.key == pygame.K_d:
show_dirt = not show_dirt
if event.key == pygame.K_UP:
rect_dirty_spot.y -= 5
if event.key == pygame.K_DOWN:
rect_dirty_spot.y += 5
if event.key == pygame.K_RIGHT:
rect_dirty_spot.x += 5
if event.key == pygame.K_LEFT:
rect_dirty_spot.x -= 5
gameDisplay.blit(background_resized, (0,0))
if show_dirt:
gameDisplay.blit(dirty_spot_resized, rect_dirty_spot)
pygame.display.update()
clock.tick(5)
count += 1
pygame.quit()
quit()
like this? (I changed the FPS to 5 so it takes a while to change)
import pygame
pygame.init()
SCALE = 1
display_width = int(1200 * SCALE)
display_height = int(800 * SCALE)
x = (display_width * 0.45)
y = (display_height * 0.8)
transparent = (0, 0, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)
green = (44, 215, 223)
red = (255, 67, 34)
blue = (77, 77, 77)
count = 0
running = True
box1_image = True
box3_image = True
background = pygame.image.load('background.jpg')
dirty_spot = pygame.image.load('dirty.png')
background_resized = pygame.transform.scale(background, (display_width, display_height))
background_rect = background_resized.get_rect()
gameDisplay = pygame.display.set_mode(background_rect.size)
pygame.display.set_caption("Game 1")
clock = pygame.time.Clock()
dirty_spot_resized = pygame.transform.scale(dirty_spot, (200, 200))
square_dirty_spot = dirty_spot_resized.convert()
rect_dirty_spot = square_dirty_spot.get_rect()
def dirtyspot1(imagex, imagey):
gameDisplay.blit(dirty_spot_resized, (imagex,imagey))
while running:
rects = []
if rects != []:
pygame.display.update(rects)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
running = False
gameDisplay.fill(white)
#if box3_image: #Replacing the Bool passing variable with a simple loop count to trigger the else condition
if count < 50:
print("Test 1")
dirtyspot1(0,0)
else:
print("Test 2")
dirty_rect = background.subsurface(rect_dirty_spot)
gameDisplay.blit(dirty_rect, (0,0))
rects.append(pygame.Rect(0,0, 200, 200))
pygame.display.update()
clock.tick(5)
count += 1
pygame.quit()
quit()
I think you have misunderstood things. You can blit one image or surface on top of another, and you build things up like that, then blit them to the display and finally update the display.
You have this line:
background_resized = pygame.transform.scale(dirty_spot, (display_width, display_height))
which I assume should be background not dirty_spot.
I moved the call to display.update() out of the if loop, because you call display.update() last.
I am trying to make a game where there are a bunch of falling circles and the player(the square) needs to avoid them. I have managed to get the controls of the player and all of the falling circles but when I press play all I see are the falling circles and no square. The only time the square shows is when I press the arrow keys. How can I make the square and circles show up at the same time?
import pygame
from pygame.locals import *
import os
import random
import math
import sys
import time
white = (255,255,255)
blue = (0,0,255)
gravity = 10
size =10
height = 500
width =600
varHeigth = height
ballNum = 5
eBall = []
apGame = pygame.display.set_mode((width, height))
pygame.display.set_caption("AP Project")
clock = pygame.time.Clock()
class Player(object):
def __init__(self):
red = (255, 0, 0)
move_x = 300
move_y = 400
self.rect = pygame.draw.rect(apGame,red, (move_x, move_y, 10, 10))
self.dist = 10
def handle_keys(self):
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit();
exit()
elif e.type == pygame.KEYDOWN:
key = e.key
if key == pygame.K_LEFT:
self.draw_rect(-1, 0)
elif key == pygame.K_RIGHT:
self.draw_rect(1, 0)
elif key == pygame.K_ESCAPE:
pygame.quit();
exit()
def draw_rect(self, x, y):
red = (255, 0, 0)
black = (0, 0, 0)
'''apGame.fill(black)'''
self.rect = self.rect.move(x * self.dist, y * self.dist);
pygame.draw.rect(apGame, red , self.rect)
pygame.display.update()
def draw(self,surface):
red = (255, 0, 0)
move_x = 300
move_y = 400
pygame.draw.rect(apGame, red, (move_x, move_y, 10, 10))
move_x = 300
move_y = 400
red = (255, 0, 0)
black = (0, 0, 0)
player = Player()
clock = pygame.time.Clock()
'''apGame.fill(black)'''
player.draw(apGame)
pygame.display.update()
for q in range(ballNum):
x = random.randrange(0, width)
y = random.randrange(0, varHeigth)
eBall.append([x, y])
while True:
#I think this is where my problem is
apGame.fill(black)
player.handle_keys()
for i in range(len(eBall)):
pygame.draw.circle(apGame, blue, eBall[i], size)
eBall[i][1] += 5
if eBall[i][1] > height:
y = random.randrange(-50, -10)
eBall[i][1] = y
x = random.randrange(0, width)
eBall[i][0] = x
pygame.display.flip()
clock.tick(30)
If you insert a line :
player.draw_rect (0, 0)
just above
pygame.display.flip ()
then your square will show up.
I would recommend using :
pygame.key.get_pressed ()
instead of :
pygame.KEYDOWN
so that your player will keep moving as long as the key is being held down instead of having to press and release the left or right keys over and over.
Edit :
In response to OPs question in comments, here is working code :
import pygame
from pygame.locals import *
import random
white = (255,255,255)
blue = (0,0,255)
gravity = 10
size =10
height = 500
width =600
varHeigth = height
ballNum = 5
eBall = []
apGame = pygame.display.set_mode((width, height))
pygame.display.set_caption("AP Project")
clock = pygame.time.Clock()
class Player(object):
def __init__(self):
red = (255, 0, 0)
move_x = 300
move_y = 400
self.rect = pygame.draw.rect(apGame,red, (move_x, move_y, 10, 10))
self.dist = 10
def handle_keys(self):
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit();
exit()
key = pygame.key.get_pressed ()
if key [pygame.K_LEFT]:
self.draw_rect(-1, 0)
elif key [pygame.K_RIGHT]:
self.draw_rect(1, 0)
elif key [pygame.K_ESCAPE]:
pygame.quit();
exit()
else :
self.draw_rect (0, 0)
def draw_rect(self, x, y):
red = (255, 0, 0)
black = (0, 0, 0)
self.rect = self.rect.move(x * self.dist, y * self.dist);
pygame.draw.rect(apGame, red , self.rect)
def draw(self,surface):
red = (255, 0, 0)
move_x = 300
move_y = 400
pygame.draw.rect(apGame, red, (move_x, move_y, 10, 10))
move_x = 300
move_y = 400
red = (255, 0, 0)
black = (0, 0, 0)
player = Player()
clock = pygame.time.Clock()
'''apGame.fill(black)'''
player.draw(apGame)
pygame.display.update()
for q in range(ballNum):
x = random.randrange(0, width)
y = random.randrange(0, varHeigth)
eBall.append([x, y])
while True:
apGame.fill(black)
for i in range(len(eBall)):
pygame.draw.circle(apGame, blue, eBall[i], size)
eBall[i][1] += 5
if eBall[i][1] > height:
y = random.randrange(-50, -10)
eBall[i][1] = y
x = random.randrange(0, width)
eBall[i][0] = x
player.handle_keys ()
pygame.display.flip()
clock.tick(30)
I want to be able to drag the blue object along the x-axis (black line) using mouse so that it does not move in y-direction. When I try to drag it, nothing happens. Where is the problem?
import pygame
def initialize():
pygame.init()
global height, width
height = 600
width = 900
screen = pygame.display.set_mode((width, height))
screen.fill((255, 255, 255))
pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3)
return screen
def object():
dragging = False
object_1 = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150)
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if object_1.collidepoint(event.pos):
dragging = True
mouse_x, mouse_y = event.pos
offset_x = object_1.x - mouse_x
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
dragging = False
elif event.type == pygame.MOUSEMOTION:
if dragging:
mouse_x, mouse_y = event.pos
object_1.x = mouse_x + offset_x
return object_1
if __name__ == "__main__":
running = True
screen = initialize()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
object_1 = object()
pygame.draw.rect(screen, (0, 0, 250), object_1)
pygame.display.update()
You have to create the object once before the main application loop and you have to handle the events in the application loop.
Furthermore you have to redraw the entire scene in the application loop. The main application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
Add a function which creates an object:
def create_object():
object_1 = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150)
return object_1
Create an object before the application loop:
if __name__ == "__main__":
# [...]
object_1 = create_object()
while running:
# [...]
Add a function which can drag an object:
dragging = False
def drag_object(events, object_1):
global dragging, offset_x
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if object_1.collidepoint(event.pos):
dragging = True
mouse_x, mouse_y = event.pos
offset_x = object_1.x - mouse_x
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
dragging = False
elif event.type == pygame.MOUSEMOTION:
if dragging:
mouse_x, mouse_y = event.pos
object_1.x = mouse_x + offset_x
Get the list of events once in the application loop and pass the events to the function drag_object:
while running:
# [...]
drag_object(events, object_1)
Clear the display, draw the scene and update the display in the application loop:
while running:
# [...]
screen.fill((255, 255, 255))
pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3)
pygame.draw.rect(screen, (0, 0, 250), object_1)
pygame.display.update()
See the example:
import pygame
def initialize():
pygame.init()
global height, width
height = 600
width = 900
screen = pygame.display.set_mode((width, height))
return screen
def create_object():
object_1 = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150)
return object_1
dragging = False
def drag_object(events, object_1):
global dragging, offset_x
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if object_1.collidepoint(event.pos):
dragging = True
mouse_x, mouse_y = event.pos
offset_x = object_1.x - mouse_x
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
dragging = False
elif event.type == pygame.MOUSEMOTION:
if dragging:
mouse_x, mouse_y = event.pos
object_1.x = mouse_x + offset_x
if __name__ == "__main__":
running = True
screen = initialize()
object_1 = create_object()
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
drag_object(events, object_1)
screen.fill((255, 255, 255))
pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3)
pygame.draw.rect(screen, (0, 0, 250), object_1)
pygame.display.update()
Alternatively you can create a class for the object:
import pygame
def initialize():
pygame.init()
global height, width
height = 600
width = 900
screen = pygame.display.set_mode((width, height))
return screen
class MyObject:
def __init__(self):
self.rect = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150)
self.dragging = False
self.offset_x = 0
def drag(self, events):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.rect.collidepoint(event.pos):
self.dragging = True
self.offset_x = self.rect.x - event.pos[0]
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.dragging = False
elif event.type == pygame.MOUSEMOTION:
if self.dragging:
self.rect.x = event.pos[0] + self.offset_x
def draw(self, surf):
pygame.draw.rect(surf, (0, 0, 250), object_1)
if __name__ == "__main__":
running = True
screen = initialize()
object_1 = MyObject()
while running:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
object_1.drag(events)
screen.fill((255, 255, 255))
pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3)
object_1.draw(screen)
pygame.display.update()
So, I am programming a game which is two-players. I am trying to make it split screen from the center (vertically) where each player has their own screen and their own game is working. This would allow them to be at different stages in the game. For example, if one gets out, the other is not affected.
My game is where a snake has 5 balls initially of different colours and it is automatically moving in an upward direction. The user has to move it right or left to collect the correct colour ball and then collide with the correct colour block. I want this happening in the same screen but two times which will make it a two player game.
First, you have to encapsulte the game state for each sub-game. Then, instead of drawing the scene of the game(s) directly to the screen, draw them to seperate Surfaces, and then draw those to Surfaces to the screen.
Here's a simple, running example:
import pygame
import pygame.freetype
import random
pygame.init()
FONT = pygame.freetype.SysFont(None, 32)
class Player(pygame.sprite.Sprite):
def __init__(self, color, canvas, bindings):
super().__init__()
self.image = pygame.Surface((32, 32))
self.image.fill(color)
self.rect = self.image.get_rect(center=canvas.get_rect().center)
self.canvas = canvas
self.bindings = bindings
self.pos = pygame.Vector2(self.rect.topleft)
def update(self):
pressed = pygame.key.get_pressed()
direction = pygame.Vector2()
for key in self.bindings:
if pressed[key]:
direction += self.bindings[key]
if direction.length() > 0:
direction.normalize_ip()
self.pos += direction*4
self.rect.topleft = self.pos
self.rect.clamp_ip(self.canvas.get_rect())
self.pos = self.rect.topleft
class Game(pygame.sprite.Sprite):
def __init__(self, color, bg_color, bindings, left):
super().__init__()
self.bg_color = bg_color
self.image = pygame.Surface((400, 600))
self.image.fill(self.bg_color)
self.rect = self.image.get_rect(left=0 if left else 400)
self.player = Player(color, self.image, bindings)
self.target = pygame.Vector2(random.randint(100, 300), random.randint(100, 500))
self.stuff = pygame.sprite.Group(self.player)
self.score = 0
def update(self):
self.stuff.update()
self.image.fill(self.bg_color)
self.stuff.draw(self.image)
FONT.render_to(self.image, (190, 50), str(self.score), (200, 200, 200))
pygame.draw.circle(self.image, (210, 210, 210), [int(v) for v in self.target], 5)
if (self.player.rect.center - self.target).length() <= 20:
self.score += 1
self.target = pygame.Vector2(random.randint(100, 300), random.randint(100, 500))
def main():
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
player1_bindings = {
pygame.K_w: pygame.Vector2(0, -1),
pygame.K_a: pygame.Vector2(-1, 0),
pygame.K_s: pygame.Vector2(0, 1),
pygame.K_d: pygame.Vector2(1, 0)
}
player2_bindings = {
pygame.K_UP: pygame.Vector2(0, -1),
pygame.K_LEFT: pygame.Vector2(-1, 0),
pygame.K_DOWN: pygame.Vector2(0, 1),
pygame.K_RIGHT: pygame.Vector2(1, 0)
}
player1 = Game(pygame.Color('dodgerblue'), (30, 30, 30), player1_bindings, True)
player2 = Game(pygame.Color('orange'), (80, 20, 30), player2_bindings, False)
games = pygame.sprite.Group(player1, player2)
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
return
games.update()
screen.fill((30, 30, 30))
games.draw(screen)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
I am attempting to create a racing game in pygame. I want it such that when the car goes off the track, it slows down. I have tried to do this by having another sprite that is an outline of the track and when the car touches that sprite, it slows down. This does not work and I don't know why. Is there a better way to do this?
Img is the car image
Back is the racetrack
BackHit is the outline
I receive this error code:
Traceback (most recent call last):
File "C:\Users\Daniella\Desktop\Python\Games\game.py", line 75, in
if pygame.sprite.collide_mask(Img, BackHit):
File "C:\Users\Daniella\AppData\Roaming\Python\Python36\site-packages\pygame\sprite.py", line 1470, in collide_mask
xoffset = right.rect[0] - left.rect[0]
AttributeError: 'pygame.Surface' object has no attribute 'rect'
This is the code:
import pygame
Width = 800
Height = 600
Black = (0, 0, 0)
White = (255, 255, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
Blue = (0, 0, 255)
Yellow = (255, 255, 0)
BackColour = (198, 151, 107)
pygame.init()
GameDisplay = pygame.display.set_mode((Width, Height))
pygame.display.set_caption("A bit Racey")
Clock = pygame.time.Clock()
Img = pygame.image.load("download.png")
ImgWidth = 46
ImgHeight = 68
Img = pygame.transform.scale(Img, (ImgWidth, ImgHeight))
Back = pygame.image.load("back1.png")
BackWidth = Width*4
BackHeight = Height*4
Back = pygame.transform.scale(Back, (BackWidth, BackHeight))
BackHit = pygame.image.load("back1 hit1.png")
BackHitWidth = Width*4
BackHitHeight = Height*4
BackHit = pygame.transform.scale(BackHit, (BackHitWidth, BackHitHeight))
def Car():
GameDisplay.blit(Img, (400-ImgWidth/2, 300-ImgHeight/2))
def Background(X, Y):
GameDisplay.blit(Back, (X, Y))
def BackgroundHit(X, Y):
GameDisplay.blit(BackHit, (X, Y))
X = (Width*0.45)
Y = (Height*0.5)
XChange = 0
YChange = 0
Changer = 1
Crashed = False
while not Crashed:
for Event in pygame.event.get():
if Event.type == pygame.QUIT:
Crashed = True
elif Event.type == pygame.KEYDOWN:
if Event.key == pygame.K_LEFT:
Img = pygame.transform.rotate(Img, -90)
XChange = 5 / Changer
elif Event.key == pygame.K_RIGHT:
Img = pygame.transform.rotate(Img, 90)
XChange = -5 / Changer
elif Event.key == pygame.K_UP:
Img = pygame.transform.rotate(Img, 0)
YChange = 5 / Changer
elif Event.key == pygame.K_DOWN:
Img = pygame.transform.rotate(Img, 180)
YChange = -5 / Changer
if Event.type == pygame.KEYUP:
if Event.key == pygame.K_LEFT or Event.key == pygame.K_RIGHT:
XChange = 0
elif Event.key == pygame.K_UP or Event.key == pygame.K_DOWN:
YChange = 0
if pygame.sprite.collide_mask(Img, BackHit):
Changer = 2
Y += YChange
X += XChange
GameDisplay.fill(White)
BackgroundHit(X, Y)
Background(X, Y)
Car()
pygame.display.update()
Clock.tick(200)
pygame.quit()
quit()
Here's a little example that shows you how you can use pygame.mask.from_surface and pygame.Mask.overlap for pixel perfect collision detection.
import pygame as pg
# Transparent surfaces with a circle and a triangle.
circle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.circle(circle_surface, (30, 90, 200), (30, 30), 30)
triangle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.polygon(triangle_surface, (160, 250, 0), ((30, 0), (60, 60), (0, 60)))
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# Use `pygame.mask.from_surface` to get the masks.
circle_mask = pg.mask.from_surface(circle_surface)
triangle_mask = pg.mask.from_surface(triangle_surface)
# Also create rects for the two images/surfaces.
circle_rect = circle_surface.get_rect(center=(320, 240))
triangle_rect = triangle_surface.get_rect(center=(0, 0))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEMOTION:
triangle_rect.center = event.pos
# Now calculate the offset between the rects.
offset_x = triangle_rect.x - circle_rect.x
offset_y = triangle_rect.y - circle_rect.y
# And pass the offset to the `overlap` method of the mask.
overlap = circle_mask.overlap(triangle_mask, (offset_x, offset_y))
if overlap:
print('The two masks overlap!', overlap)
screen.fill((30, 30, 30))
screen.blit(circle_surface, circle_rect)
screen.blit(triangle_surface, triangle_rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
To create a mask for the background or track you need to create an extra image and either leave the track transparent or the area where the car should slow down and then check if the car collides with the track or with the outside area. Here I check if the green triangle collides with the "track" (the blue lines), and in your game you would then slow the car down if it doesn't collide with the track.
import pygame as pg
bg_surface = pg.Surface((640, 480), pg.SRCALPHA)
pg.draw.lines(
bg_surface, (30, 90, 200), True,
((60, 130), (300, 50), (600, 200), (400, 400), (150, 300)),
12)
triangle_surface = pg.Surface((60, 60), pg.SRCALPHA)
pg.draw.polygon(triangle_surface, (160, 250, 0), ((30, 0), (60, 60), (0, 60)))
def main():
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
bg_mask = pg.mask.from_surface(bg_surface)
triangle_mask = pg.mask.from_surface(triangle_surface)
bg_rect = bg_surface.get_rect(center=(320, 240))
triangle_rect = triangle_surface.get_rect(center=(0, 0))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.MOUSEMOTION:
triangle_rect.center = event.pos
offset_x = triangle_rect.x - bg_rect.x
offset_y = triangle_rect.y - bg_rect.y
overlap = bg_mask.overlap(triangle_mask, (offset_x, offset_y))
if overlap:
print('The two masks overlap!', overlap)
screen.fill((30, 30, 30))
screen.blit(bg_surface, bg_rect)
screen.blit(triangle_surface, triangle_rect)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
pygame.sprite.collide_mask is meant for pygame.sprite.Sprite objects. Instead use mask_var.overlap(other_mask, offset). To calculate the offset between the two masks, use offset = [other_object_x - object_x, other_object_y - object_y]. And to get the mask of an image, use mask_var = pygame.mask.from_surface(image).