Posts: 10
Threads: 3
Joined: Jul 2024
So I'm pretty new to coding and I've spent the past two days on a tutorial for making a simple game using pygame because of multiple issues I have had. This issue is listed as "SyntaxError: Non-UTF-8 code starting with '\xff' in file /Users/Itookoutthename/Library/Application Support/JetBrains/PyCharmCE2024.1/light-edit/pexels-photo-207529.jpeg on line 1, but no encoding declared; see https://peps.python.org/pep-0263/ for details" I've been looking online for hours and have no idea how to fix this. Any suggestions?
Posts: 6,794
Threads: 20
Joined: Feb 2020
Jul-18-2024, 08:41 PM
(This post was last modified: Jul-18-2024, 08:42 PM by deanhystad.)
jpeg is a binary format, so decoding does not happen when you read a jpeg file. You must be trying to read the file like it is a text file. I cannot think of a reason why you would do this, so the error is likely a logic error. I cannot offer suggestions on how to fix a logic error without seeing the errant code. You need to post the code that results in this error.
Posts: 10
Threads: 3
Joined: Jul 2024
Jul-19-2024, 12:50 AM
(This post was last modified: Jul-19-2024, 03:02 AM by deanhystad.)
Ok, this is the full code
import pygame
import time
import random
WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Asteroid Dodge')
BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2023.1/light-edit/Unkown.jpeg")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run == False
break
pygame.quit()
deanhystad write Jul-19-2024, 03:02 AM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Posts: 1,145
Threads: 114
Joined: Sep 2019
Jul-19-2024, 04:28 AM
(This post was last modified: Jul-19-2024, 04:28 AM by menator01.)
As the code stands now, it will not run.
Images will need to use win.blit to get the background image to show.
I do not see where you called the main function either.
Here is a basic example
import pygame
import os
# Set a path to current directory of executing script
path = os.path.realpath(os.path.dirname(__file__))
# Pulling image from current directory
# This could be some other path
bg_image = pygame.image.load(f'{path}/my_img.png')
# Set the screen
screen = pygame.display.set_mode((800,600))
running = True
while running:
# Blit the image to the screen surface
screen.blit(bg_image, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the display
pygame.display.update()
Posts: 10
Threads: 3
Joined: Jul 2024
Jul-19-2024, 02:04 PM
(This post was last modified: Jul-19-2024, 02:04 PM by Genericgamemaker.)
Like I said, I'm currently following a tutorial, so the code will not be complete. All I'm trying to figure out is how to fix the error I mentioned in the first post.
Posts: 6,794
Threads: 20
Joined: Feb 2020
Couple of questions. Does it run if you do this:
import pygame
import time
import random
WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Asteroid Dodge")
# BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2023.1/light-edit/Unkown.jpeg")
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False # = for assignment, not == for equality test
break
pygame.quit()
main() You will get a window with a black background with the title Ateroid Dodge.
This confuses me:
BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2023.1/light-edit/Unkown.jpeg") The file path looks like a file in pycharm. Is this a file you created? You should not put your source and support files in pycharm.
Posts: 10
Threads: 3
Joined: Jul 2024
Jul-19-2024, 02:41 PM
(This post was last modified: Jul-19-2024, 02:41 PM by Genericgamemaker.)
BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2024.1/light-edit/Unkown.jpeg") is me trying to set the background to an image, though I haven't actually put that in the code (setting the background to BG). I'm just trying to get the code to read the file but I keep receiving the error message that I listed in the first post. And yes the code you included does run.
Posts: 7,319
Threads: 123
Joined: Sep 2016
Jul-19-2024, 04:01 PM
(This post was last modified: Jul-19-2024, 04:01 PM by snippsat.)
(Jul-18-2024, 08:06 PM)Genericgamemaker Wrote: code starting with '\xff' in file /Users/Itookoutthename/Library/Application Support/JetBrains/PyCharmCE2024.1/light-edit/pexels-photo-207529.jpeg on line 1 You most use whole path on Windows,also with drive name, /Users/ is not correct.
Eg it should be like this C/:Users/<your username>/Itookoutthename/......
Also make you own folder,so avoid these long paths 🚀
Here is working example,see to ways to find files that works r (raw string) or just backslash this way / )
Also i made folder py_game with a img folder.
# space.py
import pygame
import time
import random
pygame.init()
WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Asteroid Dodge')
# Load background image,Windows need whole parth with drive
BG = pygame.image.load(r"G:\div_code\py_game\img\Space.jpg")
# Load spaceship image
SPACESHIP = pygame.image.load("G:/div_code/py_game/img/spaceship.png")
SPACESHIP = pygame.transform.scale(SPACESHIP, (70, 70))
class Spaceship:
def __init__(self):
self.img = SPACESHIP
self.x = WIDTH // 2
self.y = HEIGHT - 400
self.vel = 5
def draw(self, window):
window.blit(self.img, (self.x, self.y))
def move(self, keys):
if keys[pygame.K_LEFT] and self.x - self.vel > 0:
self.x -= self.vel
if keys[pygame.K_RIGHT] and self.x + self.vel + self.img.get_width() < WIDTH:
self.x += self.vel
def main():
run = True
clock = pygame.time.Clock()
spaceship = Spaceship()
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
spaceship.move(keys)
WIN.blit(BG, (0, 0))
spaceship.draw(WIN)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
Posts: 10
Threads: 3
Joined: Jul 2024
If I don't put my files in Pycharm, where should I put them?
Posts: 7,319
Threads: 123
Joined: Sep 2016
(Jul-19-2024, 05:05 PM)Genericgamemaker Wrote: If I don't put my files in Pycharm, where should I put them? You should to learn some basic stuff first,this is needed when doing programming.
Create a New Folder on Windows
So do it both ways as practice with File Explorer and cmd, learn command line stuff using cmd.
|