PNG File Can't Open [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to make a game using python and pygame but for some reason or another pycharm isn't able to open my png file of a race car and I get this error-
Traceback (most recent call last):
File "C:/Users/Zack's PC/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/main1.py", line 14, in <module>
carImg = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
pygame.error: Couldn't open :/Users/Zack's PC/Pictures/gameimages/racecarimage.png
Here is the code
import pygame
pygame.init()
display_width= 900
display_height=600
black= (0,0,0)
white= (255,255,255)
red= (255,0,0)
screen= pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Race Car')
clock = pygame.time.Clock()
carImg = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
def car(x,y):
gameDisplay.blit(carImg,(x,y))
x = (display_height * 0.45)
y = (display_width * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
car(x,y)
gameDisplay.fill(white)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()

You forgot the drive letter, change:
Img = pygame.image.load(':/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')
to:
Img = pygame.image.load('C:/Users/Zack\'s PC/Pictures/gameimages/racecarimage.png')

Related

How do i delete the last number that is being shown? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I am trying to make a game that has a cookie in the middle and if you click that cookie, your points increase
My problem is that once i display the points and increase it, the numbers overlap
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((1000,500))
pygame.display.set_caption("Cookie Clicker")
clock = pygame.time.Clock()
screen.fill("White")
font1 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 25)
font2 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 30)
font3 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 35)
font4 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 40)
font5 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 45)
font6 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 50)
noc = 0
# Texts
nuofco = font4.render(str(noc), False, "Black")
nuofco_r = nuofco.get_rect(center = (237,80))
# Drawn Rects
cookie = pygame.draw.ellipse(screen, "Brown", (110,150,250,250))
pygame.draw.rect(screen, "Black", pygame.Rect(475,0,25,500))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if cookie.collidepoint(event.pos):
noc += 1
screen.blit(nuofco,nuofco_r)
pygame.display.update()
clock.tick(60)
After calculating the new number I think you need to redraw everything on the screen including the background. Because everything you draw on the screen is actually drawn on top of the previous one. And everything overlaps when the background is not drawn.
And also in the main loop, the usually recommended order is to take input, update variables, draw everything. It would be better for you if you first calculate everything that needs to be calculated and then draw what needs to be drawn.

my code gives me a tuple error and i dont know why [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 10 months ago.
Improve this question
MAIN CODE
i dont really know why i keep on getting tuple errors the code looks fine
its just the loop part of my game for now, this is it
import pygame
# important window variables
WIDTH, HEIGHT = 900, 500
WIN = WIDTH, HEIGHT
SCREEN = pygame.display.set_mode((WIN))
pygame.display.set_caption('SPACE GAME')
WHITE = (0,0,0)
# display function (what shows up on the screen)
def display():
WIN.fill(WHITE)
pygame.display.update()
# main function/loop function
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
display()
pygame.quit()
if __name__ == '__main__':
main()
I'm confident this is the issue (though without the real error traceback, I could be wrong.)
The basic issue is that you set the value of WIN as a tuple
i.e.
WIN = BASE, HEIGHT
Then in the display() function, you do the following:
WIN.fill(WHITE)
Which is what I think the error is. WIN being a tuple,
has no fill method. What I think you wanted to use is
SCREEN.fill(WHITE)
I took a gander at the documentation (as I don't know pygame),
and it confirms that: pygame.display.set_mode((WIN))
returns a Surface object which does have a fill()
method.
So, in conclusion:
# important window variables
WIDTH, HEIGHT = 900, 500
WIN = WIDTH, HEIGHT
SCREEN = pygame.display.set_mode((WIN))
pygame.display.set_caption('SPACE GAME')
WHITE = (0,0,0)
# display function (what shows up on the screen)
def display():
SCREEN.fill(WHITE)
pygame.display.update()
# main function/loop function
def main():
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
display()
pygame.quit()
if __name__ == '__main__':
main()

why i have Invalid Syntax in pygame.draw? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
import pygame
from settings import *
from player import Player
import math
from map import *
pygame.init()
sc = pygame.display.set_mode((WIDTH , HEIGHT))
clock = pygame.time.Clock()
player = Player()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
player.movement()
sc.fill(BLACK)
pygame.draw.circle(sc , GREEN , (int(player.x) ,(int(player.y)), 12 )
pygame.draw.line(sc, GREEN, player.pos, (player.x + WIDTH * math.cos(player.angle),
player.y + WIDTH * math.sin(player.angle)))
for x,y in world_map:
pygame.draw.rect(sc , DARKGRAY , (x , y , TILE , TILE),2)
pygame.display.flip()
clock.tick(FPS)
File "C:/Users/ASUS/AppData/Roaming/JetBrains/PyCharmCE2020.2/scratches/game3d.py", line 20
pygame.draw.line(sc, GREEN, player.pos, (player.x + WIDTH * math.cos(player.angle),
^
SyntaxError: invalid syntax
You have an extra parentheses on line 19
pygame.draw.circle(sc , GREEN , (int(player.x) ,(int(player.y)), 12 )
should be
pygame.draw.circle(sc , GREEN , (int(player.x) , int(player.y)), 12 )

Invalid Syntax on this code, no idea how to fix [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
No idea why the syntax is invalid, anyone have an idea? It says the two bottom lines of code in particular have invalid syntax. This is probably embarassingly simple to fix, so please have mercy on my soul.
I haven't tried anything because it's an extremely basic problem and I'm probably just bad at coding
import pygame
import sys
import time
pygame.init()
(width, height) = (1920, 1080)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('fat')
background_colour = pygame.Color('white')
color = pygame.Color('dodgerblue2')
font = pygame.font.Font(None, 100)
clock = pygame.time.Clock()
running = True
text = ''
while running:
# handle events and user-input
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if (event.key >= pygame.K_SPACE and event.key <= pygame.K_z):
# Append key-stroke's character
text += event.unicode
elif (event.key == pygame.K_BACKSPACE):
text = text[:-1]
elif (event.key == pygame.K_RETURN):
#print("interpret(text) - NOT IMPLEMENTED")
text = ""
if len(text) > 20:
text = text[:-1]
# repaint the screen
screen.fill(background_colour)
txt_surface = font.render(text, True, color)
screen.blit(txt_surface, (50, 100))
response = font.render(Room.defaultprompt, True, color)
screen.blit((response,(80, 150))
clock.tick_busy_loop(60) # limit FPS
display.flip()
The code did, but no longer, takes what the user types and presents it onto the screen. It stopped working after I tried to make pygame draw another line of text. (btw I know that Room.defaultprompt is undefined but that's because the rest of the code just isn't in the post)
Looking closely to it, so an error on this line:
screen.blit((response,(80, 150))
When it should be:
screen.blit(response,(80, 150))
#ErikXIII's answer is little different

Why won't pygame display my text? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
My code looks like this, and it opens up the display alright and makes the background white, but it refuses to show my text. It dosnt give any errors either.
import pygame
import time
import random
pygame.init()
black = (0, 0, 0)
gameDisplay = pygame.display.set_mode((1250,700))
white = (255, 255, 255)
def text_objects(text, font, color):
textSurface = font.render(text, True, color,)
return textSurface, textSurface.get_rect()
def letter_input():
gameDisplay.fill(white)
smallText = pygame.font.Font("txt.ttf", 20)
textSurf, textRect = text_objects ("Please input something to say", smallText, black)
textRect.center = (11250/2), (700/2)
gameDisplay.blit(textSurf, textRect)
pygame.display.update()
letter_input()
pygame.display.set_caption("Sentence printer")
clock = pygame.time.Clock()
Quite frankly, I'm just confused at why it won't do this.
textRect.center = (11250/2), (700/2)
Here you set the topleft coordinates of the rect to (5625.0, 350.0) and that's outside of your gameDisplay. Try for example textRect.center = (500, 300) instead.

Categories

Resources