Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unknown error in pygame :(
#1
So I am kind of new to pygame and python in general, and I am trying to make this basic game. But when I try to launch the game it just says there is an error and that it can't launch.

Here is the code:
import pygame
import sys
import math
import random
import time
pygame.init

scrn_W = 1200
scrn_H = 500

screen = pygame.display.set_mode((scrn_W,scrn_H))

pygame.display.set_caption("TheGame")

pl_WIDTH = 50
pl_HEIGHT = 50
pl_x = 100
pl_y = 500 - pl_HEIGHT
vel = 10

color = (random.randint(1,255), random.randint(1,255), random.randint(1,255))


isJump = False
jumpCount = 10

obsVel = 5
obsX = scrn_W - obsW
obsY = scrn_H - obsH

run = True
while run:
	pygame.time.delay(15)

	obsXY = (obsX, obsY)

	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			run = False

	keys = pygame.key.get_pressed()

	if keys[pygame.K_RIGHT] and pl_x < scrn_W - pl_WIDTH:
		pl_x += vel

	if keys[pygame.K_LEFT] and pl_x > 0:
		pl_x -= vel

	if not(isJump):
		#if keys[pygame.K_UP] and pl_y > 0:
		#	pl_y -= vel
		#if keys[pygame.K_DOWN] and pl_y < scrn_H - pl_HEIGHT:
		#	pl_y += vel
		if keys[pygame.K_SPACE]:
			isJump = True

	else:
		if jumpCount >= -10:
			pl_y -= (jumpCount * abs(jumpCount)) * 0.5
			jumpCount -= 1

		else:
			isJump = False
			jumpCount = 10

	obsX = 0
	if obsX <= 0:
		obsVel *= 1.04
 		obsW = random.randint(1,100)
		obsH = random.randint(1,100)
		obsWH = (obsW, obsH)
		obsX = scrn_W - obsW
		obsY = scrn_H - obsH
		colorOb = (random.randint(1,255), random.randint(1,255), random.randint(1,255))
		pygame.draw.rect(screen, colorOb, (obsXY, obsWH), 0)

	obsX -= obsVel



	screen.fill((0,0,0))
	pygame.draw.rect(screen, (255,0,0), (pl_x, pl_y, pl_WIDTH, pl_HEIGHT), 0)
	pygame.display.update()

pygame.quit()
The problem is supposedly in the "if obsX <= o:" if statement.

I'd really appreciate some help. Thanks Smile
Reply
#2
and you error is?

i get this error
Error:
Traceback (most recent call last): File "test3.py", line 28, in <module> obsX = scrn_W - obsW NameError: name 'obsW' is not defined
In which that variable is indeed undefined before that line. the only time you assign anything to that variable is on line 69
obsW = random.randint(1,100)
which is after the error
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error UserWarning: Unknown option ssl_ca_certs warnings.warn(str(exc)) Vijayaraj 0 690 Jul-11-2023, 06:01 AM
Last Post: Vijayaraj
  pygame image load error Yegor123 1 1,487 Oct-12-2022, 05:36 AM
Last Post: deanhystad
  Unknown Error with Random Stat Assigner FC8 6 2,263 Dec-06-2021, 01:56 PM
Last Post: FC8
  Unknown error occurred: Port not found NewBeie 0 1,406 Aug-27-2020, 08:50 PM
Last Post: NewBeie
  Help!Unknown ERROR bwdu 1 2,012 Apr-20-2020, 02:09 PM
Last Post: deanhystad
  Unknown error TheIDarKIKnight 0 1,417 Apr-19-2020, 05:27 PM
Last Post: TheIDarKIKnight
  import error (unknown location) pseudo 1 10,677 Nov-14-2019, 11:47 PM
Last Post: pseudo
  pygame.error: /etc/timidity.cfg: No such file or directory lsepolis123 3 4,241 Aug-23-2019, 07:53 AM
Last Post: lsepolis123
  Unknown syntax error (Im new to this) reasonablelevel 2 2,801 Jul-25-2018, 11:59 AM
Last Post: reasonablelevel
  Unknown Syntax Error AlwaysNew 6 5,178 Sep-09-2017, 01:57 AM
Last Post: AlwaysNew

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020