I'm a super beginner studying Python at Python Game Programming Basic Mathematics and Physics.
As stated in the subject book (textbook), when I entered the code below and ran it on Run Module, an error message appeared, a black screen with pygame window appeared, and I waited for a while, but there was no change.
I tried to move the input position back and forth such as SURFACE(......) and main(), but I got a similar error message and the screen was also black.What is missing in order for the screen to appear like the one in the textbook?
Do I need to install class slider?Thank you for your guidance.
Error Message Excerpt
Traceback (most recent call last):
File "C:\Users\User\Documents\trg_function0.py", line 83, in<module>
main()
File "C:\Users\User\Documents\trg_function0.py", line 41, in main
slider = Slider (Rect(20, 410, 360, 35), 0, 360, 0)
TypeError: Slider() takes no arguments
The code you entered
trg_function0.py
import sys
from path import sin, cos, radians
import pygame
from pygame.locals import Rect, QUIT, MOUSEMOTION, MOUSEBUTTONDOWN, MOUSEBUTTONUP
pygame.init()
SURFACE=pygame.display.set_mode(400,450))
FPSCLOCK=pygame.time.Clock()
Class Slider:
"Slider widget".
def_init__(self, rect, min_value, max_value, value):
self.rect=rect
self.slider_rect=rect.copy()
self.slider_rect.inflate_ip(-20,-20)
self.knob_rect=rect.copy()
self.knob_rect.move_ip(10,0)
self.knob_rect.width=4
self.min_value = min_value
self.max_value = max_value
self.value = value
def draw(self):
""" US>"Draw slider""
pygame.draw.rect (SURFACE, (225, 225, 225), self.rect)
pygame.draw.rect (SURFACE, (64, 64, 128), self.slider_rect)
pygame.draw.rect (SURFACE, (0,0,255), self.knob_rect)
default_pos(self, xpos):
""" US>"Set slider value""
xpos = max(self.slider_rect.left, min(self.slider_rect.right, xpos))
ypos=self.knob_rect.center[1]
self.knob_rect.center=(xpos, ypos)
default_value(self):
""" US>"Retrieve slider value"
ratio=(self.knob_rect.center[0]-self.slider_rect.left)/self.slider_rect.width
return(self.max_value-self.min_value) * ratio+self.min_value
defmain():
""" "Main routine ""
sysfont=pygame.font.SysFont (None, 24)
slider = Slider (Rect(20, 410, 360, 35), 0, 360, 0)
mouse_down = false
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
elifevent.type==MOUSEMOTION:
if mouse_down and slider.rect.collidepoint(event.pos):
slider.set_pos (event.pos[0])
elifevent.type==MOUSEBUTTONDOWN:
mouse_down = True
elifevent.type==MOUSEBUTTONUP:
mouse_down = false
SURFACE.fill((255,255,255))
slider.draw()
for index in range (0,400, 10):
pygame.draw.line (SURFACE, (225, 225, 225), (0, index), (400, index))
pygame.draw.line (SURFACE, (225, 225, 225), (index, 0), (index, 400))
pygame.draw.line(SURFACE,(0,0,0),(0,200),(400,200))
pygame.draw.line(SURFACE,(0,0,0),(200,0),(200,400))
pygame.draw.circle(SURFACE, (255, 0, 0), (200, 200), (150, 2))
theta=slider.get_value()
cos_v=round(cos(radians(theta))), 3)
sin_v=round(sin(radians(theta))), 3)
xpos=cos_v*150+200
ypos=sin_v*-150+200
pygame.draw.line (SURFACE, (0,0,192), (xpos, ypos), (xpos, 200))
pygame.draw.line(SURFACE,(0,192,0),(xpos,ypos),(200,ypos))
pygame.draw.line (SURFACE, (192, 0, 0), (xpos, ypos), (200, 200))
bmp=sysfont.render("cos:{}.format(cos_v), True, (0,0,192)))
SURFACE.blit (bmp, (xpos, 200))
bmp=sysfont.render("sin:{}.format(sin_v), True, (0,192,0))
SURFACE.blit(bmp, (200,ypos))
pygame.display.update()
FPSCLOCK.tick(10)
if__name__=='__main__':
main()
When I searched, the corresponding part of the book appeared, so I looked at it and found that it was a typo.
The source of the questionnaire is different from the description in the book, so please correct the following:
class slider:def_init__(...):
increase the indentation of the entire method
class slider:
def draw(self):
Increase the indentation of pygame.draw.rect() methodsdefmain():mouse_down=false
in two defmain():
starts with false
and starts with false
defmain():Reduce indentation of elifevent.type==MOUSEBUTTONDOWN:
and mouth_down=True
in the first for
loop of
defmain(): The second for
loop in
contains only two lines of pygame.draw.line()
below which pygame.draw.line()
and pygame.draw.circle()
reduce the induct to outside the loop.
defmain(): pygame.draw.circle()
in
and the two parameters , (150,2)
after
are removed from the extra parentheses to make it , 150,2)
Free gift (for good looks)
class Slider:
def draw(self):, def set_pos(self, xpos):
, def get_value(self):
before
The content is the description of the book itself, so I will stop writing the whole revised version.
At the end of the corresponding section of the book, be aware of typing/transcription errors when studying programming.
You don't have to be able to understand the program at this stage.However, enter and run the program to understand the relationship between the center angle and sin/cos.
© 2024 OneMinuteCode. All rights reserved.