Here is my code
import pygame as pg
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
WIDTH = 1920
HEIGHT = 1080
pg.init()
FPS = 50
fpsClock = pg.time.Clock()
screen = pg.display.set_mode((WIDTH, HEIGHT), 0, 32)
ball1Img = pg.image.load('test/sferoid.png')
x1 = y1 = 200 # координаты
xv1 = yv1 = 4 # скорость
check = parity = False
class Ball(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.image.load('test/sferoid.png')
self.rect = self.image.get_rect(centerx=200, bottom=200)
def update(self) -> None:
global xv1
global yv1
xv1 = -xv1 if (self.rect.x > 1920 - 49 or self.rect.x < 0) else xv1 # отскок
yv1 = -yv1 if (self.rect.y > 1080 - 49 or self.rect.y < 0) else yv1 # от стенок
self.rect.x += xv1
self.rect.y += yv1
class Basket(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
# self.image = pg.image.load('test/desk.png')\
self.image = pg.image.load('test/desk.png')
self.image = pg.transform.scale(self.image, (100, 25))
self.rect = self.image.get_rect(centerx=WIDTH // 2, bottom=HEIGHT - 50)
def update(self) -> None:
klavisha = pg.key.get_pressed()
if klavisha[pg.K_a] and self.rect.x >= 0:
self.rect.x -= 20
if klavisha[pg.K_d] and self.rect.right <= WIDTH:
self.rect.x += 20
basket = Basket()
ball = Ball()
while True:
fpsClock.tick(FPS)
screen.fill(BLACK)
basket.update()
ball.update()
screen.blit(basket.image, basket.rect)
screen.blit(ball.image, ball.rect)
pg.display.update()
if pg.sprite.spritecollide(basket, ball, dokill=False):
xv1 = -xv1
yv1 = -yv1
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
I apologize for such a crooked code, but please understand and forgive.
sferoid
desk
When I try to run the program, I get a TypeError: 'Ball' object is not iterable at the line:
if pg.sprite.spritecollide(basket, ball, dokill=False):
xv1 = -xv1
yv1 = -yv1
But if you remove this line, then the code will work.
I don't get why is is doing this but if someone can help me that would be greatly appreciated.
pypame.sprite.spritecollide is used to detect the collision between a pygame.sprite.Sprite and the sprites in a pygame.sprite.Group. However, basket and ball are both pygame.sprite.Sprite objects. You have to use pygame.sprite.collide_rect:
if pg.sprite.spritecollide(basket, ball, dokill=False):
if pygame.sprite.collide_rect(basket, ball):
Related
This question already has answers here:
How do I detect collision in pygame?
(5 answers)
Closed 1 year ago.
My problem: I am trying to create a vertical ball drop game, and I am testing for collision, which does work. But how would I reset my ball when it hits the hoop? Instead of it detecting and then hitting the bottom of the screen which results in a Game over screen because you lose. Any help is appreciated.
import time, random
from pygame.locals import *
import pygame, sys
pygame.init()
FPS = 60
FramePerSec = pygame.time.Clock()
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
SCREEN_BOTTOM = 0
SPEED = 5
SCORE = 0
font = pygame.font.SysFont("Verdana", 60)
font_small = pygame.font.SysFont("Verdana", 20)
game_over = font.render("Game Over", True, BLACK)
background = pygame.image.load("background.jpg")
DISPLAYSURF = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
DISPLAYSURF.fill(WHITE)
pygame.display.set_caption("Ball Drop")
class Ball(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Ball.png")
self.rect = self.image.get_rect()
self.rect.center = (random.randint(40, SCREEN_WIDTH - 40), 0)
def move(self):
global SCORE
self.rect.move_ip(0, SPEED)
if (self.rect.bottom > 600):
self.rect.top = 0
self.rect.center = (random.randint(30, 380), 0)
# Need to check PNG for hit detection
class Basket(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("Basket.png")
self.rect = self.image.get_rect()
self.rect.center = (160, 520)
def move(self):
pressed_keys = pygame.key.get_pressed()
if(self.rect.x >= (SCREEN_WIDTH - 145)):
self.rect.x -= 5;
elif(self.rect.x <= -5):
self.rect.x += 5;
else:
if pressed_keys[pygame.K_a]:
self.rect.move_ip(-SPEED, 0)
if pressed_keys[pygame.K_d]:
self.rect.move_ip(SPEED, 0)
class Wall(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("wall.png")
self.rect = self.image.get_rect()
self.rect.center = (0, 670)
B2 = Basket()
B1 = Ball()
W1 = Wall()
balls = pygame.sprite.Group()
balls.add(B1)
# Need to fix wall sprite group
walls = pygame.sprite.Group()
walls.add(W1)
all_sprites = pygame.sprite.Group()
all_sprites.add(B2)
all_sprites.add(B1)
INC_SPEED = pygame.USEREVENT + 1
pygame.time.set_timer(INC_SPEED, 1000)
while True:
for event in pygame.event.get():
if event.type == INC_SPEED:
SPEED += 0.3
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAYSURF.blit(background, (0, 0))
scores = font_small.render(str(SCORE), True, BLACK)
DISPLAYSURF.blit(scores, (10, 10))
for entity in all_sprites:
DISPLAYSURF.blit(entity.image, entity.rect)
entity.move()
# NEed to fix collison and Counting stats
if pygame.sprite.spritecollideany(W1, balls):
DISPLAYSURF.fill(RED)
DISPLAYSURF.blit(game_over, (30, 250))
pygame.display.update()
for entity in all_sprites:
entity.kill()
time.sleep(2)
pygame.quit()
sys.exit()
if pygame.sprite.spritecollideany(B2, balls):
print("Hit")
SCORE += 1
pygame.display.update()
pygame.display.update()
FramePerSec.tick(FPS)
pygame.sprite.spritecollideany() returns the hit Sprite (ball) object. Change the position of this ball:
while True:
# [...]
ball_hit = pygame.sprite.spritecollideany(B2, balls)
if ball_hit:
ball_hit.rect.center = (random.randint(30, 380), 0)
SCORE += 1
print("Hit")
# [...]
I'd like to add clouds in my game to my game was more realistic, but I don't know, how to realize right. What's wrong?
from pygame.locals import *
import pygame
import os
import random
WIDTH = 1200
HEIGHT = 700
FPS = 60
usr_y = HEIGHT - 120
usr_x = WIDTH - 1120
BLUE = (0, 255, 255)
GREEN = (34, 89, 76)
NOTGREEN = (0, 128, 128)
WHITE = (255, 255, 255)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mini-games")
clock = pygame.time.Clock()
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
player_img = pygame.image.load('Sonic.actionp1.png').convert()
pygame.mixer.music.load('Фоновая музыка.mp3')
pygame.mixer.music.play(-1)
clouds_jpg = pygame.image.load('Clouds.jpg').convert()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = player_img
self.image.set_colorkey(NOTGREEN)
self.rect = self.image.get_rect()
self.rect.centerx = usr_x
self.rect.centery = usr_y
self.y = self.rect.y
def update(self):
self.rect.y = round(self.y)
self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -8
if keystate[pygame.K_RIGHT]:
self.speedx = 8
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
class Clouds(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = clouds_jpg
self.image.set_colorkey(WHITE)
self.rect = self.image.get_rect()
self.rect.centerx = random.randint(0, WIDTH)
self.rect.centery = random.randint(HEIGHT - 550, HEIGHT)
def update(self):
self.rect.x(-5, 0)
ADDCLOUD = pygame.USEREVENT + 1
pygame.time.set_timer(ADDCLOUD, 1000)
all_sprites = pygame.sprite.Group()
player = Player()
clouds = Clouds()
all_sprites.add(player, clouds)
jump = False
counter = -20
def make():
global usr_y, counter, jump
if counter >= -20:
player.y -= counter
counter -= 1
else:
counter = 20
jump = False
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.key == K_SPACE:
jump = True
elif event.type == pygame.QUIT:
running = False
if jump:
make()
all_sprites.update()
screen.fill(BLUE)
all_sprites.draw(screen)
pygame.display.flip()
clouds.update()
pygame.quit()
The instruction self.rect.x(-5, 0) doesn't make any sense. You can move a sorite respectively rectangle with the pygame.Rect.move_ip instruction:
self.rect.x(-5, 0)
self.rect.move_ip(-5, 0)
Change the clouds position to the right as it goes out of the Window:
class Clouds(pygame.sprite.Sprite):
# [...]
def update(self):
self.rect.move_ip(-5, 0)
if self.rect.right <= 0:
self.rect.left = screen.get_width()
Hey I have a problem with my game code. Is there a way to check if, for example, the right side of my player sprite (hero) is colliding with the group obstacle_sprite?
I can only figure out how to check for collision in general but I want only a true output if the right side of the player is colliding wit the obstacle.
In my code I am using a invisible sprite as hitbox and draw the player inside this invisible sprite to check collisions but make it look like the obstacle denies moving of the player.
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PINK = (230, 77, 149)
FPS = 60
window_width = 900
window_height = 600
window_color = WHITE
window = pygame.display.set_mode((window_width, window_height))
window.fill(window_color)
pygame.display.set_caption("Bub - the game")
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, velocity, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width + 2*velocity, height + 2*velocity))
self.rect = self.image.get_rect()
self.rect.topleft = (x - velocity, y - velocity)
self.velocity = velocity
self.is_jump = False
self.jump_count = 15
self.fall_count = 1
self.ground = self.rect.bottom
self.body = pygame.Rect(x, y, width, height)
pygame.draw.rect(window, color, self.body)
def move(self):
key = pygame.key.get_pressed()
if key[pygame.K_a] and self.rect.left >= 0:
self.rect.x -= self.velocity
if key[pygame.K_d] and self.rect.right <= window_width:
self.rect.x += self.velocity
if not self.is_jump:
if key[pygame.K_SPACE] and self.rect.bottom == self.ground:
self.is_jump = True
else:
if self.jump_count >= 0:
self.rect.y -= round((self.jump_count ** 2) * 0.1)
self.jump_count -= 1
else:
self.jump_count = 15
self.is_jump = False
def gravity(self):
if not self.is_jump and self.rect.bottom != self.ground:
self.rect.y += round((self.fall_count ** 2) * 0.1)
if self.fall_count <= 15:
self.fall_count += 1
else:
self.fall_count = 1
def update(self):
self.move()
self.gravity()
pygame.draw.rect(window, BLACK, self.body)
self.body.topleft = (self.rect.x + self.velocity, self.rect.y + self.velocity)
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.center = (x, y)
player_sprite = pygame.sprite.Group()
obstacle_sprite = pygame.sprite.Group()
hero = Player(0, 570, 30, 30, 5, BLACK)
player_sprite.add(hero)
obstacle_1 = Obstacle(100, 585, 120, 30, PINK)
obstacle_sprite.add(obstacle_1)
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(window_color)
player_sprite.update()
player_sprite.draw(window)
obstacle_sprite.update()
obstacle_sprite.draw(window)
pygame.display.update()
pygame.quit()
It's easier if you keep track of your player's velocity; this will make handling jumping also less complex.
A common technique AFAIK is to do two collision detection runs: one for the x-axis and one for the y-axis.
Here's how I changed your code. As you can see, checking for the side of a collision is as easy as checking xvel < 0 or xvel > 0:
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
PINK = (230, 77, 149)
FPS = 60
window_width = 900
window_height = 600
window_color = WHITE
window = pygame.display.set_mode((window_width, window_height))
window.fill(window_color)
pygame.display.set_caption("Bub - the game")
clock = pygame.time.Clock()
class Toast(pygame.sprite.Sprite):
def __init__(self, *grps):
super().__init__(*grps)
self.image = pygame.Surface((900, 200))
self.image.fill(WHITE)
self.rect = self.image.get_rect(topleft=(10, 10))
self.font = pygame.font.SysFont(None, 32)
def shout(self, message):
self.text = self.font.render(message, True, BLACK)
self.image.fill(WHITE)
self.image.blit(self.text, (0, 0))
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, velocity, color, *grps):
super().__init__(*grps)
self.image = pygame.Surface((width + 2*velocity, height + 2*velocity))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.topleft = (x - velocity, y - velocity)
self.velocity = velocity
self.on_ground = True
self.xvel = 0
self.yvel = 0
def collide(self, xvel, yvel, obstacle_sprites):
global toast
for p in pygame.sprite.spritecollide(self, obstacle_sprites, False):
if xvel > 0:
self.rect.right = p.rect.left
toast.shout('collide RIGHT')
if xvel < 0:
self.rect.left = p.rect.right
toast.shout('collide LEFT')
if yvel > 0:
self.rect.bottom = p.rect.top
self.on_ground = True
self.yvel = 0
toast.shout('collide BOTTOM')
if yvel < 0:
self.rect.top = p.rect.bottom
self.yvel = 0
toast.shout('collide TOP')
def update(self, obstacle_sprites):
global toast
key = pygame.key.get_pressed()
self.xvel = 0
if key[pygame.K_a]:
self.xvel =- self.velocity
if key[pygame.K_d]:
self.xvel = self.velocity
if self.on_ground:
if key[pygame.K_SPACE]:
toast.shout('JUMPING')
self.on_ground = False
self.yvel = -20
else:
self.yvel += 1
self.rect.left += self.xvel
self.collide(self.xvel, 0, obstacle_sprites)
self.rect.top += self.yvel
self.on_ground = False
self.collide(0, self.yvel, obstacle_sprites)
self.rect.clamp_ip(pygame.display.get_surface().get_rect())
if self.rect.bottom == pygame.display.get_surface().get_rect().bottom:
self.on_ground = True
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color, *grps):
super().__init__(*grps)
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect(center=(x, y))
all_sprites = pygame.sprite.Group()
player_sprite = pygame.sprite.Group()
obstacle_sprites = pygame.sprite.Group()
toast = Toast(all_sprites)
hero = Player(0, 570, 30, 30, 5, GREEN, all_sprites, player_sprite)
for x in [
(100, 585, 120, 30),
(300, 535, 120, 30),
(500, 485, 120, 30)
]:
Obstacle(*x, PINK, all_sprites, obstacle_sprites)
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(window_color)
all_sprites.update(obstacle_sprites)
all_sprites.draw(window)
pygame.display.update()
pygame.quit()
I've completed a small game project of the breakout game, but my main problem is that the ball seems to get stuck going side to side much more often than i would be okay with it happening. It's definitely a problem with the ball velocity and I've tried messing around with the numbers but I can't seem to make it stop getting stuck. Any ideas on how to fix it ,or even improve any of the code, would be greatly appreciated.
import sys
import pygame as pg
from random import randint
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
score = 0
lives = 3
def run_game():
pg.init()
screen = pg.display.set_mode((800,600))
pg.display.set_caption("Brick Breaker")
bg_color = (0, 0, 0)
screen.fill(bg_color)
class Paddle(pg.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pg.Surface([width, height])
self.image.fill(black)
self.image.set_colorkey(black)
pg.draw.rect(self.image, color, [0, 0, width, height])
self.rect = self.image.get_rect()
def moveLeft(self,pixels):
self.rect.x -= pixels
if self.rect.x < 0:
self.rect.x = 0
def moveRight(self,pixels):
self.rect.x += pixels
if self.rect.x > 700:
self.rect.x = 700
class Ball(pg.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pg.Surface([width, height])
self.image.fill(black)
self.image.set_colorkey(black)
pg.draw.rect(self.image, color, [0, 0, width, height])
self.velocity = [randint(4,8),randint(-8,8)]
self.rect = self.image.get_rect()
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
def bounce(self):
self.velocity[0] = -self.velocity[0]
self.velocity[1] = randint(-8,10)
class Brick(pg.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pg.Surface([width, height])
self.image.fill(black)
self.image.set_colorkey(black)
pg.draw.rect(self.image, color, [0, 0, width, height])
self.rect = self.image.get_rect()
clock = pg.time.Clock()
all_sprites_list = pg.sprite.Group()
paddle = Paddle(white,100,10)
paddle.rect.x = 350
paddle.rect.y = 560
ball = Ball(white,10,10)
ball.rect.x = 345
ball.rect.y = 195
all_bricks = pg.sprite.Group()
for i in range(7):
brick = Brick(red,80,30)
brick.rect.x = 60 + i* 100
brick.rect.y = 60
all_sprites_list.add(brick)
all_bricks.add(brick)
for i in range(7):
brick = Brick(blue,80,30)
brick.rect.x = 60 + i* 100
brick.rect.y = 100
all_sprites_list.add(brick)
all_bricks.add(brick)
for i in range(7):
brick = Brick(green,80,30)
brick.rect.x = 60 + i* 100
brick.rect.y = 140
all_sprites_list.add(brick)
all_bricks.add(brick)
all_sprites_list.add(paddle)
all_sprites_list.add(ball)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
paddle.moveLeft(10)
if keys[pg.K_RIGHT]:
paddle.moveRight(10)
all_sprites_list.update()
if ball.rect.x>=790:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.x<=0:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.y>590:
ball.velocity[1] = -ball.velocity[1]
global lives
lives-=1
if lives == 0:
font = pg.font.Font(None, 74)
text = font.render("YOU LOSE", 1, red)
screen.blit(text,(280,250))
pg.display.flip()
sys.exit()
if ball.rect.y<40:
ball.velocity[1] = -ball.velocity[1]
if pg.sprite.collide_mask(ball, paddle):
ball.rect.x -= ball.velocity[0]
ball.rect.y -= ball.velocity[1]
ball.bounce()
brick_collision_list = pg.sprite.spritecollide(ball,all_bricks,False)
for brick in brick_collision_list:
ball.bounce()
brick.kill()
if len(all_bricks)==0:
font = pg.font.Font(None, 74)
text = font.render("YOU WIN", 1, green)
screen.blit(text,(280,250))
pg.display.flip()
sys.exit()
screen.fill(black)
font = pg.font.Font(None, 40)
text = font.render("lives: " + str(lives), 1, white)
screen.blit(text, (10,10))
all_sprites_list.draw(screen)
pg.display.flip()
clock.tick(50)
run_game()
When the ball hits the paddle, then set the bottom of the ball to the top of the paddle. It is not necessary to change the direction of the ball, because it's direction is changed in ball.bounce()
if pg.sprite.collide_mask(ball, paddle):
ball.rect.bottom = paddle.rect.top
#ball.rect.x -= ball.velocity[0]
#ball.rect.y -= ball.velocity[1]
ball.bounce()
In Ball.bounce invert the y direction of the ball and compute a new random x direction:
class Ball(pg.sprite.Sprite):
def __init__(self, color, width, height):
# [...]
self.velocity = [randint(-8,8), randint(4,8)]
def bounce(self):
# invert y
self.velocity[1] = -self.velocity[1]
# either random x
# self.velocity[0] = randint(-8, 8)
# or slightly changed x
self.velocity[0] = max(-8, min(8, self.velocity[0] + randint(-3, 3)))
I'm making a Flappy Bird clone in PyGame and I'm trying to add a moving background image. I'm using a 800x600 image right now, and for some reason, when I blit the image, the performance suffers terribly. Is there something I'm doing wrong that is taking up a lot of resources?
import pygame, sys
import random
from pygame import *
pygame.init()
red = (255, 0, 0)
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("flap it up")
clock = pygame.time.Clock()
MAX_VEL = 5
GAP = 175
WALLSPEED = -2
WALLEVENT = pygame.USEREVENT+1
SCOREEVENT = pygame.USEREVENT+2
pygame.time.set_timer(WALLEVENT, 5000)
pygame.time.set_timer(SCOREEVENT, 2500)
myfont = pygame.font.SysFont("monospace", 18)
class Player(object):
def __init__(self):
self.rect = pygame.Rect(384, 284, 32, 32)
self.xvel = 0
self.yvel = MAX_VEL
self.xcel = 0
self.ycel = 1
def move(self):
self.rect.top += self.yvel
if self.yvel < MAX_VEL:
self.yvel += self.ycel
else:
self.yvel = MAX_VEL
def jump(self):
self.yvel = -15
def draw(self):
pygame.draw.rect(screen, red, self.rect)
class Wall(object):
def __init__(self, y):
self.rect1 = pygame.Rect(800, y, 32, 600-y)
self.rect2 = pygame.Rect(800, 0, 32, y-GAP)
self.xvel = WALLSPEED
def move(self, walls):
self.rect1.x += self.xvel
self.rect2.x += self.xvel
if self.rect1.x < -31:
walls.remove(self)
def draw(self):
pygame.draw.rect(screen, green, self.rect1)
pygame.draw.rect(screen, green, self.rect2)
class BG(object):
def __init__(self, x):
self.x = x
self.xvel = WALLSPEED
self.img = pygame.image.load("bg.jpg")
def move(self, bg):
self.x += self.xvel
if self.x < -799:
bg.remove(self)
bg.append(BG(self.x+1600))
screen.blit(self.img, (self.x, 0))
def lose():
pygame.quit()
quit()
def main(player, walls, bg, score, playing):
while playing:
for event in pygame.event.get():
if event.type == KEYDOWN:
player.jump()
if event.type == WALLEVENT:
numbo = random.randrange(GAP, 600, 25)
walls.append(Wall(numbo))
if event.type == SCOREEVENT:
score += 1
for b in bg:
b.move(bg)
label = myfont.render("Score: " + str(score), 1, (0, 0, 0))
screen.blit(label, (30, 20))
player.move()
player.draw()
for w in walls:
w.move(walls)
w.draw()
if player.rect.colliderect(w.rect1) or player.rect.colliderect(w.rect2):
lose()
playing = False
clock.tick(60)
pygame.display.update()
player = Player()
walls = []
bg = []
score = 0
walls.append(Wall(300))
bg.append(BG(0))
bg.append(BG(800))
playing = True
main(player, walls, bg, score, playing)
Typically, this should not be a problem. You should try to set the FPS manually and see if there is a difference
# Create a clock
clock = pygame.time.Clock()
# Set FPS (frames per second)
clock.tick(50)