Invalid Syntax on this code, no idea how to fix [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 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

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()

PNG File Can't Open [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
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')

Python, How to render Text Emoji with Pygame? [duplicate]

This question already has answers here:
How to load colorful emojis in pygame?
(2 answers)
Closed last year.
How do I render a emoji in a string in python 3.6 using pygame(font.render(), font.freetype/font.font)? If not possible anymore, I need a code for render in Pillow Draw.text(). I would be grateful.
Discard this: (Filling this question of nothing because looks like my post is mostly code, so I'm adding some nothings to enable the post)
# -*- coding: UTF-8 -*-
import pygame, emoji
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
done = False
pygame.font.init()
font = pygame.freetype.SysFont("segoe-ui-symbol.ttf", size=50)
#font = pygame.font.SysFont("segoe-ui-symbol.ttf", 72)
font_color = (255,255,255, 255)
font_background = (0,0,0, 0)
text_string = emoji.emojize('😖')
#text_string = u'ujiHelloÁpQ|, World 👍😖'
#text_string = u'ujiHelloÁpQ|\U0001f44d, World 😖'
#print(emoji.emojize('Python is :thumbs_up_sign:'))
#text = font.render("♛", True, font_color, (0,0))
#text = font.render(text_string, True, font_color, (0,0))
text = font.render(text_string, fgcolor=font_color, size=0)
image_size = list(text[0].get_size())
text_image = pygame.Surface(image_size)
text_image.fill(font_background)
pygame.display.flip()
text_image.blit(text[0], (0,0))
#print(dir(text_image))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
screen.fill((0, 0, 0))
#screen.blit(text,
screen.blit(text_image,
(320 - text[0].get_width() // 2, 240 - text[0].get_height() // 2))
pygame.display.flip()
clock.tick(60)
I see two possible problems here.
First, if nothing is showing up or the app seems to stall, then Pygame cannot find the SysFont. This was happening on Mac, because there is no segoe-ui-symbol.ttf as system font. As such, I downloaded a version from the internet and created a normal freetype.Font from it.
It works fine.
The second problem may be that the font itself doesn't have those characters in it. Sometimes the OS replaces characters that a font cannot render with a different font.

Python/Pygame, Where do I put my pygame.time.wait loop?

So, I am in the middle of making two, very similar games (Exact details are irrelevant right now.)
Anyway, I need to know where to insert my "pygame.time.wait" code.
The exact code I need looks like this:
pygame.time.wait(100)
score = score + 1
Currently, my code looks like so:
import sys, pygame
from pygame.locals import *
pygame.init()
size = width, height = 800,500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("One Score (Created by - Not Really Working Lamp Productions:)")
WHITE = (255,255,255)
score = 0
screen.fill (WHITE)
myfont = pygame.font.SysFont("monospace", 16)
scoretext = myfont.render("Score = "+str(score), 1, (0,0,0))
screen.blit(scoretext, (5, 10))
disclaimertext = myfont.render("Copyright, 2013, Not Really Working Lamp Productions.", 1, (0,0,0))
screen.blit(disclaimertext, (5, 480))
while 1:
for event in pygame.event.get():
pygame.display.flip()
if event.type == pygame.QUIT:sys.exit()
The problem is though, that I don't know WHERE in this code I should put my pygame.time.wait code. Could anybody fill me in? (I think it goes in the "while 1:" loop, but I don't know where.)
Good afternoon. You should put your 'wait code' into the body of FOR circle:
while 1:
for event in pygame.event.get():
pygame.display.flip()
if event.type == pygame.QUIT:sys.exit()
pygame.time.wait(100)
score = score + 1

Categories

Resources